Practical use of Active Scripting: WMI
Short list of WMI tasks:
- BIOS information
- Display configuration
- Video adapter configuration
- Serial ports information
- CPU information for each processor
- Disk drives information
- Physical memory information
- User accounts
- Daylight Saving Time
- Kill the specified program
- Logoff current user on any WMI enabled computer
- Ethernet adapters’ link speed
- List printers with status and number of printjobs, or pause or resume printing on the specified printer(s), or flush all printjobs, or list all printers, their status and number of printjobs
- Windows Registry
- Reboot/Shut down any WMI enabled computer on the network
- Services
- Startup commands (Startup folder and registry Run)
- Synchronize your computer’s system time with any webserver
- Uptime for any WMI enabled computer
- and many others
With Active Scripting any of above task is just few lines of code. For example, all disk drives information:
wmi.Connect()
wmi.ExecQuery('Select * from Win32_DiskDrive')
LOOP i=1 TO wmi.items.Count()
CLEAR(DriveQ)
DriveQ:Caption = wmi.items.GetProp(i, 'Caption')
DriveQ:Description = wmi.items.GetProp(i, 'Description')
DriveQ:Manufacturer = wmi.items.GetProp(i, 'Manufacturer')
DriveQ:Model = wmi.items.GetProp(i, 'Model')
DriveQ:Name = wmi.items.GetProp(i, 'Name')
DriveQ:Partitions = wmi.items.GetProp(i, 'Partitions')
DriveQ:Size = wmi.items.GetProp(i, 'Size')
DriveQ:Status = wmi.items.GetProp(i, 'Status')
DriveQ:SystemName = wmi.items.GetProp(i, 'SystemName')
DriveQ:TotalCylinders = wmi.items.GetProp(i, 'TotalCylinders')
DriveQ:TotalHeads = wmi.items.GetProp(i, 'TotalHeads')
DriveQ:TotalSectors = wmi.items.GetProp(i, 'TotalSectors')
DriveQ:TotalTracks = wmi.items.GetProp(i, 'TotalTracks')
DriveQ:TracksPerCylinder = wmi.items.GetProp(i, 'TracksPerCylinder')
ADD(DriveQ)
END
How to terminate running process:
wmi.Connect()
!- find all processes with specific caption
wmi.ExecQuery(printf('Select * from Win32_Process WHERE Caption=%S', pProcess))
!- terminate each process
LOOP i=1 TO wmi.items.Count()
wmi.items.CallMethod(i, 'Terminate')
END
How to reboot specific computer:
!- required "Shutdown" privelege
wmi.Connect(pMachineName, 'Shutdown')
!- find primary OS
wmi.ExecQuery('SELECT * FROM Win32_OperatingSystem WHERE Primary=True')
!- reboot
wmi.items.CallMethod(1, 'Reboot()')
IF wmi.ErrNumber()
MESSAGE(wmi.ErrDescription())
END
Querying the amount of memory a particular process uses:
wmi.Connect()
wmi.ExecQuery(printf('SELECT * FROM Win32_Process WHERE Name = %S', pProcess))
LOOP i=1 TO wmi.items.Count()
CLEAR(MemQ)
MemQ:Amount = wmi.items.GetProp(1, 'WorkingSetSize')
ADD(MemQ)
END