2017-02-15 94 views
0

是否可以在PowerShell中使用Invoke-Command在遠程機器上運行腳本?如何在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如何寫?提前致謝!

+0

剛剛使用'調用 - 命令 - 計算機名稱「服務器」-ScriptBlock {&「C:\ Users \ user \ Desktop \ 1.ps1「}'它對我來說工作得很好 - 如果你手動運行該腳本,它會工作嗎? – ConnorLSW

回答

0

代替此:

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 
+0

在這種情況下,文件路徑是本地文件,而不是遠程計算機上的路徑。我對嗎? –

0

,以避免與在本地機器上/遠程機器文件路徑混淆;我總是從smb-shares運行東西;您可以輸入UNC作爲文件路徑...例如: -FilePath \ server \ Netscripts \ script.ps1

相關問題