Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (9/11/09)


Share this post:

Hey, Scripting Guy! Question

Hey Scripting Guy! I stumbled across the TechNet Script Center recently while trying to solve a problem I had at work. I had heard about scripting, of course, but had never taken the time to learn about it. I was able to find a script that resolved my issue at work, but now I am thinking I would like to learn more about this powerful tool. Is there a list that tells me everything I can do with WMI and in script? I really wish I had learned about this stuff five years ago because it would have made my life better. I do not want to go another five years before I figure this out. Thank you for creating a wonderful site.

— HS from Australia

Hey, Scripting Guy! Answer

Hello HS,

Scripting Guy Ed Wilson here. Thank you for your kind words. The Scripting Wife and I absolutely love visiting Australia. As a former network administrator and consultant, I am absolutely sold on the power of scripting and have seen firsthand the impact it can make on the life of an IT pro. There are actually two kinds of scripting that you can do these days. The first is VBScript, which has been around since the Windows 95 period, but was only installed by default beginning with Windows 2000. It is based on COM technology and is a derivative of the old BASIC language. VBScript is currently in maintenance mode, which means that even though it is still around and is installed with Windows 7 and Windows 2008 R2 server, nothing new is being added to it beyond bug fixes.

The new kid on the block is Windows PowerShell. It came out at the same time as Windows Vista and is an installable feature on Windows Server 2008. It is based upon the .NET Framework and Windows PowerShell 2.0 is installed by default on Windows 7 and Windows Server 2008 R2. It will also be an installable feature on Windows Server 2008 R2 Core Edition. All new management tools are building Windows PowerShell cmdlets: Exchange, SQL, Systems Server, Active Directory, and Group Policy to name a few. With Windows PowerShell you have both a console (shell) and a scripting language built into the same product. Therefore, you have the option of running a command-line command or writing a script.

In less than a year, it will be important for all IT pros to know Windows PowerShell. In less than three years, I would consider it mandatory knowledge. You can download and install Windows PowerShell 1.0 on Windows XP, Windows Server 2003, and Windows Vista. It is, as previously mentioned, already on the disk for Windows Server 2008. You cannot install Windows PowerShell on Windows Server 2008 Core Edition. Nevertheless, with Windows Server 2008 R2, you are able to add Windows PowerShell 2.0 as a feature.

Here are the getting started pages for both VBScript and Windows PowerShell.

To directly answer your question about WMI classes, I would download and use either the Scriptomatic 2.0 for VBScript or the PowerShell Scriptomatic for Windows PowerShell. These tools will allow you to explore the WMI namespaces and WMI classes. And here is a good book on WMI that goes over all the WMI classes and namespaces: Microsoft Windows Scripting with WMI: Self-Paced Learning Guide


Hey, Scripting Guy! Question

Hey Scripting Guy! I am attempting to create a Windows PowerShell script that will read the disk space on my computer, and write it to a Microsoft Excel spreadsheet. I am not getting too far. Can you help me?

— JK

Hey, Scripting Guy! Answer

Hello JK,

It seems that IT pros love writing stuff to Microsoft Excel spreadsheets. I have written several Hey, Scripting Guy! articles that talk about using Windows PowerShell to both read and write to Microsoft Excel. In fact, one article tracks closely with what you need to accomplish: How Can I Pull Server Diagnostic Information and Have it Saved in an Excel Spreadsheet with a Chart?. However, I asked James on the Windows PowerShell team to come up with a script just for you. Here is his response.


$a = new-Object -comobject Excel.Application
$a.visible = $True

if (Test-Path $strPath)
    # Open the document
    $b = $a.WorkBooks.Open($strPath)
    $c = $b.Worksheets.Item(1)       
} else {
    # Create It
    $b = $a.Workbooks.Add()
    $c = $b.Worksheets.Item(1)   
    $c.Cells.Item(1,1) = “freespace (GB)”
    $c.cells.item(1,2) = “date”
    $d = $c.UsedRange
    $d.Interior.ColorIndex = 19
    $d.Font.ColorIndex = 11
    $d.Font.Bold = $True

$introw = $c.UsedRange.Rows.Count + 1
$free = (Get-WMIObject Win32_LogicalDisk)[0].freespace /1GB
$c.cells.item($introw, 1) = $free
$c.cells.item($introw, 2) = get-date
$d = $c.UsedRange
$null = $d.EntireColumn.AutoFit()
if (Test-Path $strPath) {
} else {
$c = $null
$d = $null
$b = $null
$a = $null

When you run the WriteFreeSpaceToExcel.ps1 script, the Microsoft Excel spreadsheet seen in the next image is created in your Documents directory. You may need to modify the script to detect if the spreadsheet exists before attempting to call the saveas method. This depends upon the version of Microsoft Office you are using. 




Hey, Scripting Guy! Question

Hey, Scripting Guy! I love VBScript. Often I run multiple scripts at the same time. Unfortunately, when I look in Task Manager, all I see are several instances of CScript or WScript running. I would like to know which script host is related to which specific script.

— JG

Hey, Scripting Guy! Answer

Hello JG,

There are a couple of things that might help you when you attempt to track down multiple scripting hosts. One thing you can do is capture the PID number of the script you are running. Then you can use that in Task Manager. This page talks about running programs in VBScript.

The WshScriptExec object is returned by the exec method of the wshshell object. It contains a processID property. This script illustrates accessing the processID from the WshShcriptExec object.


Set objshell = CreateObject(“WScript.Shell”)
Set objExec = objshell.Exec(“wscript C:\fso\test.vbs”)
WScript.Echo objExec.processID

If you feel the need to be able to identify a particular instance of wscript.exe, use a script launcher script that records the PID. Another way to find the specific script host that is running your script is to examine the commandLine property from the Win32_Process WMI class. The GetProcessIDAndCommandLineofWscriptOrCScript.vbs script illustrates this.


On Error Resume Next
Dim strComputer
Dim objWMIService
Dim propValue
Dim objItem
Dim SWBemlocator
Dim UserName
Dim Password
Dim colItems

strComputer = “.”
UserName = “”
Password = “”
Set SWBemlocator = CreateObject(“WbemScripting.SWbemLocator”)
Set objWMIService = SWBemlocator.ConnectServer(strComputer,”root\CIMV2″,UserName,Password)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_Process where name = ‘wscript.exe’ OR name = ‘cscript.exe'”,,48)
For Each objItem in colItems
      WScript.Echo “CommandLine: ” & objItem.CommandLine
      WScript.Echo “ExecutablePath: ” & objItem.ExecutablePath
      WScript.Echo “Handle: ” & objItem.Handle
      WScript.Echo “Name: ” & objItem.Name
      WScript.Echo “ParentProcessId: ” & objItem.ParentProcessId
      WScript.Echo “ProcessId: ” & objItem.ProcessId

Hey, Scripting Guy! Question

Hey, Scripting Guy! This is a VBScript snippet and I am wondering how to translate it to Windows PowerShell. I’m furiously studying Windows Powershell resources. Could you please offer a little inspiration? Thanks very much!

Function GetTempPath
 GetTempPath = WshShell.ExpandEnvironmentStrings(“%TEMP%”) & “\”
End Function

— RK

Hey, Scripting Guy! Answer

Hello RK,

You can try the following code to return your temp directory:

Function Get-Temp
End Function

Well this draws to a conclusion another week on the TechNet Script Center. Thank you for all of the questions you’ve sent to scripter@microsoft.com. Join us next week as we answer additional scripting questions.

If you want to know exactly what we will happening in the near future on the Script Center, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on  the Official Scripting Guys Forum. Have a great weekend.

Ed Wilson and Craig Liebendorfer, Scripting Guys



Leave a comment