1
在當前機器 上執行腳本下面的代碼適用於執行腳本(目前,該腳本是一個簡單的消息框顯示傳入的參數)遠程服務器
Arguments:
UserName = Nothing
Password = Nothing
RemoteMachineName = "CurrentMachineName"
PathBashFile = "Path/To/My/Local/Script.ps1"
Params = "parameter1"
然而,當我想在遠程計算機上運行本地腳本,腳本爲從不執行。該代碼不會拋出任何異常。
Arguments:
UserName = "MyUsername"
Password = "MyPassword"
RemoteMachineName = "RemoteMachineName"
PathBashFile = "Path/To/My/Local/Script.ps1"
Params = "parameter1"
所有我的結果是:
outParams("processId") = Nothing
outParams("returnValue") = 8
這是怎麼回事?爲什麼我的腳本沒有按預期在遠程機器上運行? (沒有消息框彈出無論是機器,我已經嘗試過其他的cmdlet,但沒有以往任何時候都工作)。
下面是代碼:
Try
connOptions = New ConnectionOptions()
connOptions.Username = UserName
connOptions.Password = Password
connOptions.Impersonation = ImpersonationLevel.Impersonate
connOptions.Authentication = Management.AuthenticationLevel.PacketPrivacy
managementPath = New ManagementPath("\\" & RemoteMachineName & "\root\cimv2:Win32_Process")
Scope = New ManagementScope(managementPath, connOptions)
Scope.Connect()
objectGetOptions = New ObjectGetOptions()
processClass = New ManagementClass(Scope, New ManagementPath("root\cimv2:Win32_Process"), objectGetOptions)
inParams = processClass.GetMethodParameters("Create")
inParams("CommandLine") = "cmd.exe /c powershell """ & PathBashFile & """ " & params
inParams("CurrentDirectory") = workingDirectoryPath
outParams = processClass.InvokeMethod("Create", inParams, Nothing)
MsgBox(outParams("processId") & " " & outParams("returnValue"))
Catch ex As Exception
Throw New Exception("[ExecuteRemoteBashFile] " & ex.Message)
End Try
如果有人能在我的代碼,指出任何錯誤,它會非常感謝!
你確定你需要PRIVS,和用戶權限(如執行批處理腳本「或類似)在遠程機器上?(我不確定是否有這些會導致在這種情況下拋出異常,說實話...我會這樣想,但我想我應該建議它以防萬一...) – Sepster
那麼,如果我在另一臺服務器本地運行腳本,它可以很好地工作 – Msonic
爲什麼不使用WS管理(考慮到您至少使用Powershell v2) ?你會避免與W打交道MI直接。檢查http://technet.microsoft.com/en-us/magazine/ff700227.aspx –