I'm trying to streamline how I use credentials in my PowerShell script. I have a .ps1 file that contains several functions, many of which require credentials to execute. I want to load these credentials only once when the script is run instead of prompting every single time I call a function. For example, I currently have a function called `Get-SomethingSecure`, which takes credentials as a parameter. I'm looking for a way to preload these credentials at the beginning of the script using `Get-Credential`, so that when I call my functions later, they automatically use the loaded credentials. Is there a way to achieve this? Thank you for any help!
1 Answer
You can definitely store credentials so they don't repeatedly prompt. However, it really depends on your use case. In many scenarios, you should ideally spawn the process using those credentials, or consider using certificates or API keys for security. Be careful with your approach.
I totally get that! For tools I've built that require service accounts, users need to provide creds for creating API tokens. But I'm realizing that this might not be the safest approach. I’m hoping to run it through TeamCity as a task that handles secrets instead.