我一直在尝试使用 http 触发的 powershell Azure Function 创建 Azure 容器实例。
我写了一些代码,它可以工作......有点
using namespace System.Net
param($Request, $TriggerMetadata)
$appId = "appID"
$appPassword = "appPassword"
$secpasswd = ConvertTo-SecureString $appPassword -AsPlainText -Force
$mycred = New-Object System.Management.Automation.PSCredential ($appId, $secpasswd)
$status = [HttpStatusCode]::OK
New-AzContainerGroup -ResourceGroupName "test" -Name "testName" `
-Image "imageName" `
-RegistryCredential $mycred `
-RestartPolicy Never
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})
当我到达端点时,我总是收到 500 响应代码,但由于某种原因容器实例无论如何都会被创建并且它可以工作。这是来自终端的错误:
Executed 'Functions.testFuntion' (Failed, Id=240084c5-6aa2-4207-ae84-66f85a03504b)
System.Private.CoreLib: Exception while executing function: Functions.testFuntion.
System.Private.CoreLib: Result: Failure
Exception: Object reference not set to an instance of an object.
Stack:
at
Microsoft.Azure.Functions.PowerShellWorker.Utility.TypeExtensions.
DeriveContentType(HttpResponseContext
Microsoft.Azure.Functions.PowerShellWorker.Utility.TypeExtensions.DeriveContentType
(HttpResponseContext httpResponseContext, RpcHttp rpcHttp) in C:\projects\azure-functions-powershell-
worker\src\Utility\TypeExtensions.cs:line 196
at
Microsoft.Azure.Functions.PowerShellWorker.Utility.TypeExtensions.ToRpcHttp(HttpResponseContext
httpResponseContext) in C:\projects\azure-functions-powershell-
worker\src\Utility\TypeExtensions.cs:line 188
at
Microsoft.Azure.Functions.PowerShellWorker.Utility.TypeExtensions.ToTypedData(Object value) in
C:\projects\azure-functions-powershell-worker\src\Utility\TypeExtensions.cs:line 242
at
Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.BindOutputFromResult(InvocationResponse
response, AzFunctionInfo functionInfo, Hashtable results) in C:\projects\azure-functions-powershell-
worker\src\RequestProcessor.cs:line 465
at
Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessInvocationRequestImpl
(StreamingMessage request, AzFunctionInfo functionInfo, PowerShellManager psManager,
FunctionInvocationPerformanceStopwatch stopwatch)
in C:\projects\azure-functions-powershell-worker\src\RequestProcessor.cs:line 305.
这是我应该担心的事情吗?尽管我收到了 500 条回复,但一切似乎都正常。
最佳答案
在 Push-OutputBinding 响应中使用 $body 之前,您可能需要定义它。
类似于:
$body = "My Message"
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})
关于Azure Functions-新-AzContainerGroup : Object reference not set to an instance of an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60977663/