0

我有一个使用 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
4

1 回答 1

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
于 2014-04-03T01:03:27.007 回答