0
我有一个脚本,它使用Enter-PSSession自动将用户登录到远程PowerShell会话中。我创建了新对象System.Management.Automation.PSCredential的-Credential。如果它不在ScriptBlock中,这很好用。当我把它放在脚本块中时,它会在我尝试连接时提示我输入凭据,但我不知道为什么。带有-Credential的Enter-PSSession在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
您无法将'PSCredential'对象传递到命令行中。命令行参数必须是文本,而不是.NET对象。 –
如果您在-runas参数中使用John的凭据创建了自定义委派会话,这会不会更容易? – mjolinor
@ mjolinor,我会怎么做? –