PowerShell远程会话中的嵌套脚本块

时间:2013-11-05 15:22:34

标签: powershell powershell-remoting

我正在尝试在远程计算机上运行脚本块,但我不会被提示输入凭据,我想在脚本中传递它们。  除非我提供凭证,否则某些命令可能会运行我想要运行的命令。

例如,该命令可以起作用:

Invoke-Command -Session $session -ErrorAction Stop -ErrorVariable $Error -Scriptblock {ls }

但是除非传递-credential(目标上的Invoke-Command)

,否则这不会工作
Invoke-Command -Session $session -ErrorAction Stop -ErrorVariable $Error -Scriptblock {Invoke-Command -computername $Env:COMPUTERNAME -Credential $Cred  -ScriptBlock {ls} }

我想要实现的方式导致访问被拒绝问题(启动进程)

Invoke-Command -Session $session -ErrorAction Stop -ErrorVariable $Error -Scriptblock {[System.Diagnostics.Process]::Start("C:\Myprocess.exe", $localArgs, "UserName", $credential.Password, "MyDomain")}

1 个答案:

答案 0 :(得分:0)

   Invoke-Command -Session $session -ErrorAction Stop -ErrorVariable $setError -Scriptblock {$Domain = "domain";$PassSec = ConvertTo-SecureString $( "password") -AsPlainText -Force ; $Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $($domain + "\" + "userName"),$passSec; Invoke-Command -computername $Env:COMPUTERNAME -Credential $Cred  -ScriptBlock {C:\Myprocess.exe } }