Powershell - Stocker un mot de passe dans un fichier
Pour récupérer le mot de passe et le stocker dans un fichier :
[string]$DestFile="$env:USERPROFILE\Documents\password.secret"
read-host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File $DestFile
Pour utiliser le mot de passe en clair :
[string]$EncryptedPasswordFile="$env:USERPROFILE\Documents\password.secret"
$PasswordSecureString=ConvertTo-Securestring (Get-content $EncryptedPasswordFile)
$PlainTextPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PasswordSecureString))
Pour utiliser le mot de passe dans un objet credential :
[string]$EncryptedPasswordFile="$env:USERPROFILE\Documents\password.secret"
[string]$Username="MyUserName"
$PasswordSecureString=ConvertTo-Securestring (Get-content $EncryptedPasswordFile)
$credentials=New-object System.Management.Automation.PSCredential ($Username, $PasswordSecureString)