Power of Windows Management Instrumentation (WMI)

Power of Windows Management Instrumentation (WMI)

Allot of new SysAdmins are getting carried away with PowerShell Scripting.. For me the big difference between PowerShell and Old School forms of Scripting is that. 1:) PowerShell has a eco-system for  integration with other Application like vSphere. The disadvantage is that Powershell needs to be available on the Windows Server and these Plugins need to be installed and Remoting is a pain and not straight forward.

If your tasks is to perform work on multiply windows servers, WMI is the best option and Powershell really is a overkill.

In this blog, I am going to document the various scripts that SysAdmin can use day to day:

WMI uses Distributed Component Object Model (DCOM) and Windows Remote Management (WimRM).  DCOM is the most powerful part of Windows.


Remote Example Script

@echo off

for /f %%I in (serverlist.txt) do call :run %%I
goto :end
echo %1
wmic /node:%1 service where StartMode="Auto" list brief /format:htable > \\##\share\#%1_report.txt
goto :eof

Const ForReading = 1, ForWriting = 2, ForAppending = 3

Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set DeviceListFile = FileSystemObject.OpenTextFile(CurrentDirectory & "\DeviceList.txt", ForReading)

Do Until DeviceListFile.AtEndOfStream
 Devicename = DeviceListFile.ReadLine
 If Trim(Devicename) <> "" Then
 Set dtmStartDate = CreateObject("WbemScripting.SWbemDateTime")
 dtmStartDate.SetVarDate dateadd("n", -10, now)' CONVERT_TO_LOCAL_TIME

Set winmgmts = GetObject("winmgmts:\\" & Devicename & "\root\cimv2")
 Set Win32_OperatingSystem = winmgmts.ExecQuery ("Select * from Win32_OperatingSystem")
 For each Instance in Win32_OperatingSystem
 Wscript.Echo Devicename & ", " & UtcDateToString(Instance.LastBootUpTime)
 End if

Function UtcDateToString(UtcFormattedDate)
 UtcDateToString = CDate(Mid(UtcFormattedDate, 5, 2) & "/" & Mid(UtcFormattedDate, 7, 2) & "/" & Left(UtcFormattedDate, 4) & " " & Mid (UtcFormattedDate, 9, 2) & ":" & Mid(UtcFormattedDate, 11, 2) & ":" & Mid(UtcFormattedDate, 13, 2))
End Function

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s