繁体   English   中英

如何在powershell中使用invoke-command,在远程机器上运行脚本

[英]How to use invoke-command in powershell, to run a script on remote machine

是否可以在 PowerShell 中使用Invoke-Command在远程计算机上运行脚本?

我努力了 :

Invoke-Command -ComputerName $MyPC -Credential $mycreds -ScriptBlock {
    & "C:\Users\MyPC\Desktop\scripts\Script1.ps1"
}

返回

script1.ps1 is not recognized as the name of a cmdlet

这里的场景是,我在远程文件夹上有一些脚本,我需要使用Invoke-Command或其他一些方式在远程机器上运行脚本。

另外,如果我想为script1.ps1传递一些参数怎么写? 提前致谢!

而不是这个:

Invoke-Command -ComputerName $MyPC -Credential $mycreds -ScriptBlock {& "C:\Users\MyPC\Desktop\scripts\Script1.ps1"}

尝试这个:

Invoke-Command -ComputerName $MyPC -Credential $mycreds -FilePath C:\Users\MyPC\Desktop\scripts\Script1.ps1

避免与本地机器/远程机器上的文件路径混淆; 我总是从 smb-shares 运行东西; 您可以输入 UNC 作为文件路径...例如:-FilePath \server\Netscripts\script.ps1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM