簡體   English   中英

使用“ Invoke-Command”在遠程計算機上運行本地文件

[英]Using “Invoke-Command” to run a local file on a remote machine

我正在嘗試使用Powershell運行位於計算機A上的蝙蝠腳本並在計算機B上執行它

計算機A是調用“調用命令”功能的計算機

我正在嘗試通過

Invoke-Command -ComputerName ComputerB -ScriptBlock{& "\\ComputerA\filepath\batch.bat"}  -Credential $Cred

當我運行這個我收到錯誤

The term '\\ComputerA\filepath\batch.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the  name, or if a path was included, verify that the path is correct and try again.

我也嘗試通過在計算機B上調用cmd並將其作為參數傳遞給bat腳本來運行它,但我沒有收到任何錯誤,但我可以告訴腳本未運行。我為此使用的命令是

Invoke-Command -ComputerName ComputerB -ScriptBlock{& "C:\Windows\System32\cmd.exe"} -ArgumentList "\\ComputerA\filepath\batch.bat" -Credential $Cred

當我進入計算機B並通過cmd手動調用腳本時,它可以工作

我確認了Invoke-Command的工作原理,因為我能夠運行計算機B本地的程序

我試圖不必在計算機B上放置任何文件,例如psexec或bat腳本本身,這就是為什么我要這樣嘗試

謝謝

當您遠程訪問計算機(在本例中為ComputerB)並嘗試訪問ComputerA上的內容時,基本上是在嘗試訪問默認情況下不允許的網絡資源。 使用Invoke-Command中的-EnableNetworkAccess參數明確允許相同

這聽起來像是“雙跳” /“第二跳”問題。 您的憑據在遠程會話中不可用(用於訪問網絡共享)。 解決方案:

  • 使用CredSSP身份驗證
  • 在本地運行。
    • 將文件復制到遠程計算機
    • 使用Invoke-Command -Scriptblock { & "c:\\temp\\batch.bat" }
    • 如有必要,刪除遠程計算機上的文件
  • 使用psexec.exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM