0

所以我正在尝试使用 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

关于发生了什么的任何想法?

4

0 回答 0