|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is an inventory application that scans computers in your network. You can list computers of particular types (i.e., SQL servers), and you can extract computer information from two different sources: the registry or Windows Management Instrumentation (WMI). Run netinventory.exe -? to see how it is used. Here is an example:
The command above will produce a file, NetInventory.xml - 3.3 KB, containing the operating system name and the service pack for all SQL servers in the network. BackgroundI built the tool to solve recurring problems such as:
In order to get this kind of inventory information, I needed MS System Center Configuration Manager, or some other inventory system or asset management system. This tool requires no installation, and does the job (almost) as well. Using the codeThere are five basic problems to solve in this application:
Listing computers in the network is described in another article. The class // List SQL servers in your network
NetworkBrowser nb = new NetworkBrowser();
ArrayList arr = nb.getNetworkComputers(ServerType.SV_TYPE_SQLSERVER);
Console.WriteLine("{0} : {1}", serverType.ToString(), arr.Count);
foreach (string name in arr)
{
Console.WriteLine(name);
}
Console.WriteLine();
One of the issues that need to be addressed when connecting to computers in your network is dealing with computers that don't respond. When connecting to the registry or WMI on a remote computer, you don't have control over the timeout. If a number of computers don't respond and you have to wait, say 30 seconds for each connection timeout, scanning the network will take forever. The class // Implement an impatient operation
class WaitAMinute : TimeoutOperation
{
public int counter = 0;
public WaitAMinute(int timeoutMiliseconds) : base(timeoutMiliseconds)
{
}
protected override void DoWork()
{
for (counter = 0; counter < 60; counter++)
{
if (isCancelled)
break;
Thread.Sleep(1000);
}
}
}
// Run the operation with timeout
WaitAMinute w = new WaitAMinute(3000);
w.Start();
Console.WriteLine("{0}", w.counter); // 3
Getting registry information from a remote computer is encapsulated in the class ArrayList arr = new ArrayList();
try
{
RegistryReader.GetRegistryKey("MYPC",
@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion",
null, 500, ref arr);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message); // Timed out
}
foreach (IDictionary dict in arr)
{
foreach (DictionaryEntry de in dict)
{
Console.WriteLine("{0} = {1}", de.Key, de.Value);
// Registry.ProgramFilesDir = C:\Program Files
// Registry.CommonFilesDir = C:\Program Files\Common Files
// ...
}
}
Getting WMI information from a remote computer is encapsulated in the class ArrayList arr = null;
try
{
arr = WMIQuery.Execute("select Caption, SerialNumber from Win32_BIOS",
@"root\CIMV2", "MYPC", 500);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message); // Timed out
}
foreach (IDictionary dict in arr)
{
foreach (DictionaryEntry de in dict)
{
Console.WriteLine("{0} = {1}", de.Key, de.Value);
// Win32_BIOS.Caption = KBC Version 43.1C
// Win32_BIOS.SerialNumber = CNU5510SRH
}
}
To manage command line switches, I used the classes Points of interest
History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||