Getting errors running JS in powershell

265 Views Asked by At

So I'm trying to run this javascript function in powershell using ScriptControl.

function Create-Script()
{
  param([string]$code = $null);
  if ( $code ) {
    $sc = New-Object -ComObject ScriptControl;
    $sc.Language = "JScript";
    $sc.AddCode($code);
    $sc.CodeObject;
  }
}

$jscode = @"
function jslen(s) {
  return s.length;
}
"@

$js = Create-Script $jscode;
$str = "abcd";
$contents = $js.jslen($str);

And I get this error:

You cannot call a method on a null-valued expression.At line:21 char:1
+ $js.jslen($str);
+ ~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Any ideas on whats going on?

0

There are 0 best solutions below