根據 OData 端點產生 PowerShell CmdletGenerate PowerShell Cmdlets based on OData Endpoint

根據 OData 端點產生 Windows PowerShell CmdletGenerate Windows PowerShell cmdlets based on an OData endpoint

Export-ODataEndpointProxy 是 Cmdlet,它會根據指定的 OData 端點所公開的功能產生一組 Windows PowerShell Cmdlet。Export-ODataEndpointProxy is a cmdlet that generates a set of Windows PowerShell cmdlets based on the functionality exposed by a given OData endpoint.

下例示範如何使用這個新的 Cmdlet:The following example shows how to use this new cmdlet:

# Export-ODataEndpointProxy 基本使用案例# Basic use case of Export-ODataEndpointProxy

Export-ODataEndpointProxy -Uri 'http://services.odata.org/v3/(S(snyobsk1hhutkb2yulwldgf1))/odata/odata.svc' -OutputModule C:\Users\user\Generated.psd1

ipmo 'C:\Users\user\Generated.psd1'
# Cmdlets are created based on the following heuristics
# New-<EntityType> -<Key> [-<Other Attributes>]
#
# Get-<EntityType> [-<Key> -Top –Skip –Filter -OrderBy]
# # If there is a complex key, the keys will actually be -<Key1> -<Key2>…
# # Note this rule applies to any other instances of the key
#
# Set-<EntityType> -<Key> [-<Other Attributes>]
#
# Remove-<EntityType> -<Key>
#
# Invoke-<EntityType><Action> [-<Key> -<Other Parameters>]
#
#
# Cmdlets from associations (Note: Get and Remove get additional parameter sets)
# Get-<EntityType> -<AssociatedEntity>
# New-<EntityType> -<AssociatedEntity> -<Key>
# Remove-<EntityType> -<AssociatedEntity> -<Key>
#
#
# Note: Every cmdlet has the –ConnectionURI parameter for explicitly setting the URI of the endpoint. This normally uses the same address that you gave the Export-ODataEndpointProxy cmdlet, but can be overridden in this fashion for the sake of similar endpoints.
#

這項功能仍有部分主要使用案例仍在開發中,包括但不限於:There are still parts of key use cases in development for this functionality, including, but not limited to:

  • 關聯Associations
  • 傳遞資料流Passing streams

根據具有 ODataUtils 的 OData 端點產生 Windows PowerShell CmdletGenerate Windows PowerShell cmdlets based on an OData endpoint with ODataUtils

ODataUtils 模組可以從支援 OData 的 REST 端點產生 Windows PowerShell Cmdlet。The ODataUtils module allows generation of Windows PowerShell cmdlets from REST endpoints that support OData. 下列的累加增強功能位在 Microsoft.PowerShell.ODataUtils Windows PowerShell 模組中。The following incremental enhancements are in the Microsoft.PowerShell.ODataUtils Windows PowerShell module.

  • 從伺服器端端點到用戶端的通道其他資訊。Channel additional information from server-side endpoint to client side.
  • 用戶端的分頁支援Client-side paging support
  • 使用 -Select 參數進行伺服器端篩選Server-side filtering by using the -Select parameter
  • Web 要求標頭的支援Support for web request headers

Export-ODataEndPointProxy Cmdlet 產生的 Proxy Cmdlet 提供伺服器端 OData 端點資訊資料流 (新的 Windows PowerShell 5.0 功能) 的其他資訊 (用戶端 Proxy 產生期間所使用的 $metadata 中未提及)。The proxy cmdlets generated by the Export-ODataEndPointProxy cmdlet provide additional information (not mentioned in the $metadata used during the client-side proxy generation) from the server side OData endpoint on the Information stream (a new Windows PowerShell 5.0 feature). 下例為取得這項資訊的方法。Here is an example of how to get that information.

Import-Module Microsoft.PowerShell.ODataUtils -Force
$generatedProxyModuleDir = Join-Path -Path $env:SystemDrive -ChildPath 'ODataDemoProxy'
$uri = "http://services.odata.org/V3/(S(fhleiief23wrm5a5nhf542q5))/OData/OData.svc/"
Export-ODataEndpointProxy -Uri $uri -OutputModule $generatedProxyModuleDir -Force -AllowUnSecureConnection -Verbose -AllowClobber
Import-Module $generatedProxyModuleDir -Force

# In the below command, we are retrieving top 1 product.
# By specifying -IncludeTotalResponseCount parameter,
# we are getting the total count of all the Product records
# available on the server side. This information
# is surfaced on the client side through the Information stream.
$product = Get-Product -Top 1 -AllowUnsecureConnection -AllowAdditionalData -IncludeTotalResponseCount -InformationVariable infoStream
# The Information stream contains the additional
# information sent from the server side.
$additionalInfo = $infoStream.GetEnumerator() | % MessageData
# 'Odata.Count' indicates the total product records
# available on the server side Odata endpoint.
$additionalInfo['odata.count']

您可以使用用戶端的分頁支援,從批次的伺服器端取得記錄。You can get the records from the server side in batches by using client-side paging support. 當您必須透過網路從伺服器取得大量資料時,這非常有用。This is useful when you must get a large amount of data from the server over the network.

$skipCount = 0
$batchSize = 3
# Client-Side Paging Support: The records from the server side
# are retrieved in batches of $batchSize
while($skipCount -le $additionalInfo['odata.count'])
{
Get-Product -AllowUnsecureConnection -AllowAdditionalData -Top $batchSize -Skip $skipCount
$skipCount += $batchSize
}

產生的 Proxy Cmdlet 支援 –Select 參數,這可用為篩選條件,只接收用戶端需要的記錄屬性。The generated proxy cmdlets support the –Select parameter which you can use as a filter to receive only the record properties that the client needs. 因為篩選發生在伺服器端,所以這會減少透過網路傳送的資料量。This reduces the amount of data that is transferred over the network, because the filtering occurs on the server side.

# In the below example only the Name property of the
# Product record is retrieved from the server side.
Get-Product -Top 2 -AllowUnsecureConnection -AllowAdditionalData -Select Name

Export-ODataEndpointProxy Cmdlet 和它產生的 Proxy Cmdlet,現在支援標頭參數 (提供值當做雜湊表),可用來傳輸伺服器端 OData 端點所預期的任何其他資訊。The Export-ODataEndpointProxy cmdlet, and the proxy cmdlets generated by it, now support the Headers parameter (supply values as a hash table), which you can use to channel any additional information that is expected by the server-side OData endpoint. 在下列範例中,您可以透過期待驗證訂閱金鑰的服務標頭傳輸訂閱金鑰。In the following example, you can channel a Subscription key through Headers for services that are expecting a Subscription key for authentication.

# As an example, in the below command 'XXXX' is the authentication used by the
# Export-ODataEndpointProxy cmdlet to interact with the server-side
# OData endpoint accessed through $endPointUri.

Export-ODataEndpointProxy -Uri $endPointUri -OutputModule $generatedProxyModuleDir -Force -AllowUnSecureConnection -Verbose -Headers @{'subscription-key'='XXXX'}