Hardcode password into powershells "New-PSSession" Hardcode password into powershells "New-PSSession" powershell powershell

Hardcode password into powershells "New-PSSession"


Yep - you can totally do this as long as you are comfortable with the security implications (a PW in a file somewhere)...

Here's an example:

 $pw = convertto-securestring -AsPlainText -Force -String <insert pw here> $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Domain\User",$pw $session = new-pssession -computername <computer> -credential $cred


I've used this approach in similar situations. It's certainly not perfect, but it makes me much less nervous than hardcoding a password in a file. I read and store the password during the first run, then read from the DPAPI-encrypted file afterward. I generally run scripts from a shared location on an internal network, and store the encrypted password file in a private folder on my local machine.

$user = "Domain\testautouser"$passwdFile = "$env:USERPROFILE\myscript-$user"if ((Test-Path $passwdFile) -eq $false) {  $cred = new-object system.management.automation.pscredential $user,        (read-host -assecurestring -prompt "Enter a password:")    $cred.Password | ConvertFrom-SecureString | Set-Content $passwdFile}else {  $cred = new-object system.management.automation.pscredential $user,        (Get-Content $passwdFile | ConvertTo-SecureString)}