首页 > 解决方案 > 在 Start-Job 中运行时调用 Web-Request Post 未到达 API

问题描述

我有一个 powershell 脚本,它调用我试图在 Azure Runbook 中运行的 API 以使其自动化。从 Web 请求返回响应需要超过 4 分钟,因此我只需触发并忘记它,以避免在 Azure 中触发错误。

我尝试过使用 Start-Job 和 -asJob 但它们中的任何一个都没有运气。我是 powershell 的新手,我曾疯狂地尝试过谷歌搜索,但运气不佳。

当不作为工作运行时,一切正常。任何帮助将不胜感激!

编辑:经过更多挖掘后,我认为这是因为 Azure 的 powershell 处于“受约束的语言模式”,或者至少由于我收到此错误而受到某种限制,Cannot invoke method. Method invocation is supported only on core types in this language mode.而且脚本在我的本地机器上运行良好。有没有另一种方法可以开火并忘记?

脚本:

    Start-Job -ScriptBlock {
    $url = "api.com/apitocall"
    $ID = [System.guid]::New("id")
    $ApiKey = "apikey"
    $credPair = "$($ID):$($ApiKey)"
    $encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
    $headers = @{ Authorization = "Basic $encodedCredentials" }
    $headers.Add('Accept','Application/Json')
    $responseData = Invoke-WebRequest -Uri $url -Method POST -Headers $headers - 
    UseBasicParsing -TimeoutSec 660
    Write-Output($responseData)
    }
Get-Job | Wait-Job

输出:

    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      Job1            BackgroundJob   Running       True            localhost            ...                      
1      Job1            BackgroundJob   Completed     True            localhost            ...      

标签: azurepowershellazure-powershell

解决方案


推荐阅读