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,
- UserName,
- Password
Module Module1 Sub Main() 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”) scope.Connect() Dim opt As New System.Management.ObjectGetOptions() Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters(“Create”) ‘ Execute the method and obtain the return values. End Function End Module Some links I found useful
Return code s |