Does anyone has some example about accessing Active Directory, LDAP querying using WMI (System.Management namespace) and not System.DirectoryServices namespace.
Here on MSDN page it is described a little using CIM classes
http://msdn.microsoft.com/en-us/library/aa392320(v=VS.85).aspx
But I cant find some C# example realizing it.
For example, to access some Win32 class you have to initialize Scope object to use CIMV2 namespace
private ConnectionOptions connection;
private ManagementScope scope;
...
connection = new ConnectionOptions();
...
scope = new ManagementScope("\\\\" + computer + "\\root\\CIMV2", connection);
try
{
scope.Connect();
}
And use ObjectQuery class for querying WMI data
ObjectQuery objectQuery = new ObjectQuery("SELECT Name FROM Win32_Processor");
ManagementObjectSearcher searcher = ManagementObjectSearcher(scope, objectQuery);
foreach (ManagementObject queryObj in searcher.Get())
{
return queryObj["Name"].ToString();
}
How is it possible to access AD using the same scope?
Thanks :)
Related
Can some one help me how to find service name from task manager using the process id in c#. I have searched in the online. But, I couldn't find correct solution.
You can use WMI to query the Win32_Service class.
var processId = 2220;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
$"SELECT * FROM Win32_service WHERE ProcessId={processId}");
foreach (ManagementObject queryObj in searcher.Get())
{
var serviceName = queryObj["Name"]; // Access service name
}
We have 6 Citrix Servers. I'm trying to find out if Remote Logons are enabled/disabled.
I plan to put this onto of a webpage to display and green icon if they are or red if they aren't.
I've managed to connect to the machines and pull operating system information etc.. However when i try and connect to view the TerminalServiceSetting information.. i get an Invalid Class error.
Here is my code.
ManagementScope scope = new ManagementScope("\\\\MACHINENAME\\ROOT\\cimv2");
scope.Connect();
//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalServiceSetting");
//create object searcher
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
//get collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();
//enumerate the collection.
foreach (ManagementObject m in queryCollection)
{
// access properties of the WMI object
Label1.Text = m["AllowTSConnections"].ToString();
}
If anyone can shed some light on it that would be great.
Thanks
Update:
I have now found the code (i think) that checks to see if remote connections are enabled or disabled.
ManagementScope scope =
new ManagementScope("\\\\MACHINENAME\\root\\CIMV2\\TerminalServices",con);
scope.Options.EnablePrivileges = true;
scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Connect();
//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalServiceSetting");
//create object searcher
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
//get collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();
//enumerate the collection.
foreach (ManagementObject m in queryCollection)
{
if (m["AllowTSConnections"].ToString() == "1")
{
Redicon.Visible = false;
}
else
{
Greenicon.Visible = false;
}
}
However when i run the code i get returned "1".. which is fine. However if i deny remote logins on the server and re run the code it stays at 1..
Any ideas?
You need to be sure that the server provide the TerminalServiceSetting information. WMI uses unmanaged code because not all servers and their configurations provide all information.
You can use Mgmtclassgen to generate managed code and at the same time make sure that the server provides the information.
Sorted!!!
I was looking up the wrong field.
the correct one is :
Label1.Text = "Remote Connections: " + m["Logons"].ToString();
I want to get id processor in .NET with WMI but when I'm using the get() method from the ManagementObjectSearcher, I'm getting an out of memory exception ...
If you want to take a look from the code see below :
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"select * from Win32_Processor");
foreach (ManagementObject share in searcher.Get())
foreach (PropertyData PC in share.Properties)
if (PC.Name.Equals("ProcessorId"))
return (string)PC.Value;
return null;
This code works on others computers but not on mine ...
I'm using windows 7.
What is the problem ?
I tried to restart WMI service and that not resolve my problem :(
There are several reasons which could cause out of memory exception.
possible memory leak in WMI, source:
http://brooke.blogs.sqlsentry.net/2010/02/win32service-memory-leak.html
check whether you have permission(s), that would explain why does your code work on some computers and why doesn't on yours.
run your code as Administrator (for debugging start VS as Administrator)
Here is an other code snippet, try this one as well... who knows
Sample:
public static String GetCPUId()
{
String processorID = "";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"Select * FROM WIN32_Processor");
ManagementObjectCollection mObject = searcher.Get();
foreach (ManagementObject obj in mObject)
{
processorID = obj["ProcessorId"].ToString();
}
return processorID;
}
Source: WIN32_Processor::Is ProcessorId Unique for all computers
Anyone know how to find Scope ?
Microsoft say
ManagementScope scope = new ManagementScope("\\\\FullComputerName\\root\\cimv2");
I work in localhost.
I already test "\\HOSTNAME\root\cimv2" and "\\HOSTNAME\MY_ACCOUNT_NAME\cimv2" but they doesn't work.
I'm on Windows 7 Pro, I use Visual Studio 2010 and it's .NET 4
#
Update 1
int i = Convert.ToInt32(processIds[index]);
String queryString = "select CreationDate from Win32_Process where ProcessId='" + processIds[index] + "'";
SelectQuery query = new SelectQuery(queryString);
ManagementScope scope = new System.Management.ManagementScope("\\\\XXX-PC\\YYY\\cimv2");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();
#keyboardP : Searcher.Get() work an exception, so I suppose my Scope it's not right.
I've been developing an application that uses Management Scopes for quite some time now. I think you just have to omit the Host Name.
This works for me:
ManagementScope scope = new ManagementScope("\\root\\cimv2");
scope.Connect();
I'm running into an odd issue retrieving printer port addresses.
When I get all the entries in Win32_TCPIPPrinterPort, the HostAddress field (which should have the IP address) is usually blank/null, only the port name has a value. To make it a bit stranger, if a particular port is not in use by any printer, THEN the HostAddress will have the the proper value.
The C# code is simple, and results in something like this;
IP_192.168.1.100,
printerportxyz,
richTextBox1.Clear();
ManagementObjectSearcher portSearcher = new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_TCPIPPrinterPort");
foreach (ManagementObject port in portSearcher.Get())
{
richTextBox1.AppendText(
String.Format("Name: {0} HostAddress: {1}",
port["Name"],
port["HostAddress"])
);
}
I also tried the same thing in WSH/VBS, and saw the same behavior.
I ended up having to re-visit this, and making another attempt. I found that the built-in prnport.vbs managment script had no issues - looking into it I saw that while establishing its WMI connection it had oService.Security_.Priveleges.AddAsString "SeLoadDriverPrivilege"
the solution in C# ended up specifying the WMI ConnectionOptions and setting EnablePrivileges to true. Then the HostAdress was no longer null for unused or in-use ports.
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;
ManagementScope mgmtScope = new ManagementScope("root\\CIMV2", connOptions);
mgmtScope.Connect();
ObjectQuery objQuery = new ObjectQuery("SELECT * FROM Win32_TCPIPPrinterPort");
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(mgmtScope, objQuery);
foreach (ManagementObject mo in moSearcher.Get())
{
Console.WriteLine(String.Format("PortName={0} HostAddress={1}", mo["Name"], mo["HostAddress"]));
}