Hello,
The best way would be to store the encrypted credentials in the file, to be used to run the code.
To create the encrypted string password you can use this:
Create encrypted string
New-StringEncryption -StringToEncrypt 'MySecurePassword123'
Output string
kvKHaoJytKOhWVSLRwpaAb4jBDz0i/s4yUdlhFKpNi0=
Once you have that secure string, you can add it into your script:
Define Credentials
[string]$userName = 'admin'
[SecureString]$securePwd = ConvertTo-SecureString -String (New-StringDecryption -EncryptedString 'kvKHaoJytKOhWVSLRwpaAb4jBDz0i/s4yUdlhFKpNi0=') -AsPlainText -Force
Create the credential object
[pscredential]$credentialObject = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePwd
Continue your Code here
-------------------------------------------------------------------------------------------------------------------------------------------
--If the reply is helpful, please Upvote and Accept as answer--