Windows – Can an encrypted credential be accessed from within a PSSession

credentialspowershellwindows

My local machine exists on a domain in a separate forest from the forest I administer. As a result, I use PSSessions extensively. While I have been able to encrypt my credentials and save them in my profile on my local machine, I have tried to encrypt my credentials and save them in my profile on the remote machine and use them once I am inside of a PowerShell Session. Below is an example:

PS C:\WINDOWS\system32> etsn -computer 192.168.1.2 -cr $mycred

[192.168.1.2]: PS C:\Users\challer\Documents> get-aduser -server dc01.domain.local -filter * -cr $mycred

After entering this command, I receive the following pop-up:

"Warning: A script or application on the remote computer 192.168.1.2 is requesting your credentials. Enter your credentials only if you trust the remote computer and the application or script that is requesting them."

Can a credential be encrypted, saved, stored on a remote server and accessed from within a PSSession? Or can an encrypted credential that is stored locally be passed through to a PSSession?

Best Answer

The problem is the fact, that remote session can't read your local variable. You can easily define variables before you Enter-PSSession and use them later in remote session:

$Session = New-PSSession -ComputerName dc01.domain.local -Credential $mycred
Invoke-Command -Session $Session -ScriptBlock { 
    $mycredRemote = $args[0] 
} -ArgumentList $mycred
Enter-PSSession -Session $Session

From this point, $mycredRemote contains your credentials.