wmi interview questions
Top wmi frequently asked interview questions
I have an application written in C# that needs to be able to configure the network adapters in Windows. I have this basically working through WMI, but there are a couple of things I don't like about that solution: sometimes the settings don't seem to stick, and when the network cable is not plugged in, errors are returned from the WMI methods, so I can't tell if they really succeeded or not.
I need to be able to configure all of the settings available through the network connections - Properties - TCP/IP screens.
What's the best way to do this?
Source: (StackOverflow)
I would like to know when a interface has been disabled.
If I go into the windows manager and disable one of the 2 enabled connections, GetIfTable() only returns status about 1 interface, it no longer sees the disconnected one.
(Returns 1 table)
How can I get something to return that the disabled interface still exists but is currently disabled?
Thanks.
http://msdn.microsoft.com/en-us/library/aa365943%28VS.85%29.aspx
Source: (StackOverflow)
We are using the following function to get the number of processors specified by the current boot configuration. This number is used purely for logging.
The function below works fine on XP, Vista, 7, 2003 and 2008. It, however, fails on the Windows 2012 Server.
// -1 = not implemented or not allowed
// 0 = not limited
// >0 = number of processors in the {current} boot entry
function Internal_GetBCDNumberOfProcessors: integer;
var
objBcdStore : OleVariant;
objElement : OleVariant;
objWBL : OleVariant;
objWMIService: OleVariant;
begin
// for more info, see: http://stackoverflow.com/questions/7517965/accessing-bcdstore-from-delphi/7527164#7527164
Result := -1;
try
objWMIService := GetObject('winmgmts:{(Backup,Restore)}\\.\root\wmi:BcdStore');
if (not VarIsNull(objWMIService)) and
boolean(objWMIService.OpenStore('', objBcdStore)) and
(not VarIsNull(objBcdStore)) and
boolean(objBcdStore.OpenObject('{fa926493-6f1c-4193-a414-58f0b2456d1e}', objWBL)) and
(not VarIsNull(objWBL))
then
if objWBL.GetElement($25000061, objElement) and //<-- fails here on Server 2012
(not VarIsNull(objElement))
then
Result := StrToIntDef(objElement.Integer, 0)
else
Result := 0;
except
on E: EOleSysError do
Result := -1;
end;
end;
If I try to run it on Win2012, the objWBL.GetElement
raises EOleSysError
exception with text OLE error D0000225
. Google doesn't find anything meaningful related to this error code :(
Stack trace says that the exception is triggered in System.Win.ComObj.DispatchInvokeError which is called by the DispatchInvoke which is called by the VarDispInvoke.
All this was reproduced using XE2. I could try to repeat the problem with XE3 but I don't believe Delphi RTL has anything to do with it.
Does anybody has any idea about possible reasons for this behaviour?
Source: (StackOverflow)
How can I get the NetBIOS (aka 'short') domain name of the current computer from PowerShell?
$ENV:USERDOMAIN displays the domain of the current user, but I want the domain that the current machine is a member of.
I've discovered you can do it pretty easily in VBScript, but apparently ADSystemInfo isn't very nice to use in PowerShell.
Update
Here's my final solution incorporating the suggestion of using Win32_NTDomain, but filtering to the current machine's domain
$wmiDomain = Get-WmiObject Win32_NTDomain -Filter "DnsForestName = '$( (Get-WmiObject Win32_ComputerSystem).Domain)'"
$domain = $wmiDomain.DomainName
Source: (StackOverflow)
My project requires detection of a specific device when it is connected to USB. The only way I can identify this device is by its description/device name, not the com port. What I have found to perform the correct function is using a WMI query and checking the name property:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from WIN32_SerialPort");
foreach (ManagementObject port in searcher.Get())
{
deviceName = (string)foundPort.GetPropertyValue("Name");
...
I initially tested this by connecting my phone, and the query returned the phone found on COM3 as expected. Then, I connected another device (a USB to serial converter, which more closely resembles the device I need this project for) and the query simply did not find it. It only finds the phone. This device does, however, show up on port COM4 in Device Manager. To spite me even more, the SerialPort class finds both devices, but it does not provide the information I need to identify the device:
string[] tempPorts = SerialPort.GetPortNames();
I have read numerous threads on SO and elsewhere and cannot find a satisfactory solution. Could someone please clarify why the WIN32_SerialPort query does not find my other device? Is it not considered a win32 serial port for some reason?
And, could someone please point me in the direction of a solution to this problem?
Source: (StackOverflow)
How to check current machine type? laptop or desktop ?
I got this from http://blog.csdn.net/antimatterworld/archive/2007/11/11/1878710.aspx ,it works well on my home machine(Win2003 on laptop), it returns "Portable", but failed on my work machine(Vista on laptop), it returns "Other".
here is the code:
public enum ChassisTypes
{
Other = 1,
Unknown,
Desktop,
LowProfileDesktop,
PizzaBox,
MiniTower,
Tower,
Portable,
Laptop,
Notebook,
Handheld,
DockingStation,
AllInOne,
SubNotebook,
SpaceSaving,
LunchBox,
MainSystemChassis,
ExpansionChassis,
SubChassis,
BusExpansionChassis,
PeripheralChassis,
StorageChassis,
RackMountChassis,
SealedCasePC
}
public static ChassisTypes GetCurrentChassisType()
{
ManagementClass systemEnclosures = new ManagementClass("Win32_SystemEnclosure");
foreach (ManagementObject obj in systemEnclosures.GetInstances())
{
foreach (int i in (UInt16[ ])(obj["ChassisTypes"]))
{
if (i > 0 && i < 25)
{
return (ChassisTypes)i;
}
}
}
return ChassisTypes.Unknown;
}
Source: (StackOverflow)
I found a code on net and have been trying to get more information about mo[].
I am trying to get all the information contained in ManagementObjectCollection.
Since parameter in mo is looking for an string value which I dont know, how can I get all the values without knowing its parameter values. Or if I want to get all the indexer values related to mo in ManagementObjectCollection
ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
foreach( ManagementObject mo in osDetailsCollection )
{
_osName = mo["name"].ToString();// what other fields are there other than name
_osVesion = mo["version"].ToString();
_loginName = mo["csname"].ToString();
}
Source: (StackOverflow)
I have a Delphi app that regularly writes to a local disk file. Occasionally it is unable to access the file - a sharing violation results when it tries to open it. A retry after a short delay is all that is needed, but when it occurs, I would like to report the process that prevented the access.
Is it feasible when a sharing violation occurs for my program to enumerate all the file handles in use, inspect the filename, and if it matches the name of my data file, retrieves the process name associated with that handle?
Some example code would be nice.
Source: (StackOverflow)
I am interfacing with a USB-to-serial port that can be inserted or removed at any time. I've found that I can use WMI (particularly with the use of WMI Code Creator) to query for device changes in the PC.
In the generated snippet below, the Win32_DeviceChangeEvent is subscribed to. However, this event doesn't reveal which device (e.g. USB, serial port, etc) caused the event. Is there a way to only receive notifications when serial ports are inserted or removed?
To clarify, the point of the code is not to detect opening/closing of serial ports, it is to detect whether a new port has been added to the machine or a previous port was removed.
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class WMIReceiveEvent
{
public WMIReceiveEvent()
{
try
{
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM Win32_DeviceChangeEvent");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
Console.WriteLine("Waiting for an event...");
watcher.EventArrived +=
new EventArrivedEventHandler(
HandleEvent);
// Start listening for events
watcher.Start();
// Do something while waiting for events
System.Threading.Thread.Sleep(10000);
// Stop listening for events
watcher.Stop();
return;
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
}
}
private void HandleEvent(object sender,
EventArrivedEventArgs e)
{
Console.WriteLine("Win32_DeviceChangeEvent event occurred.");
}
public static void Main()
{
WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
return;
}
}
}
Source: (StackOverflow)
I'm working on a program that queries three different servers in order to get CPU and LogicalDisk information.
Each server I query returns me values in 6 to 15 seconds (depending on the server). So it takes a total of 31 seconds to get all my values (15 sec for the first server, 6 for the second and 10 for the third).
I tried to multi thread each query, it reduced the execution time of 1 second for each server, so I don't think it's the solution.
I also tried to run queries directly with powershell in servers:
First server : it took 10 seconds (instead of 15) to retrieve informations
Second server : it took 10 seconds (like when I do it remotely) to retrieve informations
Third server ) it took ~1 second (instead of 6)
Here are my queries:
SELECT LoadPercentage From WIN32_Processor
SELECT Size, FreeSpace From WIN32_LogicalDisk
My Question is: is there something to do on my servers to make queries easier ?
I already tried to desactivate the firewall and the antivirus.
PS: I'm querying Windows 2003 R2 server, Win XP pro and Win 7 server, each in the same domain as my local computer.
Source: (StackOverflow)
I'm currently working on a deployment script that will take my site, export it from svn, remove any testing files etc in it, minify the javascript/css, copy the code to a remote web server, and then switch the physical path of the existing site to the new directory.
So far I have everything working except for switching the physical directory in IIS.
$IIsServer = Get-WmiObject Site -Namespace "root/WebAdministration" -ComputerName $serverIP -Credential $credentials -Authentication PacketPrivacy
$site = $IIsServer | Where-Object {$_.Name -eq $siteName}
When I look into the values I have I cant find the physical path property.
Any suggestions would be greatly appreciated.
Source: (StackOverflow)