以下代码在当前计算机上执行脚本时有效 (目前,脚本是一个显示传递参数的简单消息框)
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
如果有人能指出我的代码中的任何错误,我们将不胜感激!
答案 0 :(得分:0)
我通过将完整路径放到powershell.exe来解决我的问题,而不是假设控制台知道在哪里寻找...
inParams("CommandLine") = "cmd.exe /c C:\Windows\System32\WindowsPowerShell\v2.0\powershell.exe """ & PathBashFile & """ " & params