繁体   English   中英

通过在Powershell脚本中使用Rest API Post方法无法停止WebJobs-获取错误403-该Web应用程序已停止

[英]WebJobs not stopping by using the Rest API Post method within powershell script - Getting Error 403 - This web app is stopped

当在我的应用程序上使用天蓝色切换插槽时,我试图停止“已停止”服务器上的webjob,因为它们似乎一直在天蓝色内运行。 但是,当我在Powershell脚本中使用invoke-restmethod时,它又返回错误403-此Web应用程序已停止,但在我的Azure网站上它仍在运行。 令人沮丧的是,我可以在使用邮递员时使用该方法,但是当涉及到具有相同请求的脚本时,它不会停止webjobs。

我曾尝试在Powershell中运行此特定脚本,但由于所述错误而无法运行。 Get方法在Powershell中可以正常工作,可以将应用程序带回,但是stop rest调用却不能。

function StopWebJob($slotToStop) {
    # Get an access token to authenticate the web jobs API
    Write-Verbose -Verbose "Getting an access token..."
    $stopToken = Get-BasicAuthToken $slotToStop
    Write-Verbose -Verbose "Got access token."

    #Generate a header to start/stop the web jobs using the access token
    $stopHeader = @{ 'Authorization'= $stopToken }

    Write-Verbose -Verbose "Checking job status..."
    $getJob = "https://$WebAppName-$slotToStop.scm.azurewebsites.net/api/continuouswebjobs/{myWebJob}/"
    $getJobResult = Invoke-RestMethod -Uri $getJob -Headers $stopHeader -Method Get

    If($getJobResult.status -ne "Stopped") {
        Write-Verbose -Verbose "Stopping job..."
        $stop = "https://$WebAppName-$slotToStop.scm.azurewebsites.net/api/continuouswebjobs/{MyWebJob}/stop"
        Invoke-RestMethod -Uri $stop -Headers $stopHeader -Method Post
        Write-Verbose -Verbose "Job stopped."
    } else {    
        Write-Verbose -Verbose "Job is already stopped."
    }
}

当插槽切换时,我希望“ rest”插槽上的Job通过rest api停止。

我可以重现您的问题,看起来你错过了-UserAgentInvoke-RestMethod

在此处输入图片说明

如下所示,将-UserAgent "powershell/1.0"到您的命令中。

Invoke-RestMethod -Uri $stop -Headers $stopHeader -Method Post -UserAgent "powershell/1.0"

然后它将正常工作,您可以参考我的完整示例。

$username = "`$website"
$password = "pwd"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))

Write-Verbose -Verbose "Checking job status..."
$getJob = "https://<webappname>-slot1.scm.azurewebsites.net/api/continuouswebjobs/webjob1"
$getJobResult = Invoke-RestMethod -Uri $getJob -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get

If($getJobResult.status -ne "Stopped") {
        Write-Verbose -Verbose "Stopping job..."
        $stop = "https://<webappname>-slot1.scm.azurewebsites.net/api/continuouswebjobs/webjob1/stop"
        Invoke-RestMethod -Uri $stop -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -UserAgent "powershell/1.0"
        Write-Verbose -Verbose "Job stopped."
    } else {    
        Write-Verbose -Verbose "Job is already stopped."
    }

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM