Thursday, May 22, 2008

Invoke-Vbscript - now supporting Vista x64

As mentioned in Is this PowerShell Session a 32-bit or a 64-bit?, Invoke-Vbscript was the reason I ventured out into that.

 

Here is the updated Invoke-Vbscript. It checks the architecture using Get-Architecture from Is this PowerShell Session a 32-bit or a 64-bit?. If it is 64-bit, it invokes the 32-bit powershell to run the script using a method described in Invoking PowerShell with complex expressions using Scriptblocks.

The script -

param($vbCode,[switch]$ExecuteStatement)

if ($args) {throw "Unknown arguments: $args"}

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

if ((Get-Architecture -CurrentProcess) -eq 64) {
# Get path for this script
$me=$myinvocation.mycommand.path
# Build command as string to substitute values
$sb = " &'$me' -vbcode $vbcode -ExecuteStatement:`$$ExecuteStatement"
# Encode for safe command line passing
$encoded=[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($sb))
# Invoke powershell 32-bit
&"$env:windir\syswow64\windowspowershell\v$($host.version.major).$($host.version.minor)\powershell.exe" -noprofile -EncodedCommand $encoded
return
}

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

No comments: