public static ManagementObject GetClassObject(string server, string ns, string className, string user, string pw) { if (ns == string.Empty || className == string.Empty) { throw new ArgumentException(); } try { ManagementPath classPath = new ManagementPath(WmiHelper.MakeClassPath(server, ns, className)); ObjectGetOptions options = new ObjectGetOptions(null, true); ConnectionOptions connectOpts = new ConnectionOptions( "", //locale user, //username pw, //password "", //authority ImpersonationLevel.Impersonate, AuthenticationLevel.Connect, true, //enablePrivileges null //context ); ManagementScope scope = (server == WmiHelper.DNS2UNC(Dns.GetHostName())) ? new ManagementScope(WmiHelper.MakeNSPath(server, ns)) : new ManagementScope(WmiHelper.MakeNSPath(server, ns), connectOpts); ManagementObject classObj = new ManagementObject(scope, classPath, options); return(classObj); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(null); } }
// // METHODS // // <doc> // <desc> // Create <Add Classes> node under this node. // </desc> // </doc> public override Node[] CreateChildren() { //TODO: if connectAs and password are specified and this is a local server, //you cannot connect to WMI. Offer an option to connect as a current user //instead. If rejected, make this node an error node. Node[] children = GetNodeSite().GetChildNodes(); if (children == null || children.Length == 0) { children = new Node[DefaultClasses.Length + 1]; int i, k; for (i = 0, k = 0; i < DefaultClasses.Length; i++) { try { children[k] = new WMIClassNode(WmiHelper.MakeClassPath(GetNodeSite().GetMachineName(), DefaultNS, DefaultClasses[i]), this.ConnectAs, this.Password); } catch (Exception) { //children[k] = new ErrorNode(new ErrorComponent(exc)); continue; //k wouldn't get increased } k++; } children[k] = GetNewChildNode(); Node[] children2 = new Node[k + 1]; Array.Copy(children, children2, k + 1); return(children2); } return(children); }
private bool EnumNamespaces(string parent, int num) //recursively adds namespaces to the drop-down box { try { //show the node TreeNode dummy = new TreeNode(""); TreeNode[] children = new TreeNode[] { dummy }; TreeNode nsNode = new TreeNode(parent, //(int)schema_icons.SCHEMA_NS_CLOSED, //(int)schema_icons.SCHEMA_NS_CLOSED, children); nsNode.Collapse(); this.Nodes.Insert(num, nsNode); string nsPath = WmiHelper.MakeClassPath(server, parent, "__NAMESPACE"); string nsScopePath = WmiHelper.MakeNSPath(server, parent); ConnectionOptions connectOpts = new ConnectionOptions("", //locale this.connectAs, //username this.password, //password "", //authority ImpersonationLevel.Impersonate, AuthenticationLevel.Connect, true, //enablePrivileges null //context ); ManagementScope scope = (this.server == WmiHelper.DNS2UNC(Dns.GetHostName())) ? new ManagementScope(nsScopePath): new ManagementScope(nsScopePath, connectOpts); ManagementClass nsClass = new ManagementClass(scope, new ManagementPath(nsPath), new ObjectGetOptions(null, true)); ManagementObjectCollection subNSCollection = nsClass.GetInstances(); IEnumerator eInstances = ((IEnumerable)subNSCollection).GetEnumerator(); while (eInstances.MoveNext()) { num++; ManagementObject obj = (ManagementObject)eInstances.Current; PropertyCollection props = (PropertyCollection)obj.Properties; string NameOut = ""; string curName = props["Name"].Value.ToString(); //skip localized namespace //NOTE: this assumes that localized namespaces are always leaf if (curName.ToUpper().IndexOf("MS_", 0) == 0) { continue; } //skip root\security namespace (we don't want to expose it) if (curName.ToUpper() == "SECURITY" && parent.ToUpper() == "ROOT") { continue; } //skip root\directory\ldap namespace (BUGBUG: change this in Beta2 when we can do asynchronous class enumerations) if (curName.ToUpper() == "LDAP" && parent.ToUpper() == "ROOT\\DIRECTORY") { continue; } if (parent != "") { NameOut = parent + "\\" + curName; } else { NameOut = curName; } EnumNamespaces(NameOut, num); } return(true); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(false); } }
private void OnAdvanced(Object source, EventArgs args) { try { QueryConditionDialog dlg = null; if (this.SelectedEventType == EventType.Instance) { //spawn an instance of the appropriate intrinsic event class //and set its TargetInstance property to the selected data class //Then, pass the instance object to QueryConditionDialog ctor. ManagementClass classObj = null; ManagementObject instObj = null; ManagementScope rootScope = new ManagementScope(new ManagementPath(WmiHelper.MakeNSPath(server, "root")), new ConnectionOptions("", connectAs, password, "", ImpersonationLevel.Impersonate, AuthenticationLevel.Connect, true, null)); if (EventTypeSelector.Text == WMISys.GetString("WMISE_IntrinsicEvent_Created")) { classObj = new ManagementClass(rootScope, new ManagementPath(WmiHelper.MakeClassPath(server, "root", "__InstanceCreationEvent")), new ObjectGetOptions(null, true)); instObj = classObj.CreateInstance(); } else if (EventTypeSelector.Text == WMISys.GetString("WMISE_IntrinsicEvent_Modified")) { classObj = new ManagementClass(rootScope, new ManagementPath(WmiHelper.MakeClassPath(server, "root", "__InstanceModificationEvent")), new ObjectGetOptions(null, true)); instObj = classObj.CreateInstance(); } else if (EventTypeSelector.Text == WMISys.GetString("WMISE_IntrinsicEvent_Deleted")) { classObj = new ManagementClass(rootScope, new ManagementPath(WmiHelper.MakeClassPath(server, "root", "__InstanceDeletionEvent")), new ObjectGetOptions(null, true)); instObj = classObj.CreateInstance(); } else if (EventTypeSelector.Text == WMISys.GetString("WMISE_IntrinsicEvent_Operated")) { classObj = new ManagementClass(rootScope, new ManagementPath(WmiHelper.MakeClassPath(server, "root", "__InstanceOperationEvent")), new ObjectGetOptions(null, true)); instObj = classObj.CreateInstance(); } ManagementObject targetObj = new ManagementObject(WmiHelper.MakeClassPath(server, classTree.SelectedNS, classTree.SelectedClass), new ObjectGetOptions(null, false)); instObj["TargetInstance"] = (Object)targetObj; dlg = new QueryConditionDialog(instObj, server, classTree.SelectedNS, classTree.SelectedClass, queryString); } else { dlg = new QueryConditionDialog(server, connectAs, password, classTree.SelectedNS, classTree.SelectedClass, queryString); } DialogResult res = dlg.ShowDialog(); if (res == DialogResult.Cancel) { return; } //update query text; queryString = dlg.QueryString; } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); } }