2

I'm trying to convert the commands from How to redeploy instance using API in rancher to powershell.

pod_upgrade_body=$(curl -u "token-[use your token here]" \
-s 'https://rancher.mydomain.com/v3/project/c-zqpm5:p-h884r/workloads/deployment:development:api' \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' 2>&1 | sed  "s/\"cattle\.io\/timestamp\"\:\"[0-9T:Z-]*\"/\"cattle\.io\/timestamp\":\"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\"/g")

This is my attempt but I can't seem to make it work

Invoke-WebRequest -Method GET -Uri https://rancher... -Header @{Accept = 'application/json'; Authorization = 'Bearer [my-token]'; 'Content-type' = 'application/json'; Pragma = 'no-cache'; 'Cache-Control' = 'no-cache'}  |cat %{$_ -replace "s/\`"cattle.io/timestamp\`":\`"[0-9T:Z-]*\`"/\`"cattle.io/timestamp","$(date -u +"%Y-%m-%dT%H:%M:%SZ")\`"/g"} 

I'm getting the following error

Get-Content : A positional parameter cannot be found that accepts argument '$_ -replace "s/\`"cattle.io/timestamp\`":\`"[0-9T:Z-]*\`"/\`"cattle.io/timestamp","$(date -u 
+"%Y-%m-%dT%H:%M:%SZ")\`"/g"'.
Oplop98
  • 220
  • 1
  • 2
  • 8

1 Answers1

1
  • Since you're receiving JSON data, it is better to use Invoke-RestMethod than Invoke-WebRequest.

  • Doing so means that the JSON response is automatically parsed into a [pscustomobject] object graph that allows direct OO access to the data returned for easy, dot-notation-based querying and updating.

  • You can can re-convert to JSON using ConvertTo-Json (beware the default serialization depth of just 2: use -Depth as needed).

To put it all together:

$fromJson = 
  Invoke-RestMethod -Method GET -Uri https://rancher... -Headers @{Accept = 'application/json'; Authorization = 'Bearer [my-token]'; 'Content-type' = 'application/json'; Pragma = 'no-cache'; 'Cache-Control' = 'no-cache' }  |
    ForEach-Object { 
      $_.annotations.'cattle.io/timestamp' = Get-Date ([datetime]::UtcNow) -UFormat '%Y-%m-%dT%H:%M:%SZ'
    } 

# Re-convert to JSON, if needed.
# Adjust -Depth as needed.
$fromJson | ConvertTo-Json -Depth 5

Note: I'm assuming that the cattle.io/timestamp properties are inside annotations property, based on the forum link in your question.


As for what you tried:

  • In Windows PowerShell (but not PowerShell (Core) 7+), cat is a built in alias for Get-Content, which reads text files specified by their paths - it doesn't support receiving data via the pipeline, the way that cat Unix utility does.

  • However, there is no need for such a utility; the use of the ForEach-Object cmdlet, whose built-in alias % you tried to use, by itself is sufficient to process each input object via the script block ({ ... }) passed to it, inside of which the automatic $_ variable refers to the input object at hand.

  • The translation of the sed command into a -replace operation has several problems:

    • -replace doesn't support sed functions such as s - it is the s (string substitution) function in a manner of speaking.
    • The search regex and the replacement string must be passed as separate arguments (and each must not be enclosed in /.../ delimiters).
    • -replace doesn't support options (other than inline regex option in the regex operand), and it invariably replaces all occurrences; that is, sed's /g option is implied.
  • The external date utility is only available on Unix-like platforms by default; PowerShell's equivalent is the Get-Date cmdlet, as shown above.

mklement0
  • 382,024
  • 64
  • 607
  • 775