slcfreddy
slcfreddy

Reputation: 11

PowerShell API query contains arithmetic operators

Note: I am not a developer, just a scripter muddling along trying to figure stuff out.

Running the following as part of a larger script

$sminfo = Invoke-RestMethod `
  -Method Get `
  -Credential $cred `
  -Uri "https://$($APIServer)/api/6.0/ontap/snap-mirrors?relationship_type=data_protection" `
  -ContentType "application/json"

The object contains several sub objects, $sminfo.result.nextTag is the one I'm concerned with. The original value of this sub object is a string that contains %2b and PowerShell seems to read this as unicode and changes the string like so:

(original) nextTag=PiM%2BMjlmNj

(changed) nextTag=PiM+MjlmNj

How do I prevent PowerShell from changing the output?

Upvotes: 1

Views: 41

Answers (1)

mklement0
mklement0

Reputation: 438073

%2B is the URL-encoded form of the + characters (whose code point is 0x2B), and Invoke-RestMethod has apparently performed decoding for you.

If you really need the encoded form:

$decoded = 'nextTag=PiM+MjlmNj'
$property, $value = $decoded -split '=', 2
$encoded = $property + '=' + [uri]::EscapeDataString($value)

$encoded then contains verbatim nextTag=PiM%2BMjlmNj

Note that the [uri]::EscapeDataString() method is only applied to the value part (everything after =), because applying it to the whole string would also encode =, as %3D.


However, depending on how you later use the decoded value there may be no need to manually re-encode, as this (re-)encoding may happen automatically.

Upvotes: 1

Related Questions