在远程计算机上运行脚本并将凭据传递给远程脚本

时间:2020-02-04 20:03:23

标签: powershell-4.0 powershell-remoting

要求:必须在凭据传递到远程计算机脚本的情况下从远程计算机上的本地计算机运行。

$server = 'SRV1'
$credential = Get-Credential
$credentials = Get-Credential
Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock {
    C:\foo.ps1 $credentials
 }

foo.ps1需要使用需要$ credentials的函数

在foo.ps1内部

param($credentials)

1 个答案:

答案 0 :(得分:0)

如果不将其作为参数传递,则不能在另一台计算机上的scriptBlock中使用Variable $ credentials: Pass arguments to a scriptblock in powershell