问题
I have been trying to automate the following script: https://docs.microsoft.com/en-us/azure/api-management/scripts/powershell-add-user-and-get-subscription-key?toc=/powershell/module/toc.json
via Azure Functions.
# generate a subscription key for the user to call apis which are part of the 'Starter' product
$body = New-AzApiManagementSubscription -Context $context -UserId $user.UserId `
-ProductId $product.ProductId -Name $subscriptionName -State $subscriptionState
}
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})
My code is actually trying to return that subscription key and a 200
, which I'm definitely not getting back. I have a feeling it has something to do with the following weird logging, which I'll include in full below:
[6/12/2020 7:01:18 AM] Host lock lease acquired by instance ID '000000000000000000000000FFE6585A'.
[6/12/2020 7:01:21 AM] Executing HTTP request: {
[6/12/2020 7:01:21 AM] "requestId": "da9db8fe-4f3e-4d31-a69b-447a352d587b",
[6/12/2020 7:01:21 AM] "method": "POST",
[6/12/2020 7:01:21 AM] "uri": "/api/course_api_mgmt"
[6/12/2020 7:01:21 AM] }
[6/12/2020 7:01:21 AM] Executing 'Functions.course_api_mgmt' (Reason='This function was programmatically called via the host APIs.', Id=bfc24fc2-1ecc-440d-84d2-52ef29248c9e)
[6/12/2020 7:01:32 AM] System Log: { Log-Level: Trace; Log-Message: Profile invocation completed in 11164 ms. }
[6/12/2020 7:01:32 AM] System Log: { Invocation-Id: bfc24fc2-1ecc-440d-84d2-52ef29248c9e; Log-Level: Trace; Log-Message: Invoking function 'course_api_mgmt' code 11295 ms after receiving request. Invocation performance details: DependenciesAvailable: 0 ms; RunspaceAvailable: 11187 ms; MetadataAndTraceContextReady: 11287 ms; FunctionCodeReady: 11290 ms; InputBindingValuesReady: 11294 ms; InvokingFunctionCode: 11294 ms; }
[6/12/2020 7:01:32 AM] INFORMATION: PowerShell HTTP trigger function processed a request.
[6/12/2020 7:01:34 AM] WARNING: The provided service principal secret will be included in the 'AzureRmContext.json' file found in the user profile ( C:\Users\******\.Azure ). Please ensure that this directory has appropriate
protections.
[6/12/2020 7:01:34 AM] OUTPUT:
[6/12/2020 7:01:36 AM] OUTPUT: Account SubscriptionName TenantId Environment
[6/12/2020 7:01:36 AM] OUTPUT: ------- ---------------- -------- -----------
[6/12/2020 7:01:36 AM] OUTPUT: ****** course_api_mgmt ****** AzureCloud
[6/12/2020 7:01:36 AM] OUTPUT:
[6/12/2020 7:01:36 AM] OUTPUT: Name : course_api_mgmt (******) - ******
[6/12/2020 7:01:36 AM] OUTPUT: Account : ******
[6/12/2020 7:01:36 AM] OUTPUT: Environment : AzureCloud
[6/12/2020 7:01:36 AM] OUTPUT: Subscription : ******
[6/12/2020 7:01:36 AM] OUTPUT: Tenant : ******
[6/12/2020 7:01:36 AM] OUTPUT: TokenCache : Microsoft.Azure.Commands.Common.Authentication.Core.ProtectedFileTokenCache
[6/12/2020 7:01:36 AM] OUTPUT: VersionProfile :
[6/12/2020 7:01:36 AM] OUTPUT: ExtendedProperties : {}
[6/12/2020 7:01:36 AM] OUTPUT:
[6/12/2020 7:01:37 AM] INFORMATION: Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext
[6/12/2020 7:01:39 AM] INFORMATION: Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementUser
[6/12/2020 7:01:39 AM] ERROR: Get-AzApiManagementProduct : The pipeline has been stopped.
[6/12/2020 7:01:39 AM] At C:\Users\******\AzureProjects\course_api_mgmt\course_api_mgmt\run.ps1:56 char:16
[6/12/2020 7:01:39 AM] + ... $product = Get-AzApiManagementProduct -Context $context -Title 'Star ...
[6/12/2020 7:01:39 AM] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[6/12/2020 7:01:39 AM] + CategoryInfo : CloseError: (:) [Get-AzApiManagementProduct], PipelineStoppedException
[6/12/2020 7:01:39 AM] + FullyQualifiedErrorId : Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementProduct
[6/12/2020 7:01:39 AM]
[6/12/2020 7:01:39 AM] Script stack trace:
[6/12/2020 7:01:39 AM] at <ScriptBlock>, C:\Users\******\AzureProjects\course_api_mgmt\course_api_mgmt\run.ps1: line 56
[6/12/2020 7:01:39 AM]
[6/12/2020 7:01:39 AM] System.Management.Automation.PipelineStoppedException: The pipeline has been stopped.
[6/12/2020 7:01:39 AM] at System.Management.Automation.CommandProcessor.ProcessRecord()
[6/12/2020 7:01:39 AM] at System.Management.Automation.CommandProcessorBase.DoExecute()
[6/12/2020 7:01:39 AM] at System.Management.Automation.Internal.Pipe.AddItems(Object objects)
[6/12/2020 7:01:39 AM] at System.Management.Automation.MshCommandRuntime._WriteObjectsSkipAllowCheck(Object sendToPipeline)
[6/12/2020 7:01:40 AM] at System.Management.Automation.MshCommandRuntime.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at System.Management.Automation.Cmdlet.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementProduct.ExecuteApiManagementCmdlet()
[6/12/2020 7:01:40 AM] at Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.AzureApiManagementCmdletBase.ExecuteCmdlet()
[6/12/2020 7:01:40 AM]
[6/12/2020 7:01:40 AM]
[6/12/2020 7:01:40 AM] Result: ERROR: Get-AzApiManagementProduct : The pipeline has been stopped.
[6/12/2020 7:01:40 AM] At C:\Users\******\AzureProjects\course_api_mgmt\course_api_mgmt\run.ps1:56 char:16
[6/12/2020 7:01:40 AM] + ... $product = Get-AzApiManagementProduct -Context $context -Title 'Star ...
[6/12/2020 7:01:40 AM] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[6/12/2020 7:01:40 AM] + CategoryInfo : CloseError: (:) [Get-AzApiManagementProduct], PipelineStoppedException
[6/12/2020 7:01:40 AM] + FullyQualifiedErrorId : Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementProduct
[6/12/2020 7:01:40 AM]
[6/12/2020 7:01:40 AM] Script stack trace:
[6/12/2020 7:01:40 AM] at <ScriptBlock>, C:\Users\******\AzureProjects\course_api_mgmt\course_api_mgmt\run.ps1: line 56
[6/12/2020 7:01:40 AM]
[6/12/2020 7:01:40 AM] System.Management.Automation.PipelineStoppedException: The pipeline has been stopped.
[6/12/2020 7:01:40 AM] at System.Management.Automation.CommandProcessor.ProcessRecord()
[6/12/2020 7:01:40 AM] at System.Management.Automation.CommandProcessorBase.DoExecute()
[6/12/2020 7:01:40 AM] at System.Management.Automation.Internal.Pipe.AddItems(Object objects)
[6/12/2020 7:01:40 AM] at System.Management.Automation.MshCommandRuntime._WriteObjectsSkipAllowCheck(Object sendToPipeline)
[6/12/2020 7:01:40 AM] at System.Management.Automation.MshCommandRuntime.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at System.Management.Automation.Cmdlet.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementProduct.ExecuteApiManagementCmdlet()
[6/12/2020 7:01:40 AM] at Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.AzureApiManagementCmdletBase.ExecuteCmdlet()
[6/12/2020 7:01:40 AM]
[6/12/2020 7:01:40 AM]
Exception: The pipeline has been stopped.
Stack: at System.Management.Automation.CommandProcessor.ProcessRecord()
[6/12/2020 7:01:40 AM] at System.Management.Automation.CommandProcessorBase.DoExecute()
[6/12/2020 7:01:40 AM] at System.Management.Automation.Internal.Pipe.AddItems(Object objects)
[6/12/2020 7:01:40 AM] at System.Management.Automation.MshCommandRuntime._WriteObjectsSkipAllowCheck(Object sendToPipeline)
[6/12/2020 7:01:40 AM] at System.Management.Automation.MshCommandRuntime.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at System.Management.Automation.Cmdlet.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.WriteObject(Object sendToPipeline, Boolean enumerateCollection)
[6/12/2020 7:01:40 AM] at Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementProduct.ExecuteApiManagementCmdlet()
[6/12/2020 7:01:40 AM] at Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.AzureApiManagementCmdletBase.ExecuteCmdlet().
[6/12/2020 7:01:40 AM] OUTPUT:
[6/12/2020 7:01:40 AM] OUTPUT: ProductId : starter
[6/12/2020 7:01:40 AM] OUTPUT: Title : Starter
[6/12/2020 7:01:40 AM] OUTPUT: Description : Subscribers will be able to run 5 calls/minute up to a maximum of 100 calls/week.
[6/12/2020 7:01:40 AM] OUTPUT: LegalTerms :
[6/12/2020 7:01:40 AM] OUTPUT: SubscriptionRequired : True
[6/12/2020 7:01:40 AM] OUTPUT: ApprovalRequired : False
[6/12/2020 7:01:40 AM] OUTPUT: SubscriptionsLimit : 1
[6/12/2020 7:01:40 AM] OUTPUT: State : Published
[6/12/2020 7:01:40 AM] OUTPUT: Id : /subscriptions/******/resourceGroups/******/providers/Microsoft.ApiManagement/service/******/products/starter
[6/12/2020 7:01:40 AM] OUTPUT: ResourceGroupName : ******
[6/12/2020 7:01:40 AM] OUTPUT: ServiceName : ******
[6/12/2020 7:01:40 AM] OUTPUT:
[6/12/2020 7:01:40 AM] Executed 'Functions.course_api_mgmt' (Succeeded, Id=bfc24fc2-1ecc-440d-84d2-52ef29248c9e)
[6/12/2020 7:01:40 AM] Executed HTTP request: {
[6/12/2020 7:01:40 AM] "requestId": "da9db8fe-4f3e-4d31-a69b-447a352d587b",
[6/12/2020 7:01:40 AM] "method": "POST",
[6/12/2020 7:01:40 AM] "uri": "/api/course_api_mgmt",
[6/12/2020 7:01:40 AM] "identities": [
[6/12/2020 7:01:40 AM] {
[6/12/2020 7:01:40 AM] "type": "WebJobsAuthLevel",
[6/12/2020 7:01:40 AM] "level": "Admin"
[6/12/2020 7:01:40 AM] }
[6/12/2020 7:01:40 AM] ],
[6/12/2020 7:01:40 AM] "status": 204,
[6/12/2020 7:01:40 AM] "duration": 19341
[6/12/2020 7:01:40 AM] }
The function is clearly working in that it does the physical job of creating the user
but this is of little use if I am unable to return the subscription key in the response body.
Thoughts on how to fix this?
Edit: followed a suggestion to iteratively run commands in my local Powershell ISE terminal
PS C:\Windows\system32> Install-Module -Name AzureAutomationAuthoringToolkit
PS C:\Windows\system32> $userEmail = "test3@gmail.com"
PS C:\Windows\system32> $userFirstName = "Test"
PS C:\Windows\system32> $userLastName = "User"
PS C:\Windows\system32> $userPassword = ConvertTo-SecureString -String "234" -AsPlainText -Force
PS C:\Windows\system32> $userNote = ""
PS C:\Windows\system32> $subscriptionId = "**********************"
PS C:\Windows\system32> $apimServiceName = "**********************"
PS C:\Windows\system32> $resourceGroupName = "**********************"
PS C:\Windows\system32> $userState = "Active"
PS C:\Windows\system32> $subscriptionName = "**********************"
PS C:\Windows\system32> $subscriptionState = "Active"
PS C:\Windows\system32> $User = "**********************"
PS C:\Windows\system32> $PWord = ConvertTo-SecureString -String **********************" -AsPlainText -Force
PS C:\Windows\system32> $tenant = "**********************"
PS C:\Windows\system32> $Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
PS C:\Windows\system32> if ($PSVersionTable.PSEdition -eq 'Desktop' -and (Get-Module -Name AzureRM -ListAvailable)) {
Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
'Az modules installed at the same time is not supported.')
} else {
Install-Module -Name Az -AllowClobber -Scope CurrentUser
}
PS C:\Windows\system32> Get-ExecutionPolicy
Restricted
PS C:\Windows\system32> Set-ExecutionPolicy -ExecutionPolicy Unrestricted
PS C:\Windows\system32> Import-Module Az.Accounts
PS C:\Windows\system32> Connect-AzAccount -Credential $Credential -Tenant $tenant -ServicePrincipal
WARNING: The provided service principal secret will be included in the 'AzureRmContext.json' file found in the user profile ( C:\Users\Aubrey\.Azure ). Please ensure that
this directory has appropriate protections.
Account SubscriptionName TenantId Environment
------- ---------------- -------- -----------
********************** ********************** ********************** AzureCloud
PS C:\Windows\system32> Select-AzSubscription -SubscriptionId $subscriptionId
Name Account SubscriptionName Environment TenantId
---- ------- ---------------- ----------- --------
********************** (**********************... **********************... ********************** AzureCloud **********************...
PS C:\Windows\system32> $context = New-AzApiManagementContext -ResourceGroupName $resourceGroupName -ServiceName $apimServiceName
PS C:\Windows\system32> $context
ResourceGroupName ServiceName
----------------- -----------
********************** **********************
PS C:\Windows\system32> $user = New-AzApiManagementUser -Context $context -FirstName $userFirstName -LastName $userLastName `
-Password $userPassword -State $userState -Note $userNote -Email $userEmail
PS C:\Windows\system32> $user
UserId : d3938bdc824e4674a1c0b0d159f56638
FirstName : Test
LastName : User
Email : test3@gmail.com
State : Active
RegistrationDate : 6/12/2020 9:01:09 PM
Note :
Identities : {[test3@gmail.com, Basic]}
Id : /subscriptions/**********************/resourceGroups/**********************/providers/Microsoft.ApiManagement/service/**********************/users/**********************
ResourceGroupName : **********************
ServiceName : **********************
PS C:\Windows\system32> $product = Get-AzApiManagementProduct -Context $context -Title 'Starter' | Select-Object -First 1
Get-AzApiManagementProduct : The pipeline has been stopped.
At line:1 char:12
+ $product = Get-AzApiManagementProduct -Context $context -Title 'Start ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzApiManagementProduct], PipelineStoppedException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementProduct
ProductId : starter
Title : Starter
Description : Subscribers will be able to run 5 calls/minute up to a maximum of 100 calls/week.
LegalTerms :
SubscriptionRequired : True
ApprovalRequired : False
SubscriptionsLimit : 1
State : Published
Id : /subscriptions/**********************/resourceGroups/**********************/providers/Microsoft.ApiManagement/service/**********************/products/start
er
ResourceGroupName : **********************
ServiceName : **********************
PS C:\Windows\system32> $body = New-AzApiManagementSubscription -Context $context -UserId $user.UserId `
-ProductId $product.ProductId -Name $subscriptionName -State $subscriptionState
New-AzApiManagementSubscription : Cannot validate argument on parameter 'ProductId'. The argument is null or empty. Provide an argument that is not null or empty, and
then try the command again.
At line:2 char:20
+ -ProductId $product.ProductId -Name $subscriptionName -State ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-AzApiManagementSubscription], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.NewAzureApiManagementSubscription
PS C:\Windows\system32> $product
PS C:\Windows\system32>
回答1:
I summarize the solution as below.
According to the error message, we cannot run the command Get-AzApiManagementProduct
and Select-Object -Frist 1
in the PowerShell pipeline. So I suggest you update the command Get-AzApiManagementProduct -Context $context -Title 'Starter' | Select-Object -First 1
to
$product = Get-AzApiManagementProduct -Context $context -Title 'Starter'
$product = $product | Select-Object -First 1
来源:https://stackoverflow.com/questions/62339453/error-get-azapimanagementproduct-the-pipeline-has-been-stopped-when-adding-a