1

The following code works when the script is executed on the current machine (Currently, the script is a simple messagebox displaying the argument passed)

Arguments:

UserName = Nothing
Password = Nothing
RemoteMachineName = "CurrentMachineName"
PathBashFile = "Path/To/My/Local/Script.ps1"
Params = "parameter1"

However, when I want to run a local script on a remote computer, the script is never actually executed. The code doesn't throw any exception.

Arguments:

UserName = "MyUsername"
Password = "MyPassword"
RemoteMachineName = "RemoteMachineName"
PathBashFile = "Path/To/My/Local/Script.ps1"
Params = "parameter1"

All I have as a result is :

outParams("processId") = Nothing
outParams("returnValue") = 8

What's going on? Why isn't my script running as expected on the remote machine? (No message box pops up on either machine. I have tried other cmdlets, but none ever work)

Here is the code:

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

If someone could point out any mistakes in my code, it would be greatly appreciated!

4

1 回答 1

0

我通过将完整路径放置到 powershell.exe 来解决我的问题,而不是假设控制台知道在哪里寻找它......

inParams("CommandLine") = "cmd.exe /c C:\Windows\System32\WindowsPowerShell\v2.0\powershell.exe """ & PathBashFile & """ " & params
于 2012-11-07T19:45:23.437 回答