Archive for Gennaio 25th, 2008

Remote WMI Connection

CodeKeep C# Feed Gennaio 25th, 2008

Description: Method to connect to a remote computer. Returns the WMI Connection scope, usable to query from.

Link: http://www.codekeep.net/snippets/29295cc5-56a4-4960-915b-5c385827d395.aspx

/// <summary>
/// Connects to a computer, returns connection.
/// </summary>
/// <param name="Computer">The computer to connect to.</param>
/// <returns>The WMI Connection scope, usable to query from.</returns>
private ManagementScope ConnectTo(string Computer)
{
     //If computer info is empty, default to local computer.
     if (String.Compare(Computer.Trim(), "") == 0)
          Computer = ".";

     ConnectionOptions WMIConn = new ConnectionOptions();
     WMIConn.Impersonation = ImpersonationLevel.Impersonate;
     WMIConn.Authentication = AuthenticationLevel.PacketPrivacy;
     WMIConn.Authority = "ntlmdomain:RB";

     scope = new ManagementScope("\\\\" + Computer + "\\root\\cimv2", WMIConn);
     scope.Options.Impersonation = ImpersonationLevel.Impersonate;

     return scope;
}