I use System.Management in .NET 2.0 to connect, pass custom credentials to a remote machine and query with WMI frequently. This is straight forward and there are many examples showing the syntax how to do this. However, while working with remote IIS7 installs, I wanted to execute a command on a remote machine while passing custom credentials. I couldn’t find an example that provided this functionality, hopefully this will save someone time searching for the syntax. This sample starts an instance of ‘calc.exe’ on a remote machine.
The code accepts four parameters
- Command to run,
- Machine to connect to,
Function RunCommand(ByVal strCommand As String, ByVal strMachineName As String, ByVal strUserName As String,
Dim path As New System.Management.ManagementPath(“\” & strMachineName & “rootcimv2:Win32_Process”)
Dim opt As New System.Management.ObjectGetOptions()
Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters(“Create”)
‘ Execute the method and obtain the return values.
Some links I found useful
Return code s
5 thoughts on “WMI – start a process on remote machine and passing custom credentials.”
I exactly used your code it runs fine but nothing happens on the remote machine it occured on one machine but on the other machine it gives me the error like access denied.
What build of Windows Server 2008 are you running? Have you verified the Windows Firewall is temporarily turned off?
It starts the application but its not visible on the remote system. do you know how to make it visible or to make part of a opened session?
Processes launched by WMI can NEVER be visible. They run in their own windowstation, the same as window services. It make it visible you have to have a System Tray application or similar hidden application which is launched when each desktop starts. Then you have to setup some kind of IPC/RPC between the system tray app and your server.
The processer used for this execution is the remote processer(where exe is located) or the local processer(where Package is running)