WMI

Advertise Here

,

Examples of using VBS with WMI and stuff.

Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "X:", "\\atl-fs-01\public"

someX = InputBox("type some:")
someY = InputBox("again:")
someZ = someX + someY
Wscript.Echo someZ

use “cint” to force a string into int.

A var value can be empty or “Null”. Empty simply means not assigned, and can become 0 or empty string automatically. Null is special. Use “IsNull()” to check if a value is Null.

x = Null
y = 1
If IsNull(x) Then
    x = 2
End If

Wscript.Echo x+y
mydate = mydate & Now & VbCrLf
mydate = mydate & Date & VbCrLf
mydate = mydate & Time & VbCrLf
Wscript.Echo mydate
' example of date
DateArray = Array("6/1/2002", "June 1, 2002",  "6", "6/1") 
For Each dtmDate in DateArray 
    If IsDate(dtmDate) = 0 Then 
        Wscript.Echo dtmDate & " is not a valid date." 
    Else 
        Wscript.Echo dtmDate & " is a valid date." 
    End If 
Next

Some examples using WMI and ADSI.

Following, a script that backs up and clears all the event logs on a computer:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
        & strComputer & "\root\cimv2")
Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile")
For Each objLogfile in colLogFiles
    strBackupLog = objLogFile.BackupEventLog _
        ("c:\scripts\" & objLogFile.LogFileName & ".evt")
    objLogFile.ClearEventLog()
Next

Following, a script that returns the names of all the services installed on a computer:

strComputer =  "."
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
For Each objItem in colItems
    Wscript.Echo objItem.Name
Next

Following, returns the names of all the processes currently running.

strComputer =  "."
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objItem in colItems
    Wscript.Echo objItem.Name
Next

A a WMI script that retrieves and then displays the name of the BIOS installed on the computer. This script is written in VBScript.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\ " _
    & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_BIOS")
For Each objItem in colItems
    Wscript.Echo objItem.Name
Next
blog comments powered by Disqus
2009-07