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!