Powershell prompts

Copper Contributor
Hi,
Im writing a powershell automation script where i need to pass a password to a powershell prompt from another script file.
I wrote 2 script files. When i run the file number 1 (automation script) it will open a powershell window and ask to enter the password. Script 2 contain the password. So i want to pass that password to the powershell prompt so i don't have to type the password manually. Any answers will be appreciated.
3 Replies

@zuko98 you could also use an encrypted secure password or encrypted credential file - attached is an example of generating a secure password file with and AES keyfile and importing the settings using an xml file

 

Cred file could be used as well

$Credential = Get-Credential
$Credential | Export-CliXml -Path "$PSScriptRoot\TestCred.xml"

 

Using encrypted password string

#Make encrypted string
#$key = "1234567891234567"
$plainPassword = "PasswordHere"
$securePassword = ConvertTo-SecureString $plainPassword -AsPlainText -Force
$secureKey = ConvertTo-SecureString $Key -AsPlainText -Force
$encryptedKey = ConvertFrom-SecureString $SecureKey -Key(1..16)
$encryptedPassword = ConvertFrom-SecureString -SecureString $SecurePassword -SecureKey $secureKey
$encryptedKey | Out-File "C:\temp\test\securekey-enckey.txt" 
$encryptedPassword | Out-File "C:\temp\test\securekey-encpass.txt" 
Write-Host "Key: $Key" 
Write-Host "Text Password: $plainPassword" 
Write-Host "Encrypted Password: $encryptedPassword" 
Write-Host "Encrypted Key: $encryptedKey"

you can test the encrypted string using the following

#Use
$encryptedKeyFromFile = Get-Content "C:\temp\test\securekey-enckey.txt"
$encryptedPasswordFromFile = Get-Content "C:\temp\test\securekey-encpass.txt"
$secureDecryptedKey = ConvertTo-SecureString $encryptedKeyFromFile -Key(1..16)
$secureDecryptedPassword = ConvertTo-SecureString $encryptedPasswordFromFile -SecureKey $secureDecryptedKey

$BSTR1 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedPassword)
$textPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR1)
 
$BSTR2 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedKey)
$key = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR2)
 
Write-Host "Key: $key"
Write-Host "Text Password: $textPassword"
Write-Host "Encrypted Password: $encryptedPasswordFromFile"
Write-Host "Encrypted Key: $encryptedKeyFromFile"

$User = "DEMOLAB\account"
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $secureDecryptedPassword
$MyCredential

 

 

Did you manage to get the script working?