Invoke-RestMethod Script

the script 1 gets authentication token, the script 2 use token how can I simply insert the token token : QSDK 352d2c53db68e9b04e42298ebd27a9d2 to $headers.Add(“Authtoken”, “QSDK 352d2c53db68e9b04e42298ebd27a9d2”)

SCRIPT 1

$headers = @{}
$headers.Add("Accept","application/json")
$headers.Add("Content-Type","application/json")

$username = "apUser"
$password = "apPassword"
$encodedPassword = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($password))

$body = @{username=$username;password=$password;}

$response = Invoke-RestMethod 'https://localhost/webconsole/api/Login' -Method 'POST' -Headers $headers -Body $($body | ConvertTo-Json)

$response | Select-Object -Property token | Format-List

token : QSDK 352d2c53db68e9b04e42298ebd27a9d2

SCRIPT 2

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authtoken", "QSDK 352d2c53db68e9b04e42298ebd27a9d2")
$headers.Add("Accept", "application/json")

$response = Invoke-RestMethod 'https://localhost/webconsole/api/DDBInformation/8' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

Hi, welcome to the forum :wave:

You could put both scripts in a module. All variables in a module are in the same scope, so you could use a variable set in script 1, in script 2.

You could write the token to a file and use Get-Content in script 2 to get the token.

You could use a Global variable.

HI Matt thank you for replay, do you have example script

What’s wrong with your example? All he said was put it in a module and benefit from shared variables

1 Like

Well I need help building the module, first I need to obtain token and use that token

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authtoken", "QSDK 352d2c53db68e9b04e42298ebd27a9d2")
$headers.Add("Accept", "application/json")

$response = Invoke-RestMethod 'https://localhost/webconsole/api/DDBInformation/8' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

Making a module can be as simple as saving as a psm1 file.

Check out this link

2 Likes

@maxjamakovic did you manage to figure this out from my suggestions and Doug’s links or do you need further assistance with this?

I need assistance if possible

Is there a reason it’s two scripts? It could be combined like this:

$headers = @{
    Accept = 'application/json'
    'Content-Type' = 'application/json'
}

$username = "apUser"
$password = "apPassword"
# Commenting out the next line because $encodedPassword is not used.
#$encodedPassword = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($password))

$body = @{username=$username;password=$password;}

$response = Invoke-RestMethod 'https://localhost/webconsole/api/Login' -Method 'POST' -Headers $headers -Body $($body | ConvertTo-Json)

$token = $response | Select-Object -ExpandProperty token

$headers = @{
    Authtoken = $token
    Accept = 'application/json'
}

$response = Invoke-RestMethod 'https://localhost/webconsole/api/DDBInformation/8' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

I’ve made the assumption that the scripts are currently working for you and they just needed combining. If that’s not the case, please post any errors (formatted as code using the </> button).

Matt you’re awesome!!! Thank you so so much.

I want to buy you coffee send me your paypal or Venmo

You’re very welcome, I’m happy to have helped. No coffee necessary, but thank you for the offer.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.