i'm tring to start a calculator in a remote machine from my system. and the codes i tried is this.
Module Module1
Sub Main()
Dim retValue As String
retValue = RunCommand("calc.exe", "MachineName", "MachineName\UserID", "Password")
Console.WriteLine(retValue)
End Sub
Function RunCommand(ByVal strCommand As String, ByVal strMachineName As String, ByVal strUserName As String, ByVal strPassword As String) As String
Dim options As New System.Management.ConnectionOptions
options.Username = strUserName
options.Password = strPassword
options.Impersonation = Management.ImpersonationLevel.Impersonate
options.Authentication = Management.AuthenticationLevel.PacketPrivacy
Dim path As New System.Management.ManagementPath("\\" & strMachineName & "\root\cimv2:Win32_Process")
Dim scope As New System.Management.ManagementScope(path, options)
scope.Connect()
Dim opt As New System.Management.ObjectGetOptions
Dim classInstance As New System.Management.ManagementClass(scope, path, opt)
Dim inParams As System.Management.ManagementBaseObject = classInstance.GetMethodParameters("Create")
inParams("CommandLine") = strCommand
' Execute the method and obtain the return values.
Dim outParams As System.Management.ManagementBaseObject = classInstance.InvokeMethod("Create", inParams, Nothing)
Return "ReturnValue:" & outParams("returnValue") & " Process ID: {0}" & outParams("processId")
End Function
End Module
******while i tried to connect to some machines this error occurs.
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in system.management.dll
Additional information: Access is denied.
*******and to someother machines and to the local machine this error occurs.
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'UInt32' to type 'String' is not valid.
and my local machine the calculator is poping up and this above error occurs. but while connecting to remote machine, to some machine access is denied to some other the above error occurs and calc is not popping up too.
and one have anyidea what's wrong with my codes... please help me.
thanks in advance.