Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Guy's Scripting Ezine 34 - Scriptomatic
This week's secret is a utility called Scriptomatic. Here is a wonderful program from Microsoft which will extend your scripting techniques. What Scriptomatic does is to help you analyse and
dissect objects such as users, computers, or even the operating system itself.
Scriptomatic has those twin hallmarks of a great utility, it automates repetitive tasks, and teaches you more about scripting methods. Most of all, Scriptomatic saves you having to grapple with that
tricky WMI syntax What you see there in the interface are all the VBScript commands laid out perfectly; all that remains is for you click 'Run'. Merely inspecting that list of options is bound to
generate ideas for your own scripts. As ever, I will give you examples to get you started.
Scriptomatic is an .HTA file which connects to the operating system and then displays information on Win32 'classes' such as, UserAccount, DiskDrive or even the BIOS. HTA files are a cross between
HTML and VBScript; if you wish, you can inspect Scriptomatic.hta with Notepad or your favourite HTML editor.
Once you have obtained a free copy of Scriptomatic, just double click to execute. As the .HTA file runs, you briefly see a dialog box with 'Loading WMI Classes'. This means that the script uses the Windows
Management Interface to interrogate the operating Win32 classes. In plain English, Scriptomatic gives you a handle on objects such as; user, disk or network adapter.
When you have selected which class to investigate, you can either, click 'Run' and instantly see the output in a 'Dos Box', or else save to a .vbs file and run that script to produce a WScript message box.
The good news about Scriptomatic is that it outputs just about every property that can be scripted. The bad news is that you become swamped with data. So the key is to be ruthless and delete unwanted lines or
elements before you run the script.
Beware: If you run the raw .vbs scripts without editing, then you will be overwhelmed with data, so get ready with Task Manager, Processes (Tab), wscript.exe (Image Name) right click - end process.
Or else you may have to press the enter key 20,000 times. (So I exaggerate, but a run-away script can be a tiresome.)
Warning: For once, I would not, repeat NOT, run the Example 1 script. Just admire its methods, then compare with my modifications in example 2. If you do run Example 1, then get ready
with Task Manager - End Process.
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount",,48)
For Each objItem in colItems
Wscript.Echo "AccountType: " & objItem.AccountType
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Disabled: " & objItem.Disabled
Wscript.Echo "Domain: " & objItem.Domain
Wscript.Echo "FullName: " & objItem.FullName
Wscript.Echo "InstallDate: " & objItem.InstallDate
Wscript.Echo "LocalAccount: " & objItem.LocalAccount
Wscript.Echo "Lockout: " & objItem.Lockout
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "PasswordChangeable: " & objItem.PasswordChangeable
Wscript.Echo "PasswordExpires: " & objItem.PasswordExpires
Wscript.Echo "PasswordRequired: " & objItem.PasswordRequired
Wscript.Echo "SID: " & objItem.SID
Wscript.Echo "SIDType: " & objItem.SIDType
Wscript.Echo "Status: " & objItem.Status
Next
Here below, is the script that I do recommend you test.
Whilst Scriptomatic is fantastic, it benefits from fine tuning. My main idea is to produce one WScript message with 17 entries in one box, rather than 17 boxes each with a one line entry.
Modifications in Example 2
& VBcr This joins or concatenates a carriage return. The result is a new line for each item.
_ (Underscore) this tell the script there are more statements on the next line, note the use of ampersands (&).
Tip. What I did was a Find and Replace. Find = WScript.Echo, Replace = & VBCr _ &. Then I made adjustments to the end of line with carriage returns and deletes.
I love the construction: If Then... End if, here I use the 'If Then command' to filter just the administrator, rather than repeating the message box for each of my 500 users. If objItem.name =
"Administrator" Then
...
' ScriptomaticAdmin.vbs - Enumerates the Administrator's properties
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.2 - June 20th 2004
' -------------------------------------------------------------'
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount",,48)
For Each objItem in colItems
If objItem.name = "Administrator" Then
WScript.Echo "AccountType: " & objItem.AccountType & VBCr _
& "Caption: " & objItem.Caption & VBCr _
& "Description: " & objItem.Description & VBCr _
& "Disabled: " & objItem.Disabled & VBCr _
& "Domain: " & objItem.Domain & VBCr _
& "FullName: " & objItem.FullName & VBCr _
& "InstallDate: " & objItem.InstallDate & VBCr _
& "LocalAccount: " & objItem.LocalAccount & VBCr _
& "Lockout: " & objItem.Lockout & VBCr _
& "Name: " & objItem.Name & VBCr _
& "PasswordChangeable: " & objItem.PasswordChangeable & VBCr _
& "PasswordExpires: " & objItem.PasswordExpires & VBCr _
& "PasswordRequired: " & objItem.PasswordRequired & VBCr _
& "SID: " & objItem.SID & VBCr _
& "SIDType: " & objItem.SIDType & VBCr _
& "Status: " & objItem.Status
End If
Next
Learning Points
Note 1: strComputer "." means THIS COMPUTER.
Note 2: Here is where Scriptomatic earns its corn and connects to Windows Management Interface. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Note 3: the 'col' in colItem means collection.
Note 4: objItem.Domain The word after the full stop is a property, for example .domain .fullname or .installdate.
Note 5: Select * from Win32_UserAccount. In another script, for a different purpose, Scriptomatic could change this line to: Win32_Bios or Win32_DiskDrive.
Note 6: An old friend 'GetObject' and a new method 'ExecQuery'
Further modifications
Cut out unwanted lines. For Example delete: & "InstallDate: " & objItem.InstallDate & VBCr _
An obvious modification would be to change Administrator to another user.
Note: The last line has a different ending (there is no & VBCr _)
Different tactics.
I have merely scratched the surface, given you one example of a familiar object - a 'user account'. There are zillions of other classes that you could have selected within Scriptomatic's drop down
box. More often than not, the raw Scriptomatic code can be improved by replacing some of the echo statements with VBCr.
Scriptomatic is a 'must' utility for building up your collection of VBScripts. Learn about the objects, methods and syntax of VBScript while you explore the WMI classes.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|