Monday, November 26, 2007

Invoke-VbScript

Note: The script has been updated, see this post.

One of the best features of PowerShell is the ability to do the same on the command line as in a script. Even Command Prompt do not support than.

But as VBScript is not dead yet - I'm currently doing more than 3000-lines of it - this script is useful as it allows you to execute VbScript interactively. It has two uses: 1) Test VBScript details without having to run a huge script 2) Use VBScript features than you do not know how to do in PowerShell.

The script Invoke-VbScript.ps1

param($vbCode,[switch]$ExecuteStatement)

function PrepareVB {

$vb=new-object -com MSScriptControl.ScriptControl
$vb.language="VBScript"
$vb
}

$vb=PrepareVB
if ($ExecuteStatement.isPresent) {
$vb.ExecuteStatement($vbcode)
}
else {
$vb.Eval($vbcode)
}



Execute command example -


PS> Invoke-VbScript -exe 'msgbox("hi")'



Evaluate expressions and return result example -


PS> $name=Invoke-VbScript 'InputBox("Enter your name","Test")'



Have fun!

No comments: