带有-Credential的Enter-PSSession在ScriptBlock内部时仍然会提示输入凭据

时间:2014-04-02 04:12:15

标签: powershell scriptblock

我有一个脚本,可以使用Enter-PSSession自动将用户登录到远程PowerShell会话中。我使用New-Object System.Management.Automation.PSCredential创建-Credential。如果它不在ScriptBlock中,这很有用。当我把它放在脚本块中时,它会在尝试连接时提示我输入凭据,我不知道为什么。

我如何制作它以便在ScriptBlock内部工作?

$userADPW = "password"

$userAD = "john"

$servip = "Server2K81.homelab.com"

$PassSec = ConvertTo-SecureString $userADPW -AsPlainText -Force

$credentialPS = New-Object System.Management.Automation.PSCredential ($userAD,$PassSec)

Start-Job -ScriptBlock {param($psip,$CredentialPS2) Start-Process powershell -Argumentlist '-noexit',"Enter-PSSession -ComputerName $psip -Credential $CredentialPS2" -passthru -Wait} -ArgumentList $servip,$credentialPS

1 个答案:

答案 0 :(得分:2)

确定您理解:此解决方法将(编码)密码保留在" startinfo"对于该过程,直到它关闭 - 所以该机器上的任何东西都可以读取密码(并可能解密)。

$userADPW = "password"

$userAD = "john"

$servip = "Server2K81.homelab.com"

$PassSec = ConvertTo-SecureString $userADPW -AsPlainText -Force

$credentialPS = New-Object System.Management.Automation.PSCredential ($userAD,$PassSec)

Start-Job -ScriptBlock { 
   param($psip,$CredentialPS2) 
   Start-Process powershell -Argumentlist '-noexit',"&{
   `$Credential = New-Object System.Management.Automation.PSCredential '$($CredentialPS2.UserName)', (ConvertTo-SecureString '$(ConvertFrom-SecureString $CredentialPS2.password)')
   Enter-PSSession -ComputerName '$psip' -Credential `$Credential
  }" -passthru -Wait
} -ArgumentList $servip,$credentialPS