Friday, May 27, 2011

Stupid Geek Tricks: PowerShell, VBscript, Jscript

When I’m bored on a Friday night and none of my twitter followers are bored enough to suggest something for me to do, I do stupid crap like this:

PowerShell script that calls Cscript and a VBS script, which invokes Javascript and returns the result up the pipeline back to PowerShell.  Enjoy.

PowerShell code (test1.ps1):

$answer = cscript.exe /nologo .\test1.vbs
"Cosine of 33 is $answer"


VBscript code (test1.vbs):



Function Cosine(numValue)
Set sc = CreateObject("ScriptControl")
sc.Language = "jscript"
result = sc.Eval("Math.cos(" & numValue & ")")
sc = 0
Cosine = result
End Function

x = Cosine(33)
wscript.echo x


Drop both files in the same folder and run the PowerShell script to spank the VBscript into submission.  Yes, I know it's not passing object handles around, but rather just passing strings around but who cares. It's Friday and I'm bored out of my skull. I have no life. I hope you do.

2 comments:

Doug Finke said...

It is cool how you can interop all those. Here is the 1 liner in pure PowerShell:

"Cosine of 33 is $([Math]::Cos(33))"

skatterbrainz said...

Thanks Doug. I know it can be accomplished all in PowerShell, but I simply wanted to demonstrate calling across other languages and getting a result piped back. I've posted examples of doing it using object interfaces (rather than string pipes) using vbscript, javascript and kixtart. Like I said though: I was very bored. :)