Disclaimer: I am a Powershell noob so forgive any lack of clarity or terrible scripting practices
TL;DR
Is there a way to run Invoke-RestMethod -Uri $uri -Authentication Basic -Credentials $cred
once and not have to use the -Credentials
or -Authentication
flags in subsequent Invoke-RestMethod
commands to the same API?
Goal
I am trying to figure out how to reduce the number of sign-ins per script run in Powershell. The API seems to not like that I'm signing in so much to do GET HTTP requests because it has informed me that my password expired on my account, despite the fact that password expiration is not applied. If the fix also speeds up the script, that's a bonus, but I guess that depends more on the API response time.
About the platform
Due to privacy concerns, I don't want to reveal too much about the platform unless I have to, but here's the basic structure:
Each client/business is considered an "enterprise" and each enterprise has at least 1 "site." Most enterprises only have 1 site with anywhere between 6 and 200 users at each site, but some enterprises have up to 7 sites. This means that some enterprises have close to 600 users.
Unfortunately, the platform does not have any documentation on the API I'm using (I've already asked their support for it with no luck). The information we need is also not available to export via the web portal GUI without getting some screen scraping tool. We preferred going for a scripting approach using the platforms API so I'm tasked with writing that script.
Due to the lack of assistance from platform support and very little documentation, I am figuring all the correct URLs by logging into our admin portal on a test site, changing settings, and examining .har files to see what URLs the GET requests are going to in order to get the information I need. It's tedious, but it works and will save us time in the long run for a platform migration.
Also, I discovered that platform utilizes REST API and Basic Authentication.
About the script
I am using Invoke-RestMethod -Uri $uri -Authentication Basic -Credentials $cred
in a foreach
loop to get the info to then save into a custom object for exporting. Because of the way the current platform is structured, I have to do 5 HTTP GET requests per user to get the information we need, which takes about 4 seconds per user.
When using the web browser, I only need to sign in once and it remembers my credentials, so why not the same with Invoke-RestMethod
? I've searched around and couldn't find anything helpful.
If there's a way to make every subsequent Invoke-RestMethod
command to not require the -Credentials
flag to reduce the number of sign-ins per script run, that would be ideal. If it happens to speed up the script as well, that is even better.