Invoke-RestMethod - 如何传递"ETag不代表资源的最新状态".

gra*_*a36 2 powershell azure azure-powershell azure-automation

如何传递具有以下消息的异常?

Invoke-RestMethod:{"Message":"ETag不代表资源的最新状态."}

看到参考,我认为"传递If-Match: "*" header"应该是关键,但不知道如何做到这一点.

注意:更新或删除文件时,将应用ETag行为.您可以传递If-Match:"*"标头以禁用ETag检查.

去做

  • 将文件"sample1.html"放到网站的特定目录中
  • 在Azure自动化帐户的Runbook中

# Kudu auth information creation
$WebSiteName = Get-AutomationVariable -Name 'WebApps_name'
# Example: "WebApps04"
$username = Get-AutomationVariable -Name 'WebApps_deploy_username'
$password = Get-AutomationVariable -Name 'WebApps_deploy_password'
# Above are set by "Asset - Variable" of in Azure Automation account

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$userAgent = "powershell/1.0"

# Add-content dummy HTML file
$Filename = "sample1.html"
$a = "<HTML><BODY>Sample HTML Data " + $WebSiteName + "</BODY></HTML>"
$a | add-content $Filename

# Get-Content full path of HTML file
$b = get-content $Filename
$filepath = ($b).PSPath

# PUT HTML file $filepath as $Filename, by Kudu REST API
$apiUrl = "https://" + $WebSiteName + ".scm.azurewebsites.net/api/vfs/site/wwwroot/" + $Filename
Write-Output "putting ..."

Invoke-RestMethod `
    -Uri $apiUrl `
    -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} `
    -UserAgent $userAgent `
    -Method PUT `
    -InFile $filePath `
    -ContentType "text/html"

Write-Output "This run is finished."
Run Code Online (Sandbox Code Playgroud)

产量

putting ...

Invoke-RestMethod : {"Message":"ETag does not represent the latest state of the resource."}
At line:30 char:1
+ Invoke-RestMethod `
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], 
WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

This run is finished.
Run Code Online (Sandbox Code Playgroud)

Mat*_*sen 8

正如评论中暗示的那样,-Headers参数接受具有多个键的哈希表,如下所示:

$Headers = @{
    'Authorization' = ('Basic {0}' -f $base64AuthInfo)
    'If-Match'      = '*'
}
Run Code Online (Sandbox Code Playgroud)

然后:

Invoke-RestMethod -Headers $Headers
Run Code Online (Sandbox Code Playgroud)