我在同一個域上有兩臺計算機。winrm 在兩臺機器上運行。Test-WSMan 正在遠程機器上運行。我可以執行invoke-command -scriptblock {dir c:\automatedtests} -computername RemoteComputername并回傳預期結果:
Directory: C:\automatedtests
Mode LastWriteTime Length Name PSComputerName
d----- 1/11/2022 5:45 PM ErrorLog.txt us14-pacci04
a----- 1/13/2022 12:16 PM 18 test.ps1 us14-pacci04
. 但是,如果我嘗試在同一臺機器上執行腳本 ( invoke-command -computername 'RemoteComputerName' -filepath 'C:\AutomatedTests\test.ps1'),我會收到錯誤訊息:
invoke-command : Cannot find path 'C:\AutomatedTests\test.ps1' because it does not exist.
At line:1 char:1
invoke-command -session $s -filepath {C:\AutomatedTests\test.ps1}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (C:\AutomatedTests\test.ps1:String) [Invoke-Command], ItemNotFoundExcepion
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
這幾天我一直在谷歌上搜索,但沒有解決方案。我錯過了什么?我想念的東西一定很簡單。
編輯 1/18/22 下午 1:18 PT:好的 - 我現在明白 -filepath 是指本地檔案。那么在遠程計算機上運行腳本(腳本存盤在那里)的唯一方法是使用共享?
uj5u.com熱心網友回復:
您可以使用呼叫運算子&在遠程主機上呼叫腳本:
Invoke-Command -ScriptBlock { & 'c:\automatedtests\test.ps1'} -ComputerName RemoteComputer
或點源運營商.:
Invoke-Command -ScriptBlock { . 'c:\automatedtests\test.ps1'} -ComputerName RemoteComputer
uj5u.com熱心網友回復:
您可以使用$sess = New-PSSession... 連接到遠程計算機,然后Copy-Item -ToSession $sess ...將檔案復制到遠程計算機上的檔案夾,然后在遠程計算機Invoke-Command -Session $sess ...上運行它。
(這可以避免在需要連接到檔案共享的遠程機器上運行命令的雙跳問題)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/416703.html
標籤:
上一篇:轉換Json表
