public ManagementObjectCollection Query(ObjectQuery query, WmiPath wmiPath) { ManagementObjectCollection mgmtObjs = null; switch (wmiPath) { case WmiPath.Root: { EnumerationOptions eo = new EnumerationOptions(); eo.Rewindable = false; eo.ReturnImmediately = true; eo.EnumerateDeep = false; eo.EnsureLocatable = false; eo.DirectRead = true; eo.EnsureLocatable = false; eo.UseAmendedQualifiers = false; eo.BlockSize = 10; mgmtObjs = new ManagementObjectSearcher(Scope, query, eo).Get(); break; } case WmiPath.IIS: { mgmtObjs = new ManagementObjectSearcher(Scope, query).Get(); break; } } return(mgmtObjs); }
public static int Main() { System.Diagnostics.TraceSwitch MySwitch = new System.Diagnostics.TraceSwitch("MySwitch", null); MySwitch.Level = System.Diagnostics.TraceLevel.Verbose; Console.WriteLine("WMI COM+ Test Application"); //Create a WmiPath object WmiPath Path = new WmiPath("root\\cimv2"); Console.WriteLine("String in WmiPath object is : {0}", Path.PathString); //Make a connection to this path WmiCollection Session = WmiCollection.Connect(Path); //Get a class from this connection WmiObject ServiceClass = Session.Get(new WmiPath("Win32_LogicalDisk")); //WmiObject ServiceClass = Session.Get("Win32_LogicalDisk"); //Display the class name (__CLASS property of the class) Console.WriteLine("The name of this class is : {0}", ServiceClass["__CLASS"]); //Enumerate instances WmiCollection Instances = Session.Open(new WmiPath("Win32_LogicalDisk")); foreach (WmiObject obj in Instances) { Console.WriteLine("The key of this instance is : {0}", obj["Name"]); } //Query WmiCollection QueryRes = Session.Query(new WmiQuery("select * from Win32_Service")); foreach (WmiObject Service in QueryRes) { Console.WriteLine("{0}", Service["Name"]); } return(0); }
protected WmiManagementBase(string host, WmiPath wmiPath) { switch (wmiPath) { case WmiPath.Root: { string path = string.Format(@"\\{0}\root\cimv2", host); Scope = new ManagementScope(path); break; } case WmiPath.IIS: { string path = string.Format(@"\\{0}\root\MicrosoftIISv2", host); Scope = new ManagementScope(path); break; } } Scope.Options.Impersonation = ImpersonationLevel.Impersonate; Scope.Options.EnablePrivileges = true; Scope.Options.Authentication = AuthenticationLevel.PacketPrivacy; Scope.Connect(); }
public static int Main(String[] args) { TextWriter oldTW = null; //used to save old console.out for logging StreamWriter sw = null; //used to redirect console.out to file for logging Console.WriteLine("WMI Plus Test Application"); //validate arguments if (((args.Length != 0) && (args.Length != 2)) || ((args.Length == 2) && (args[0] != "/log"))) { Console.WriteLine("Usage: ... "); return(0); } //if "/log" specified, redirect Console.Out to log file if (args.Length > 0) { FileStream fs = new FileStream(args[1], FileMode.OpenOrCreate, FileAccess.ReadWrite); //open log file oldTW = Console.Out; //save default standard output sw = new StreamWriter(fs); Console.SetOut(sw); //replace with file } //Create a WmiPath object WmiPath Path = new WmiPath("root\\default"); Console.WriteLine("String in WmiPath object is : {0}", Path.PathString); //Make a connection to this path WmiCollection Session = WmiCollection.Connect(Path); // WmiCollection Session = WmiCollection.Connect("root\\cimv2"); //Get a class from this connection WmiObject ServiceClass = Session.Get(new WmiPath("MyStaticClass")); //Display the class name (__CLASS property of the class) Console.WriteLine("The name of this class is : {0}", ServiceClass["__CLASS"]); //Enumerate instances (static only because of security problem !!) WmiCollection Instances = Session.Open(new WmiPath("MyStaticClass")); foreach (WmiObject obj in Instances) { Console.WriteLine("The key of this instance is : {0}", obj["MyProp"]); } // WmiCollection Services = Session.Open("Win32_Service"); // foreach (WmiObject Service in Services) // Console.WriteLine("{0}", Service["Name"]); //r } //r catch (Exception e) //r { //r Console.WriteLine("Exception caught : {0}", e.Message); //r } //flush log file, and restore standard output if changed if (sw != null) { sw.Flush(); } if (oldTW != null) { Console.SetOut(oldTW); } return(0); }