I try to control NLB with WMI.
WqlObjectQuery wql = new WqlObjectQuery (#"SELECT * FROM MicrosoftNLB_Node");
ManagementObjectSearcher search = new ManagementObjectSearcher(wql);
foreach (var obj in search.Get())
{
MessageBox.Show(obj.ToString());
}
I get a error message "Invalid class"
Try this:
ManagementObjectSearcher search = new ManagementObjectSearcher(
#"root\MicrosoftNLB",
#"SELECT * FROM MicrosoftNLB_Node");
foreach (var obj in search.Get())
{
MessageBox.Show(obj.ToString());
}
The MicrosoftNLB_Node class it's part of the Root\MicrosoftNLB namespace, So it seems which you are not setting the namespace before to connect to the WMi service.
try this
ManagementObjectSearcher search = new ManagementObjectSearcher(#"root\MicrosoftNLB",wql);
Related
I am querying an SCCM database for package and application properties. What it seems like is that if I use a simple query, I can view and access the properties. On a more complex query where I am assigning variable names to keep the properties separate I cannot.
In the example code below, the query for packages will print out those properties as expected. In the query for applications, all it seems to know about is either AL or DT for the name and the value comes back as a ManagementBaseObject. In fact, the code as posted will crash every time on printing out those property values.
Very confusing, appreciate any input to get this working.
using System;
using System.Management;
namespace Application
{
public class MainApplication
{
static void Main(string[] args)
{
ManagementObjectCollection packages = GetPackages();
ManagementObjectCollection applications = GetApplications();
// * Works! *
foreach (var package in packages)
{
Console.WriteLine("Package Name: {0} ", package.Properties["PackageName"].Value.ToString());
Console.WriteLine("Package ID: {0} ", package.Properties["PackageID"].Value.ToString());
Console.WriteLine("Setup Command: {0} ", package.Properties["CommandLine"].Value.ToString());
}
// * Does not work! *
foreach (var application in applications)
{
Console.WriteLine("Package Name: {0} ", application.Properties["AL.LocalizedDisplayName"].Value.ToString());
Console.WriteLine("Package ID: {0} ", application.Properties["DT.ContentID"].Value.ToString());
Console.WriteLine("Setup Command: {0} ", application.Properties["DT.LocalizedDescription"].Value.ToString());
}
}
private static ManagementObjectCollection GetPackages()
{
String queryString = "SELECT PackageName,PackageID,CommandLine,Comment FROM SMS_Program WHERE Comment LIKE '%LISTMANUAL%' ORDER BY PackageName";
ObjectQuery query = new ObjectQuery(queryString);
ManagementScope scope = new ManagementScope("\\\\somebox.somedomain.com\\root\\sms\\site_DC1");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objectCollection = searcher.Get();
Console.WriteLine(objectCollection.Count);
return objectCollection;
}
private static ManagementObjectCollection GetApplications()
{
String queryString = "SELECT DT.LocalizedDisplayName,DT.LocalizedDescription,DT.AppModelName,DT.ContentID,AL.LocalizedDisplayName,AL.ModelName FROM SMS_DeploymentType AS DT JOIN SMS_ApplicationLatest AS AL on AL.ModelName=DT.AppModelName JOIN SMS_PackageToContent AS PTC on PTC.ContentUniqueID = DT.ContentID WHERE DT.IsLatest='TRUE' AND DT.LocalizedDescription LIKE '%LISTMANUAL%' ORDER BY AL.LocalizedDisplayName";
ObjectQuery query = new ObjectQuery(queryString);
ManagementScope scope = new ManagementScope("\\\\somebox.somedomain.com\\root\\sms\\site_DC1");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objectCollection = searcher.Get();
Console.WriteLine(objectCollection.Count);
return objectCollection;
}
}
}
ManagementObjectSearcher mo = new ManagementObjectSearcher("select * from Win32_SoundDevice");
foreach (ManagementObject soundDevice in mo.Get())
{
Console.WriteLine(soundDevice.GetPropertyValue("DeviceId"));
Console.WriteLine(soundDevice.GetPropertyValue("Name"));
}
With this I can get names of all audiodevices. But how to know which is used right now?
You could use NAudio
And then something like this oneliner:
string curDevName = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia).FriendlyName;
I've created this VBScript WMI script:
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objWMIService = GetObject("winmgmts:\\localhost\root\MicrosoftIISv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM IIsWebVirtualDirSetting", _
"WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
WScript.Echo "Path: " & objItem.Path
WScript.Echo
Next
Which returns the physical path (C:\inetpub\wwwroot\webapplication1) to all the applications in IIS.
Now I'm trying to use C# to populate a combobox with those values:
public static ArrayList Test2()
{
ArrayList WebSiteListArray = new ArrayList();
ConnectionOptions connection = new ConnectionOptions();
ManagementScope scope =
new ManagementScope(#"\\" + "localhost" + #"\root\MicrosoftIISV2",
connection);
scope.Connect();
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope,
new ObjectQuery("SELECT * FROM IIsWebVirtualDirSetting"), null);
ManagementObjectCollection webSites = searcher.Get();
foreach (ManagementObject webSite in webSites)
{
WebSiteListArray.Add(webSite.Path);
}
return WebSiteListArray;
}
But the output is the virtual path:
(`IIsWebVirtualDirSetting.Name="W3SVC/1/ROOT/webapplication1"`)
What needs to be changed in my query?
Note: I need to support IIS6 and .NET 4.0
Finally got it...
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\MicrosoftIISv2",
"SELECT * FROM IIsWebVirtualDirSetting");
foreach (ManagementObject queryObj in searcher.Get())
{
result.Add(queryObj["Path"]);
}
I prefer like this:
Connect at my local network server SOMEREMOTESERVER:
ConnectionOptions connection = new ConnectionOptions();
connection.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;
ManagementScope scope =
new ManagementScope(#"\\SOMEREMOTESERVER\root\MicrosoftIISV2",
connection);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM IISWebServerSetting");
var collection = new ManagementObjectSearcher(scope, query).Get();
foreach (ManagementObject item in collection)
{
var value = item.Properties["ServerBindings"].Value;
if (value is Array)
{
foreach (ManagementBaseObject a in value as Array)
{
Console.WriteLine(a["Hostname"]);
}
}
ManagementObject maObjPath = new ManagementObject(item.Scope,
new ManagementPath(
string.Format("IISWebVirtualDirSetting='{0}/root'", item["Name"])),
null);
PropertyDataCollection properties = maObjPath.Properties;
Console.WriteLine(properties["path"].Value);
Console.WriteLine(item["ServerComment"]);
Console.WriteLine(item["Name"]);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
}
I'm using this little code snippet to catch Java processes with certain parameters:
string query = "Select * From Win32_Process Where Name = 'javaw.exe'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
string cmdLine = obj.GetPropertyValue("CommandLine").ToString();
if (cmdLine.IndexOf("someapplication") != -1)
{
// ...
}
}
This code worked like a charm just a couple of days ago when I didn't have SP1 for VS2010. Now it throws a null pointer exception on line 7. I'm trying to compile for .NET Framework 2.0.
Help!? :/
if (cmdLine != null && cmdLine.IndexOf("someapplication") != -1)
It probably has less to do with SP1 and more to do with a Java update. Just check for null:
string query = "Select * From Win32_Process Where Name = 'javaw.exe'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
object cmdLineValue = obj.GetPropertyValue("CommandLine");
if(cmdLineValue != null) {
string cmdLine = cmdLineValue.ToString();
if (cmdLine.IndexOf("someapplication") != -1)
{
// ...
}
}
}
I'm using this code:
NetworkInformation.NetworkInterface[] interfaces = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
the above code retrieving only the active network connections, I need of all. How I do this?
Thanks in advance. :)
using System.Management;
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration" );
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
Console.WriteLine(mo["Description"].ToString());
}
Edit:
to find all others ["Properties"] name, change the foreach like this:
foreach (ManagementObject mo in queryCollection)
{
foreach (PropertyData pd in mo.Properties)
{
Console.WriteLine(pd.Name);
}
}