5

Question

How to pass the exception having the following message?

Invoke-RestMethod : {"Message":"ETag does not represent the latest state of the resource."}

Seeing the reference, I think "passing If-Match: "*" header" should be a key, but don't know how to do this.

Note: when updating or deleting a file, ETag behavior will apply. You can pass a If-Match: "*" header to disable the ETag check.

To do

  • Putting the file "sample1.html" to specific directory of the website
  • In a Runbook of Azure Automation account

Code

# 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."

Output

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.
grantaka36
  • 277
  • 1
  • 2
  • 14

2 Answers2

9

As hinted in the comments, the -Headers parameter accept a hashtable with multiple keys, like so:

$Headers = @{
    'Authorization' = ('Basic {0}' -f $base64AuthInfo)
    'If-Match'      = '*'
}

and then:

Invoke-RestMethod -Headers $Headers
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
1

This code is working fine...!!!

# 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

# Debug printing
Write-Output "debug printing ..." ;
Write-Output $username ;
Write-Output $WebSiteName ;
Write-Output $filepath ;
Write-Output $a ;

# PUT HTML file $filepath as $Filename, by Kudu REST API
$apiUrl = "https://" + $WebSiteName + ".scm.azurewebsites.net/api/vfs/site/wwwroot/" + $Filename
Write-Output "putting ..." ; 
$HeaderValues = @{
    'Authorization' = ('Basic {0}' -f $base64AuthInfo)
    'If-Match'      = '*'
        }
# Invoking Rest Method Passing $HeaderValues as parameter
Invoke-RestMethod `
-Uri $apiUrl `
-Headers $HeaderValues `
-UserAgent $userAgent `
-Method PUT `
-InFile $filePath `
-ContentType "text/html"

Write-Output "This run is finished." ;
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45