I am trying to read two setting values in Local Group policy (gpedit.msc). The path is :
Local Computer Policy\Windows Settings\Security Settings\Local Policies\User Rights Assignment
The Policy that I want to read are :
1. Perform volume maintainace tasks (Users assigned to it)
2. Lock Pages in memory (Users assigned to it).
I have searched the web (including all stackoverflow threads) and could not find a solution to this but could not get a solution to this. Below is the code I am using currently but it only returns me 7 values.
I am not sure if this is possible. Please suggest. I am using C# .NET as language and would prefer if possible be able to read these setting from a remote machine (so I am preferring WMI approach).
Also I only want to read values. Now editing or writing...
Please suggest..
Girija
Code
private void Test()
{
ManagementScope scope =
new ManagementScope(
"\\\\localhost\\root\\rsop\\Computer");
scope.Connect();
ObjectQuery query = new ObjectQuery(
"SELECT * FROM RSOP_UserPrivilegeRight");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
List<string> val = new List<string>();
foreach (ManagementObject mgo in queryCollection)
{
var d = mgo["Name"];
val.Add(Convert.ToString(d));
}
}
Related
I am working on a project which supposed to include computer's fans status. Most of the properties I need can be acquired from the Win32_Fan class. Sadly, I couldn't find a way to use this class for getting the current reading of the fan speed. In the Win32_Fan MSDN page it is mentioned in the "DesiredSpeed" property that the current speed is determined by a sensor named CIM_Tachometer:
DesiredSpeed
Data type: uint64
Access type: Read-only
Qualifiers: Units ("revolutions per minute")
Currently requested fan speed, defined in revolutions per minute, when
a variable speed fan is supported (VariableSpeed is TRUE). The current
speed is determined by a sensor (CIM_Tachometer) that is associated
with the fan using the CIM_AssociatedSensor relationship.
This property is inherited from CIM_Fan.
For more information about using uint64 values in scripts, see
Scripting in WMI.
After I saw that, I searched for this Tachometer CIM sensor and found the following code snippet (which was taken from http://wutils.com/wmi/root/cimv2/cim_tachometer/cs-samples.html):
//Project -> Add reference -> System.Management
//using System.Management;
//set the class name and namespace
string NamespacePath = "\\\\.\\ROOT\\cimv2";
string ClassName = "CIM_Tachometer";
//Create ManagementClass
ManagementClass oClass = new ManagementClass(NamespacePath + ":" + ClassName);
//Get all instances of the class and enumerate them
foreach (ManagementObject oObject in oClass.GetInstances())
{
//access a property of the Management object
Console.WriteLine("Accuracy : {0}", oObject["Accuracy"]);
}
And so I tried implementing it in my code:
public static String[] GetFanInfo()
{
ManagementClass cSpeed = new ManagementClass
("\\\\.\\ROOT\\cimv2:CIM_Tachometer"); //Create ManagementClass for the current speed property
ManagementObjectSearcher temp = new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSAcpi_ThermalZoneTemperature"); //Create management object searcher for the temperature property
ManagementObjectSearcher mos = new ManagementObjectSearcher
("SELECT * FROM Win32_Fan"); //Create a management object searcher for the other properties
string[] Id = new string[8]; //Preparig a string array in which the results will be returned
Id[0] = "Fan"; //First value is the category name
foreach (ManagementObject mo in mos.Get())
{
Id[1] = mo["Name"].ToString(); //Name of the component
Id[2] = mo["Status"].ToString(); //Component's status
long vel = Convert.ToInt64(mo["DesiredSpeed"]); //Desired speed of the component
Id[4] = Convert.ToString(vel);
bool s = Convert.ToBoolean(mo["variableSpeed"]); //Wheater or not variable speed are supported
Id[5] = s.ToString();
break;
}
foreach (ManagementObject obj in temp.Get())
{
Double temperature = Convert.ToDouble(obj["CurrentTemperature"].ToString()); //Fetching the temperature
Id[3] = Convert.ToString((temperature - 2732) / 10.0) + " C";
}
foreach (ManagementObject sObject in cSpeed.GetInstances()) //Get all instances of the class and enumerate them
{
Id[7] = sObject["CurrentReading"].ToString(); //Getting the current reading
}
return Id;
}
To my surprise, it seems that the whole section of the current reading is skipped during runtime. occur anyway!
My question is, why is this certain part skipped? Is the Tachometer a sensor which cannot be used? is it disabled for some reason?
Thanks ahead.
P.S.
I'm writing the program in Microsoft Visual Studio 2015 using winforms for the user interface.
we have a print server and lots of printers on it. We access them like this:
\\print-server-name\printer1
\\print-server-name\printer1_color
\\print-server-name\printer2
...etc.
I now need a list of all printers on that server. Until now, I could only find all printers installed locally on the machine. I found this while googling which gave me only the local printers aswell:
PrintServer lps = new PrintServer();
PrintQueueCollection prQueue = lps.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Connections });
liServerPrinterNames = (from printer in prQueue select printer).ToList();
Actually I only need the names of all printers on the server in a string array, I don't even need objects for interaction, only the names of all printers as string. Is that possible? How?
Thanks for any help in advance!
Gets all InstalledPrinters
foreach (string printer in PrinterSettings.InstalledPrinters)
Using WMI Windows Management Instrumentation
SelectQuery query = new SelectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos= new ManagementObjectSearcher(mos, query);
System.Management.ManagementObjectCollection moc= mos.Get();
foreach (ManagementObject Printers in moc )
Printers["Name"]; //GetPrinterName
}
Here is how I did it:
using System.Printing;
string serverAddress = #"\\server.domain.local"
PrintServer printServer = new PrintServer($#"{serverAddress}")
PrintQueueCollection queues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local });
foreach (var item in queues)
{
Console.WriteLine(item.Name)
}
If you want the full name (\\server.domain.local\PrinterName) use property FullName instead of Name :)
I have the following piece of code.
It is returning different results when running on the same machine in case of web and desktop applications.
Here is my code. Please guide me on what to do regarding this???
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
return (from ManagementObject wmiHD in searcher.Get()
select wmiHD["SerialNumber"] == null ? "VM HD" : wmiHD["SerialNumber"].ToString()).ToList();
Here is a LINQ-free version of the same code
var hdCollection = new List<string>();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject wmiHD in searcher.Get())
{
// get the hardware serial no.
if (wmiHD["SerialNumber"] == null)
{
hdCollection.Add("VM HD");
}
else
{
hdCollection.Add(wmiHD["SerialNumber"].ToString());
}
}
return hdCollection;
That could possibly be caused by two things:
web server runs with different user account (probably NetworkService)
http://www.bluevisionsoftware.com/WebSite/TipsAndTricksDetails.aspx?Name=AspNetAccount
web server runs code without Fulltrust permissions (probably medium trust)
http://discussion.accuwebhosting.com/iis-web-server/993-how-grant-full-trust-mode-domain-asp-net-2-0-iis-6-0-a.html
Both actions can compromise security, but the first one gives more choices to fix this by setting ACLs.
presently I am writing a small piece of code to get the list of web services hosted on IIS in a remote system,
the working code right now is :
string q2 = "select * from Win32_PerfFormattedData_W3SVC_WebService";
ManagementScope scope2 = new ManagementScope(string.Format(#"\\dtp-robaro2\{1}", host, ns), options);
// ns here is string ns = #"root\cimv2";
scope2.Connect();
ManagementObjectSearcher search2 = new ManagementObjectSearcher(scope, new ObjectQuery(q2));
foreach (ManagementObject mo in search2.Get())
{
Console.WriteLine(mo.ClassPath);
Console.WriteLine(mo.GetText(TextFormat.Mof));
}
now I was wondering if WMI is turned off on the remote system that i am querying then is there any alternative way to access the information i get with the above code?
Use tool Service Control - SC.EXE
I have converted this from VB.Net to C# so it may not be exactly correct, but this will do what you need:
public List<string> GetSites(string MachineName)
{
List<string> siteList = new List<string>();
DirectoryEntry iis = new DirectoryEntry(string.Format("IIS://{0}/w3svc/1/root", MachineName));
foreach (DirectoryEntry site in iis.Children) {
if (site.SchemaClassName == "IIsWebServer") {
siteList.Add(site.Properties("ServerComment").Value.ToString());
}
}
return siteList;
}
The class is documented in
http://msdn.microsoft.com/en-us/library/aa375050%28VS.85%29.aspx
And from this page it seems it's not an abstract class:
http://msdn.microsoft.com/en-us/library/aa375084%28VS.85%29.aspx
But whenever I run the code below I get an "Invalid Class" exception in ManagementObjectSearcher.Get(). So, does this class exist or not?
ManagementScope scope;
ConnectionOptions options = new ConnectionOptions();
options.Username = tbUsername.Text;
options.Password = tbPassword.Password;
options.Authority = String.Format("ntlmdomain:{0}", tbDomain.Text);
scope = new ManagementScope(String.Format("\\\\{0}\\root\\RSOP", tbHost.Text), options);
scope.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM RSOP_RegistryPolicySetting"));
foreach (ManagementObject queryObj in searcher.Get())
{
wmiResults.Text += String.Format("id={0}\n", queryObj["id"]);
wmiResults.Text += String.Format("precedence={0}\n", queryObj["precedence"]);
wmiResults.Text += String.Format("registryKey={0}\n", queryObj["registryKey"]);
wmiResults.Text += String.Format("valueType={0}\n", queryObj["valueType"]);
}
In the first link above, it lists as a requirement something called a "MOF": "Rsopcls.mof". Is this something I should have but have not? How do I obtain it? Is it necessary in the querying machine or the queried machine? Or both?
I do have two copies of this file:
C:\Windows>dir rsop*.mof /s
Volume in drive C has no label.
Volume Serial Number is 245C-A6EF
Directory of C:\Windows\System32\wbem
02/11/2006 05:22 100.388 rsop.mof
1 File(s) 100.388 bytes
Directory of C:\Windows\winsxs\x86_microsoft-windows-grouppolicy-base-mof_31bf3856ad364e35_6.0.6001.18000_none_f2c4356a12313758
19/01/2008 07:03 100.388 rsop.mof
1 File(s) 100.388 bytes
Total Files Listed:
2 File(s) 200.776 bytes
0 Dir(s) 6.625.456.128 bytes free
Duh. I was using the wrong namespace. It was root\RSOP\Computer.