1

Problem

I am unable to use an Environment Variable as a token in an Invoke-RestMethod

I have the following code, that does work:

$props = @{
    Uri = $my_url
    Method = "POST"
    ContentType = "application/json"
    Headers = @{ Authorization = "Bearer Token123456789" }
}

$payload = Invoke-RestMethod @props

But I don't want the token hard-coded here.

However, if I replace Token123456789 with the environment variable $env:token which contains the identical token, it fails.

$props = @{
    Uri = $my_url
    Method = "POST"
    ContentType = "application/json"
    Headers = @{ Authorization = "Bearer $env:token" }
}

$payload = Invoke-RestMethod @props

The error message is

Invoke-RestMethod : Specified value has invalid Control characters.

Parameter name: value

At C:\Temp\test.ps1:29 char:12

$payload = Invoke-RestMethod @props

  • CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ArgumentException
  • FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

What I have tried

I have tried comparing them directly, like this

$bearer1 = "$env:token"
$bearer2 = "Token123456789"
echo $bearer1
echo $bearer2
echo ($bearer1 -eq $bearer2)

The first two echo's output the same results.

However, the last one returns False

Conclusion

The Environment Variable for some reason does not equal its string equivalent.

How do I turn the environment variable into this identical string so that it will work in my Invoke-RestMethod?

Tyler
  • 85
  • 8
  • 1
    Use the [subexpression operator](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_operators#subexpression-operator--): `"Bearer $($env:token)"` – iRon Nov 17 '22 at 18:18
  • How do you define `$env:token` ? – Santiago Squarzon Nov 17 '22 at 18:25
  • assuming this could be a graph API query, if you use `Get-MsalToken`, something like this should work fine: `$result = Get-MsalToken @tokenParams; $env:token = $result.CreateAuthorizationHeader()` – Santiago Squarzon Nov 17 '22 at 18:30
  • @iRon No, "$env:name" interpolates just fine even without subexpression. – zett42 Nov 17 '22 at 18:37
  • Is "Token123456789" the literal value you are testing with or are you actually passing a different value that indeed contains special characters which might need encoding? – zett42 Nov 17 '22 at 18:39
  • @iRon This unfortunately did not work – Tyler Nov 17 '22 at 19:03
  • @SantiagoSquarzon I defined it using the setx command in command prompt I.e. setx token Token123456789 – Tyler Nov 17 '22 at 19:03
  • @zett42 The token only contains alphanumeric characters and underscores – Tyler Nov 17 '22 at 19:04
  • The token is constantly changing so, why do you define it in cmd? Why not define it from powershell each time you request it? it seems you're looking to make things harder than it should – Santiago Squarzon Nov 17 '22 at 19:09
  • @SantiagoSquarzon The token is defined on the creation of a docker container. It is not constantly changing. It needs to be accessed from a script within the container after creation. – Tyler Nov 17 '22 at 19:27
  • 1
    Ah my bad, assumed this was a Graph token for some reason. Ok, in that case, instead of using cmd, do it from powershell itself using `[System.Environment]::SetEnvironmentVariable('token', $tokenGoesHere, [System.EnvironmentVariableTarget]::User)` then after restarting your process or starting a new powershell session `$env:token` should be available to you – Santiago Squarzon Nov 17 '22 at 19:39
  • @SantiagoSquarzon I don't understand why that worked, but it did. Thanks! Odd that setx would somehow be different from SetEnvironmentVariable – Tyler Nov 17 '22 at 19:56
  • my guess, doing it from cmd appends an extra space, or line break or some encoding issue. hard to tell without comparing the hex from both sides. but yeah, I guess you learnt now that defaulting to cmd for something that can be done from pwsh is not a good idea, and yes 100% of the things you can do in cmd can be done in pwsh and much much more – Santiago Squarzon Nov 17 '22 at 19:58
  • @SantiagoSquarzon I checked for spaces, but couldn't find any. How could I compare the hex? And yes... I have been learning how much better powershell is overall. There's just things like setx in cmd which was shorter to type, so I didn't think anything of it. – Tyler Nov 17 '22 at 20:15
  • you could try `$env:varFromPwsh | Format-Hex` and compare it with `$env:varFromSetx | Format-Hex` , maybe that can give you a hint – Santiago Squarzon Nov 17 '22 at 20:19
  • 2
    @SantiagoSquarzon So the was somehow an invisible dot before the token when I used setx. This was caused by a lack of quotes for some reason. `setx token Token123456789` appended a dot. `setx token "Token123456789"` worked without appending the dot. – Tyler Nov 17 '22 at 20:42
  • 1
    @SantiagoSquarzon Thanks for you help through this! Wouldn't have found that without you. – Tyler Nov 17 '22 at 20:43

0 Answers0