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.

Post a Comment