user3331975 user3331975 - 5 months ago 36
PowerShell Question

using secure password with multiple users without prompt

I am trying to have my password secured and stored in a file so that I don't need to enter each time when I run the script.

First step, I ran the following, entered the password which got stored into E:\cred.txt file. The txt file now contains an encrypted password.

(Get-Credential).Password | ConvertFrom-SecureString | Out-File "E:\cred.txt"

Secondly, I ran the below Script:

$File = "E:\cred.txt"
$User = ""

#### I have two different user accounts, one for admin and other for operator,
#### however both user accounts use same password.

$adminuser = $User
$operator = $User -replace ""

#### I would need to read $File to get only the password

$pass = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList (Get-Content $File | ConvertTo-SecureString)



UserName Password
-------- --------

From the output, it seems New-Object refers to both UserName and Password. And when I try to connect to systems, it fails with Authentication error. Since I already have two different usernames hard coded within the script, how should I get only the password stored in $pass? or is it possible to include all usernames ($User, $adminuser, $operator) into the cred.txt file?


Try this:

#saving credentials
Get-Credential | Export-CliXml -Path c:\credential.xml

#importing credentials to a variable
$Credential = Import-CliXml -Path c:\credential.xml

Or this:

#you could then write it to a file or, i say its a better approach to a registry key
$SecurePassword = ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force | ConvertFrom-SecureString

#now you are taking it back as a secure string
$RegistrySecureString = $SecurePassword  | ConvertTo-SecureString

#you can aslo see the password
$UserName = "NULL"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $RegistrySecureString
$Password = $Credentials.GetNetworkCredential().Password