David De Jaeger
David De Jaeger

Reputation: 11

Connecting to WMI Remotely with C# to a non-domain PC not working

I use the Microsoft.Management.Infrastructure namespace to connect to remote computers to get WMI information and it works. But when I try to connect to a non-domain PC it does not work. Can anyone pinpoint what I am doing wrong. Here is the code:

string computer = "Computer_B";
string domain = "WORKGROUP"; 
string username = ".\\LocalAdminUserName";
string plaintextpassword; 

Console.WriteLine("Enter password:");
plaintextpassword = Console.ReadLine(); 

SecureString securepassword = new SecureString();
foreach (char c in plaintextpassword)
{
    securepassword.AppendChar(c);
} 

CimCredential Credentials = new 
CimCredential(PasswordAuthenticationMechanism.Default, domain, 
username,securepassword); 

WSManSessionOptions SessionOptions = new WSManSessionOptions();
SessionOptions.AddDestinationCredentials(Credentials); 

CimSession Session = CimSession.Create(computer, SessionOptions); 

var allVolumes = Session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_LogicalDisk"); 

 // Loop through all volumes
 foreach (CimInstance oneVolume in allVolumes)
 {
       Console.Writeline(oneVolume.CimInstanceProperties["SystemName"].Value.ToString());
 } 

I am not sure what to take as paramaters for domain and username for a local computer. I have already done/tryed the following:

Any suggestions what I am doing wrong?

The error I keep getting is: WinRM cannot process the request. The following error with error code 0x8009030e occurred while using Negotiate authentication: A specified logon session does not exist. It may already have been terminated.
This can occur if the provided credentials are not valid on the target server, or if the server identity could not be verified. If you trust the server identity, add the server name to the TrustedHosts list, and then retry the request. Use winrm.cmd to view or edit the TrustedHosts list. Note that computers in the TrustedHosts list might not be authenticated. For more information about how to edit the TrustedHosts list, run the following command: winrm help config.

Upvotes: 1

Views: 1322

Answers (1)

Amit Shakya
Amit Shakya

Reputation: 1476

Try out the code below, this is working on impersonation logic.

ConnectionOptions cOption = new ConnectionOptions();
                ManagementScope scope = null;
                Boolean isLocalConnection = isLocalhost(machine);

                if (isLocalConnection)
                {
                    scope = new ManagementScope(nameSpaceRoot + "\\" + managementScope, cOption);
                }
                else
                {
                    scope = new ManagementScope("\\\\" + machine + "\\" + nameSpaceRoot + "\\" + managementScope, cOption);
                }

                if (!String.IsNullOrEmpty(ACTIVE_DIRECTORY_USERNAME) && !String.IsNullOrEmpty(ACTIVE_DIRECTORY_PASSWORD) && !isLocalConnection)
                {
                    scope.Options.Username = ACTIVE_DIRECTORY_USERNAME;
                    scope.Options.Password = ACTIVE_DIRECTORY_PASSWORD;
                }
                scope.Options.EnablePrivileges = true;
                scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
                scope.Options.Impersonation = ImpersonationLevel.Impersonate;
                scope.Connect();

Upvotes: 1

Related Questions