- Access exclusive content
- Connect with peers
- Share your expertise
- Find support resources
06-14-2021 02:08 PM
Hello Community,
I am looking to get help with using concurrent sessions feature of the the Rest API in Powershell 7 . I am able to produce multiple authentication tokens by setting the concurrentSessions parameter to True but I get an error when I pass these auth tokens to account search API URL using the ForEach-Object -Parallel feature in PS7. Here is my PS code:
1..2 | ForEach-Object -Parallel {
$lbody = "{`n`"concurrentSessions`": `"true`"`n}"
$LoginrequestTokens = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Windows/Logon -UseDefaultCredentials -Method Post -Body $lbody
Write-Host $LoginrequestTokens
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", $LoginrequestTokens)
$response = Invoke-RestMethod 'https://<hostname>/PasswordVault/api/Accounts?search=<keyword>&searchType=contains&limit=1' -Method 'GET' -Headers $headers
$id=$response.value.id
WRITE-HOST $id
$Logoff = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Logoff -Method POST -Headers $headers
Write-Host $Logoff
} -ThrottleLimit 2
Here is the error:
YWU5NTkwN2UtMTY4OC00ZDlmLTgyZTAtZThkYzljNTM0ZTBhO0ZBMDdBODFCMEM0N0QwMEI7MDAwMDAwMDIxMzA4QUEwNjUyNjZBOUZFREUwMjhCMDU4NTMyM0I0NDc0NDc3NzE0Qzg0RTEzMUZDOEUxODIwRDExOTA3RDZFMDAwMDAwMDA7
ODU2NDA1NTQtYmU4My00NWQyLTlmOTctMzJlZGVlZjA4NzhkOzAxODgyNUNGQzBFQUQ4M0Q7MDAwMDAwMDJGMzc0NENFQjcxNTM2MTJFOEU2NDIwQTIzNTQ3OUVEOTQxQjQ2RDJCRDQ4MDZFRjMxMjU0MjU0QTJDRjM2RkI3MDAwMDAwMDA7
Invoke-RestMethod:
Line |
8 | … $response = Invoke-RestMethod 'https://<host>/PasswordVault/ap …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"ErrorCode":"ITACM040S","ErrorMessage":"User was automatically logged off from Vault (Diagnostic information: 540).\n\nLogoff from the Vault and logon again."}
@{LogoffUrl=}
Invoke-RestMethod:
Line |
8 | … $response = Invoke-RestMethod 'https://<host>/PasswordVault/ap …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"ErrorCode":"ITACM040S","ErrorMessage":"User was automatically logged off from Vault (Diagnostic information: 540).\n\nLogoff from the Vault and logon again."}
@{LogoffUrl=}
I think the issue here is usage of incorrect authentication token in subsequent API calls which is ending the session abruptly? Would appreciate some tips on how to handle multiple authentication tokens.
Thanks,
Pooja
06-14-2021 03:30 PM
Disregarding the parallel loop for now:
06-14-2021 04:00 PM
Hi Pete - See my answers below
$lbody = "{`n`"concurrentSessions`": `"true`"`n}"
$LoginrequestTokens = Invoke-RestMethod https://<host>/PasswordVault/API/auth/Windows/Logon -UseDefaultCredentials -Method Post -Body $lbody
Write-Host $LoginrequestTokens
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", $LoginrequestTokens)
$response = Invoke-RestMethod 'https://<host>/PasswordVault/api/Accounts?search=<keyword>searchType=contains&limit=1' -Method 'GET' -Headers $headers
$id=$response.value.id
WRITE-HOST $id
$Logoff = Invoke-RestMethod https://<host>/PasswordVault/API/auth/Logoff -Method POST -Headers $headers
Write-Host $Logoff
Sorry I'm not a powershell expert, not sure how to test this quickly. I can try to get back to you tomorrow on this.
Thanks for your response!
06-14-2021 04:20 PM
Actually, on your 2nd question, do you mean the authentication tokens generated by the parallel loop are valid outside of the loop? Not sure how I would generate concurrent sessions without using parallel loop (or parallel logins)?
06-14-2021 04:29 PM
well, just run the commands sequentially and set relevant variables:
$LoginrequestToken1
$Header1
$LoginrequestToken2
$Header2
Then you can test commands using both auth tokens and confirm validity.
06-15-2021 02:55 PM
Hi @Pete Maan ,
Yes, the tokens in sequential commands work
06-15-2021 04:42 PM
So I presume using `-ThrottleLimit 1` also works?
Am not sure of your need for a parallel loop to complete a simple query of accounts, but if you just authenticated once outside of the loop, it would likely suffice.
i.e.
$lbody = "{`n`"concurrentSessions`": `"true`"`n}"
$LoginrequestTokens = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Windows/Logon -UseDefaultCredentials -Method Post -Body $lbody
Write-Host $LoginrequestTokens
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", $LoginrequestTokens)
1..2 | ForEach-Object -Parallel {
$response = Invoke-RestMethod 'https://<hostname>/PasswordVault/api/Accounts?search=<keyword>&searchType=contains&limit=1' -Method 'GET' -Headers $($using:headers)
$id=$response.value.id
WRITE-HOST $id
} -ThrottleLimit 2
$Logoff = Invoke-RestMethod https://<hostname>/PasswordVault/API/auth/Logoff -Method POST -Headers $headersGiven the significant processing overhead this type of parallel loop has, and also the potential load this could introduce in terms of vault tasks depending on how your environment is configured, you should keep a close eye on performance - both of the script and the vault.
Click Accept as Solution to acknowledge that the answer to your question has been provided.
The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!
These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!
The LIVEcommunity thanks you for your participation!

