Tuesday, February 7, 2012

Calling vbScript via PowerShell

Following is an example of how to call a vbScript from PowerShell. I recently had to do something similar to this for a Postini SafeSender list conversion to Exchange 2007/AD.

First the vbScript:
Saved as CallFromPowerShell.vbs

Option Explicit
Dim strComputer, objWMI, OS

strComputer = WSH.Arguments(0)

On Error Resume Next
Set objWMI=GetObject("winmgmts://" & strComputer).InstancesOf("win32_operatingsystem")
If objWMI is nothing Then
WScript.Echo "Unable to connect to " & strComputer
For Each OS In objWMI
wscript.Echo OS.Caption
end If

To call this from PowerShell:

$computers = 'fatbeard-vp01','fatbeard-vp02','fatbeard-vp03'          
$computers |
foreach {"{0,20}`t{1}" -f $_,$(cscript.exe //nologo c:\temp\callfrompowershell.vbs $_) }

fatbeard-vp01 Microsoft Windows 7 Enterprise
fatbeard-vp02 Microsoft(R) Windows(R) Server 2003, Enterprise Edition
fatbeard-vp03 Unable to connect to fatbeard-vp03

No comments: