WMI scripting example

A couple of years a go I wrote a script for a customer to monitor his backups. The advertorial of the backup tool advertised that it was monitorable with WMI – Windows Management instrumentation. The customer had already an installation of Nagios, the thing he wanted was a plugin to produce a warning if the backups failed current day, and an error if it failed two days in a row.

Because the nagios backup-plugin is a bit too specific, I made a simple WMI scripting example in Visual Basic Script that lists the current printers installed.

WMI works with paths that contain classes which you can reference from VBS. The easiest way to find a path to a WMI class is with an WMI explorer, for example WMI Explorer by CodePlex – https://wmie.codeplex.com/.

Screenshot WMI explorer
     Screenshot WMI explorer

The object for listing the printers is ‘Win32_Printer’. All WMI objects have API reference documentation in the MSDN - Microsoft Developer Network. The reference for this object can be found @ https://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

With the wmi explorer, you can see that the path is – \\TRATZ5V5\ROOT\CIMV2 – and with the API reference we know what properties are available. With this information the vbscript kan be written,


'' GetObject() returns SWbemServicesEx - https://msdn.microsoft.com/en-us/library/aa393854(v=vs.85).aspx
'' the computername (TRATZV5) can be replaced by a dot, to use the current system
Set objService = GetObject("winmgmts:\\.\root\cimv2") '' ExecQuery() returns SWbemObjectSet - https://msdn.microsoft.com/en-us/library/aa393762(v=vs.85).aspx
Set items = objService.ExecQuery("Select * from Win32_Printer") '' show printernames
For Each objItem in items
WScript.Echo "Printer name: " + objItem.DeviceID
Next '' Example of showing only the first listed printer
If items.Count > 0 Then
Script.Echo "Name of first printer: " & items.ItemIndex(0).DeviceID
End If


The script can be started from the dos prompt in console mode with cscript,

        cscript //nologo listprinters.vbs


Win32 mode,

        wscript listprinters.vbs



Gerelateerd

27-06-2016 Java JNI and Windows messages
13-06-2016 Introduction to Angular 2, a hello world example
- Bent u opzoek naar een Php of Java programmeur voor uw website of applicatie? (freelance / detachering)
- Losse tickets, opdrachten, of gehele projecten in de planning?
- Systeembeheer van Linux of Windows Server ?

Dan kom ik graag met u in contact! Meer informatie over mij vindt u hier.
Sitemap | Op alle producten & diensten zijn de algemene voorwaarden van toepassing
Php programmeur | Maatwerk software Alkmaar | Maatwerk software Heerhugowaard | Maatwerk software Purmerend | Maatwerk software Zaandam | Software laten maken | Freelance php programmeur Afbouw maatwerk software Blogs