0

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?

2 Answers 2

0

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.

1
  • I tried this in PowerShell ISE and it worked! But when running 'Get-ADUser -Filter * -Server dc02.domain.local -Credential $MyCred' I am still prompted for my credentials with that pop-up. My guess is that this is happening because I am querying a remote server that is different from the one I created a new PSSession for. Do you know of a workaround?
    – challer
    May 9, 2014 at 12:59
0

Try this, it reverses the SecureString to clear text, locally, and remotelly:

$mycred=Get-Credential 
$remote = ($env:LOGONSERVER).Replace('\\','')
echo $mycred.Username
echo ([System.Runtime.InteropServices.marshal]::PtrToStringAuto(
    [System.Runtime.InteropServices.marshal]::SecureStringToBSTR(($mycred.Password))))

$Session = New-PSSession -ComputerName $remote -Credential $mycred
Invoke-Command -Session $Session -ScriptBlock { 
    $mycredRemote = $args[0] 
    echo $mycredRemote.Username
    echo ([System.Runtime.InteropServices.marshal]::PtrToStringAuto(
    [System.Runtime.InteropServices.marshal]::SecureStringToBSTR(($mycredRemote.Password))))
} -ArgumentList $mycred
Enter-PSSession -Session $Session

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .