I may have struck upon a seriously powerful nugget tonight. \:\)

I'll be the first one to admit that I despise the syntax of Powershell, but there are those that think Kixtart is dying and PS is the future. To each his own opinion I guess. Anyway, tonight I was fumbling around looking for god knows what and found the following article:

Invoking PowerShell from VBScript (COM)
http://blogs.msdn.com/b/powershell/archive/2008/07/25/invoking-powershell-from-vbscript-com.aspx

See the instructions for download here: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=213343#Post213343
This leads you to Sapien.com... where you can download the ActiveX Powershell for free after giving them your email address. http://www.sapien.com/auth Then click "Downloads" and then click "Free Components", then click "ActiveXPosh", then Download.

The Object supports these methods and properties.
  • ClearOutput()
  • Execute()
  • Eval()
  • GetValue()
  • Int()
  • IsPowerShellInstalled()
  • Output()
  • OutputString()
  • OutputMode
  • OutputWidth

After I downloaded the exe, I installed it. I don't know if it was because I have a 64bit OS or not, but I had to copy the dll to the system32 directory, and the use regasm in the .net folder to register it.

Thankfully I hadn't gotten rid of that book I bought on Powershell (although the dust was piling up on it). So I started trying things...

First I made a UDF to initialize PS with it's constants.
Function PSInit()
Global $OUTPUT_CONSOLE, $OUTPUT_WINDOW, $OUTPUT_BUFFER
$OUTPUT_CONSOLE = 0
$OUTPUT_WINDOW = 1
$OUTPUT_BUFFER = 2
$PSInit = CreateObject("SAPIEN.ActiveXPoSH")
EndFunction


(As I was testing I realized you must turn on "NoVarsinStrings" and "NoMacrosinStrings")

And here is just some of the things I came up with...

Hello World
break on
$RC=setoption("NoVarsInStrings","on")
$RC=setoption("NoMacrosInStrings","on")
$RC=setoption("WrapATEOL","on")


dim $ps


$PS=PSInit()
if $PS.init(not 0)
if $PS.IsPowerShellInstalled
$PS.outputmode = $output_buffer
? $PS.getvalue('"hello world".substring(0,5)')
$PS.execute('$test = "hello world"')
? $PS.getvalue("$test.substring(6,5)")
endif
else
? "Init Failed"
endif



Contents of a Dir command
break on
$RC=setoption("NoVarsInStrings","on")
$RC=setoption("NoMacrosInStrings","on")
$RC=setoption("WrapATEOL","on")


dim $ps


$PS=PSInit()
if $PS.init(not 0)
if $PS.IsPowerShellInstalled
$PS.outputmode = $output_buffer
$PS.Execute('Get-ChildItem -path "d:\temp"')
;$PS.Execute('dir "d:\temp"') ; same as command above
? $PS.outputstring
endif
else
? "Init Failed"
endif


Something called a Hashtable
break on
$RC=setoption("NoVarsInStrings","on")
$RC=setoption("NoMacrosInStrings","on")
$RC=setoption("WrapATEOL","on")


dim $ps


$PS=PSInit()
if $PS.init(not 0)
if $PS.IsPowerShellInstalled
$PS.outputmode = $output_buffer
$PS.execute('$user=@{FirstName="John";LastName="Smith";PhoneNumber="555-1212"}')
? $PS.getvalue("$user.lastname")
? $PS.getvalue("$user.PhoneNumber")
endif
else
? "Init Failed"
endif


Finally, using I think .Net to create a form...
break on
$RC=setoption("NoVarsInStrings","on")
$RC=setoption("NoMacrosInStrings","on")
$RC=setoption("WrapATEOL","on")


dim $ps


$PS=PSInit()
if $PS.init(not 0)
if $PS.IsPowerShellInstalled
$PS.outputmode = $output_buffer
$PS.execute('[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")')
$PS.execute('$form=new-object Windows.forms.form')
$PS.execute('$form.text="My First Form"')
$PS.execute('$button=new-object Windows.Forms.Button')
$PS.execute('$button.text="Push Me"')
$PS.execute('$button.dock="fill"')
$PS.execute('$button.add_click({form.close()})')
$PS.execute('$form.controls.add($button)')
$PS.execute('$form.add_shown({$form.Activate()})')
$PS.execute('$form.showdialog()')
endif
else
? "Init Failed"
endif