通过Azure函数。
# 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
})
我的代码实际上正在尝试返回该订阅密钥和一个200
,我绝对不会回来的。我觉得这与以下奇怪的日志记录有关,我将在下面全文介绍:
[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] }
该功能很明显是在完成创建用户的实际工作中起作用
但是如果我无法在响应正文中返回订阅密钥,这将毫无用处。
关于如何解决此问题的想法?
编辑:遵循了在本地Powershell ISE终端中迭代运行命令的建议
PS C:\Windows\system32> Install-Module -Name AzureAutomationAuthoringToolkit
PS C:\Windows\system32> $userEmail = "[email protected]"
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 : [email protected]
State : Active
RegistrationDate : 6/12/2020 9:01:09 PM
Note :
Identities : {[[email protected], 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>
我总结如下解决方案。
根据错误消息,我们无法在PowerShell管道中运行命令Get-AzApiManagementProduct
和Select-Object -Frist 1
。因此,我建议您将命令Get-AzApiManagementProduct -Context $context -Title 'Starter' | Select-Object -First 1
更新为
$product = Get-AzApiManagementProduct -Context $context -Title 'Starter'
$product = $product | Select-Object -First 1