Store your creds in a secure file, the registry or Windows credential manager, and call from there as needed. Plenty of examples all over the web.
• Securing Credentials and Passwords
Securely Store Credentials on Disk
Quickly and securely storing your credentials – PowerShell
Save Encrypted Passwords to Registry for PowerShell
Manipulate credentials in the Windows 8/2012 PasswordVault using
Powershell PasswordVault.psm1
Powershell: How to encrypt and store credentials securely for use
with automation scripts
Sample from the last link:
<# Set and encrypt credentials to file using default method #>
$credential = Get-Credential
$credential.Password |
ConvertFrom-SecureString |
Set-Content c:scriptsencrypted_password1.txt
<#
Set some variables
...
#>
$emailusername = "myemail"
$encrypted = Get-Content c:scriptsencrypted_password.txt |
ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($emailusername, $encrypted)
if($something = $somethingElse)
{
<#
Do some stuff
...
#>
$EmailFrom = "myemail@gmail.com"
$EmailTo = "myemail+alerts@gmail.com"
$Subject = "I did some stuff!"
$Body = "This is a notification from Powershell."
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = $credential;
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}
This is a very common thing to do to limit the need to enter creds manually/multiple times and very well documented, with modules in the MS powershellgallery.com to use for this type of thing.
Find-Module -Name '*credential*' |
Format-Table -AutoSize
<#
# Results
Version Name Repository Description
------- ---- ---------- -----------
2.0 CredentialManager PSGallery Provides access to credentials ...
1.0.4 WindowsCredential PSGallery Management module for Windows Credential Store.
...
1.1.0 CredentialSpec PSGallery Tools to create and find Credential Spe ...
4.5 BetterCredentials PSGallery A (compatible) major upgrade for Get-Credential, ...
1.0.11 pscredentialmanager PSGallery This module allows management and autom ...
...
1.0.0 CredentialLocker PSGallery CredentialLocker is a module that provide ...
1.1 CredentialsManager PSGallery The module Credentials Manager provides y ...
1.2.2.20190715 SimplyCredential PSGallery Simply Module for windows credentials.
1.0.2 CredentialManagement PSGallery Manage Credentials stored in the Windows Credential Manager
...
1.1.0 PSCredentialTools PSGallery PSCredentialTools provides various methods for ...
1.0.0.0 SelectCredential PSGallery A module for selecting the credential stor ...
2.1 SecuredCredential PSGallery SecuredCredential Routines for modules suppor ....
1.0.477 PSCredentialStore PSGallery A simple credential manager to store and ...
...
3.0 CredentialUtility PSGallery This is a credential manager tool which co ...
1.3 MiCredentialModule PSGallery Saves/Retrieves credentials to/from a file (w ...
1.3 vaultcredential PSGallery Manages credentials in the credential vault
0.0.1 SecureCredentials PSGallery This module allow to secure store encrypted ...
#>