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

1

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:

  • run winrm quickconfig on the remote local computer

  • use PasswordAuthenticationMechanism.Negotiate cause I have read Kerberos only
    works for domain users and password

  • added the computer I run the code on to the TrustedHosts on the local computer with winrm config. Also tryed adding * to the TrustedHosts

  • Used for username="computer_B\LocalAdminUserName". I have also tryed with domain=""

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.

c#
wmi
remote-access
credentials
winrm

1 Answer

1

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();
answered on Stack Overflow Nov 17, 2017 by Amit Shakya

User contributions licensed under CC BY-SA 3.0