I'm making an application that needs to list all the LPT ports in a machine with their IO addresses. (ie it's output : LPT1 [starting ; ending] ....)
Using WMI you can get this info.. the name/number from Win32_ParallelPort and the addresses from Win32_PortResource.
The problem is that i don't know how to associate the portname with it's addresses.
You have to query three times and loop over the results to get the matching records from ParallelPort, PnPAllocatedResource and PortResource. The following code does exactly that:
var parallelPort = new ManagementObjectSearcher("Select * From Win32_ParallelPort");
//Dump(parallelPort.Get());
foreach (var rec in parallelPort.Get())
{
var wql = "Select * From Win32_PnPAllocatedResource";
var pnp = new ManagementObjectSearcher(wql);
var searchTerm = rec.Properties["PNPDeviceId"].Value.ToString();
// compensate for escaping
searchTerm = searchTerm.Replace(#"\", #"\\");
foreach (var pnpRec in pnp.Get())
{
var objRef = pnpRec.Properties["dependent"].Value.ToString();
var antref = pnpRec.Properties["antecedent"].Value.ToString();
if (objRef.Contains(searchTerm))
{
var wqlPort = "Select * From Win32_PortResource";
var port = new ManagementObjectSearcher(wqlPort);
foreach (var portRec in port.Get())
{
if (portRec.ToString() == antref)
{
Console.WriteLine( "{0} [{1};{2}]",
rec.Properties["Name"].Value,
portRec.Properties["StartingAddress"].Value,
portRec.Properties["EndingAddress"].Value );
}
}
}
}
}
Related
I am trying to get a list of MSFT_Partitions from a MSFT_Disk object and loop over them.
This is the code I have been using so far but it always outputs an System.Management.ManagementException: 'Invalid query ' exception.
This is the code I am using at the moment:
public static void GetDiskInfo() {
var rawDiskInfos = new ManagementObjectSearcher("root\\Microsoft\\Windows\\Storage", "SELECT * FROM MSFT_Disk");
foreach(var rawDiskInfo in rawDiskInfos.Get()) {
Console.WriteLine(rawDiskInfo["FriendlyName"]);
GetPartitionInfo(rawDiskInfo["ObjectId"]);
}
}
public static void GetPartitionInfo(object objectId) {
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\Microsoft\\Windows\\Storage");
var query = new ObjectQuery("ASSOCIATORS OF {MSFT_Disk.ObjectId=\"" + objectId + "\"} WHERE AssocClass = MSFT_DiskToPartition");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach(var partiton in queryCollection) {
Console.WriteLine(partiton["Guid"]);
}
}
I already tried a lot of solutions I found online and all of them resulted in the same exception.
Thank you very much for your help!
Most queries you'll find on the internet don't do it, but in the general case, you must escape raw strings you pass to WQL queries (in your case objectId contains special characters), using the backslash character, with a method like this:
public static string EscapeWql(string text)
{
if (text == null)
return null;
var sb = new StringBuilder(text.Length);
foreach (var c in text)
{
if (c == '\\' || c == '\'' || c == '"')
{
sb.Append('\\');
}
sb.Append(c);
}
return sb.ToString();
}
So your method should now look like this:
public static void GetPartitionInfo(object objectId)
{
var scope = new ManagementScope(#"root\Microsoft\Windows\Storage");
var query = new ObjectQuery("ASSOCIATORS OF {MSFT_Disk.ObjectId=\"" + EscapeWql((string)objectId) + "\"} WHERE AssocClass = MSFT_DiskToPartition");
using (var searcher = new ManagementObjectSearcher(scope, query))
{
using (var queryCollection = searcher.Get())
{
foreach (var partition in queryCollection)
{
Console.WriteLine(partition["Guid"]);
}
}
}
}
PS: don't forget using statements on IDisposable classes.
I'm completly new to SCCM and I am confused by the object model. I have a folder like this:
And I want to get a list of the object inside it:
APP0001V01 (etc)
APP0001V02 (etc)
etc
The code I have so far is:
public void GetCollectionTest2()
{
var con = this.scomService.Connect(Server, UserName, Password);
string query = "select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
IResultObject packages = con.QueryProcessor.ExecuteQuery(query);
foreach (IResultObject ws in packages)
{
foreach (IResultObject item in ws.Properties)
{
Debug.Print(item.ToString());
}
string query2 = "SELECT * FROM SMS_FullCollectionMembership WHERE InstanceKey = '" + ws["InstanceKey"] + "'";
IResultObject packages2 = con.QueryProcessor.ExecuteQuery(query2);
foreach (IResultObject ws2 in packages2)
{
Debug.Print(ws2["Name"].StringValue);
}
}
}
I think that
"select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
Is returning the folder I want but when I try to get the contents I just keep drawing a blank.
What should I be doing?
Update
Thanks to the reply from Xin Guo I now have:
public void GetCollectionTest2()
{
var con = this.scomService.Connect(Server, UserName, Password);
string query = "select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
IResultObject packages = con.QueryProcessor.ExecuteQuery(query);
foreach (IResultObject ws in packages)
{
foreach (IResultObject item in ws.Properties)
{
Debug.Print(item.ToString());
}
}
}
This seems to give me all the item but how do I get their names?
At the moment I can only return these:
instance of SMS_ObjectContainerItem
{
ContainerNodeID = 16777219;
InstanceKey = "0010047C";
MemberGuid = "C8A66344-B7E8-451B-A4EF-9BFB3B3E228C";
MemberID = 16778256;
ObjectType = 5000;
ObjectTypeName = "SMS_Collection_Device";
SourceSite = "";
};
I assume I need to link to use an ID to look the name up somewhere else but I can't find any documentation on this?
I recommend you run select * from SMS_ObjectContainerItem to see the result.
Here is the result my test lab.
WMI Explorer is a very useful tool.
Ahh finally worked it out thanks to a good rummage through WMI Explorer. In the diagram above the folders on the left are Containers & those on the right are collections.
So the code I wanted was:
public void GetCollectionTest2()
{
var con = this.scomService.Connect(Server, UserName, Password);
string query = "select * from SMS_ObjectContainerItem where ContainerNodeID = '16777219'";
IResultObject packages = con.QueryProcessor.ExecuteQuery(query);
foreach (IResultObject ws in packages)
{
Debug.Print(ws["InstanceKey"].StringValue);
string query2 = "SELECT * FROM SMS_Collection WHERE CollectionID='"+ ws["InstanceKey"].StringValue +"'";
//// Run query.
IResultObject colResultObject = con.QueryProcessor.ExecuteQuery(query2);
foreach (IResultObject ws2 in colResultObject)
{
Debug.Print(ws2["Name"].StringValue);
}
}
}
Thanks for your help in getting to this Xin
I need to query LDAP on multiple paths and I wish to use DirectoryServices for various reasons.
var ADobjects = new Dictionary<string, ADobject>();
foreach (var OUItem in OUs)
{
using (DirectoryEntry ldap = new DirectoryEntry("LDAP://" + OUItem))
{
using (DirectorySearcher searcher = new DirectorySearcher(ldap))
{
searcher.Filter = "(objectClass=user)";
searcher.PropertiesToLoad.Add("distinguishedName");
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("displayName");
using (SearchResultCollection results = searcher.FindAll())
{
foreach (SearchResult result in results)
{
var dn = result.Properties["distinguishedName"][0].ToString();
if (!ADobjects.ContainsKey(dn))
{
ADobjects.Add(dn, new ADobject(result));
}
}
}
}
}
}
This works.. but when I have 10 OU's to query I would like to launch these queries in parallel.
I know how to launch an async method (more or less) but how can I use these queries to fill 1 dictionary at the same time??
I've clicked around but I'm really unsure if this can and should be done async. Since every search might take a few seconds, it could really benefit from async tasks.
You can use futures http://msdn.microsoft.com/en-us/library/ff963556.aspx
example from mentioned source:
int BufferSize = ...
var buffer1 = new BlockingCollection<string>(BufferSize);
var f = new TaskFactory(TaskCreationOptions.LongRunning,
TaskContinuationOptions.None);
var stage1 = f.StartNew(() => LdapSearch(buffer1));
var stage2 = f.StartNew(() => LdapSearch(buffer1));
var stage3 = f.StartNew(() => LdapSearch(buffer1));
var stage4 = f.StartNew(() => LdapSearch(buffer1));
Task.WaitAll(stage1, stage2, stage3, stage4);
static void LdapSearch(BlockingCollection<string> output,
)
{
output.Add(some result);
}
I want to check what type of operating system i use and what kind of processor. this should be check on run time. i tried using
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
and
System.OperatingSystem osInfo2 = System.Environment.OSVersion;
Console.WriteLine(osInfo2.ToString());
but it's just the enviroment that VS is running on.
I was told to use WMI to check it but i can't find out how.
can someone help me with that?
Retrieving OS info:
var wmi =
new ManagementObjectSearcher( "select * from Win32_OperatingSystem" )
.Get()
.Cast<ManagementObject>()
.First();
OS.Name = ((string)wmi["Caption"]).Trim();
OS.Version = (string)wmi["Version"];
OS.MaxProcessCount = (uint)wmi["MaxNumberOfProcesses"];
OS.MaxProcessRAM = (ulong)wmi["MaxProcessMemorySize"];
OS.Architecture = (string)wmi["OSArchitecture"];
OS.SerialNumber = (string)wmi["SerialNumber"];
OS.Build = ((string)wmi["BuildNumber"]).ToUint();
Retrieving CPU info:
var cpu =
new ManagementObjectSearcher( "select * from Win32_Processor" )
.Get()
.Cast<ManagementObject>()
.First();
CPU.ID = (string)cpu["ProcessorId"];
CPU.Socket = (string)cpu["SocketDesignation"];
CPU.Name = (string)cpu["Name"];
CPU.Description = (string)cpu["Caption"];
CPU.AddressWidth = (ushort)cpu["AddressWidth"];
CPU.DataWidth = (ushort)cpu["DataWidth"];
CPU.Architecture = (CPU.CpuArchitecture)(ushort)cpu["Architecture"];
CPU.SpeedMHz = (uint)cpu["MaxClockSpeed"];
CPU.BusSpeedMHz = (uint)cpu["ExtClock"];
CPU.L2Cache = (uint)cpu["L2CacheSize"] * (ulong)1024;
CPU.L3Cache = (uint)cpu["L3CacheSize"] * (ulong)1024;
CPU.Cores = (uint)cpu["NumberOfCores"];
CPU.Threads = (uint)cpu["NumberOfLogicalProcessors"];
CPU.Name =
CPU.Name
.Replace( "(TM)", "™" )
.Replace( "(tm)", "™" )
.Replace( "(R)", "®" )
.Replace( "(r)", "®" )
.Replace( "(C)", "©" )
.Replace( "(c)", "©" )
.Replace( " ", " " )
.Replace( " ", " " );
Yes WMI is the best way to do this kind of stuff
You can use this to retrieve OS informations :
ManagementObjectSearcher objMOS = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_OperatingSystem");
To determine the operating system use this code:
string OPSystemVersion = Environment.OSVersion.ToString();
To determine the CPU name and type first add System.Management reference to your project, then you can use this code:
public static string SendBackProcessorName()
{
ManagementObjectSearcher mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
string Procname = null;
foreach (ManagementObject moProcessor in mosProcessor.Get())
{
if (moProcessor["name"] != null)
{
Procname = moProcessor["name"].ToString();
}
}
return Procname;
}
Look at the ManagementClass class:
http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx
var mgmt = new ManagementClass("Win32_OperatingSystem");
foreach (ManagementObject mgmtObj in mgmt.GetInstances())
{
// Just get first value.
return mgmtObj[info.Information].ToString().Trim();
}
i had written a code to display the description(Name) of connected USB devices.once i removed a device,then i need to refresh the ManagementObject and have to display the connected device description.
Here is my Code,
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice");
foreach (ManagementObject mo in searcher.Get())
{
string str1 = mo["CurrentRefreshRate"].ToString();
Console.WriteLine(str1);
string dependent = mo["Dependent"].ToString();
string deviceId = dependent.Split('=')[1];
deviceId = deviceId.Replace('\"', '\'');
ManagementObjectSearcher searcher2 =
new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity Where DeviceID = " + deviceId);
foreach (ManagementObject mo2 in searcher2.Get())
{
HardwareDetails Detail = new HardwareDetails();
Detail.Description = mo2["Description"].ToString();
Detail.DeviceId = mo2["DeviceId"].ToString();
string[] str = Detail.DeviceId.Split('\\');
string Id = str[1];
if (Id.Contains('&'))
{
string[] separate = Id.Split('&');
Detail.Vid = separate[0].Contains('_') ? separate[0].Split('_')[1] : separate[0].Split('D')[1];
Detail.Pid = separate[1].Contains('_') ? separate[1].Split('_')[1] : separate[1].Split('D')[1];
//Detail.Pid = pid1[1];
}
else
{
Detail.Vid = "";
Detail.Pid = "";
}
if (list.Count > 0)
{
foreach (HardwareDetails h in list)
{
if (!(h.Description == Detail.Description))
{
list.Add(Detail);
break;
}
}
}
else
list.Add(Detail);
}
}
// remove duplicates, sort alphabetically and convert to array
HardwareDetails[] usbDevices = list.ToArray();
return usbDevices;
Did you try this?
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM Win32_DeviceChangeEvent");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
watcher.EventArrived +=
new EventArrivedEventHandler(HandleEvent);
// Start listening for events
watcher.Start();
.........
// Stop listening for events
watcher.Stop();
And in the HandleEvent add or remove device from the list
Hope this helps!