在PowerShell中运行JS时出错

时间:2016-09-01 09:28:06

标签: javascript powershell scriptcontrol

所以我试图使用ScriptControl在powershell中运行这个javascript函数。

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);

我收到了这个错误:

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

关于最新情况的任何想法?

0 个答案:

没有答案