我正在尝试使用invoke-command. 该方法的一部分包括一个ScriptBlock参数,我觉得我没有正确地做某事。
首先,我尝试在脚本中创建一个方法,如下所示:
param([string] $filename)
function ValidatePath( $file, $fileType = "container" )
{
    $fileExist = $null
    if( -not (test-path $file -PathType $fileType) )
    {               
        throw "The path $file does not exist!"
        $fileExist = false
    }
    else
    {
        echo $filename found!
        $fileExist = true
    }
    return $fileExist
}
$responseObject = Invoke-Command -ComputerName MININT-OU9K10R
    -ScriptBlock{validatePath($filename)} -AsJob
$result = Receive-Job -id $responseObject.Id
echo $result
要调用这个,我会这样做.\myScriptName.ps1  -filename C:\file\to\test。该脚本将执行,但不会调用该函数。
然后我想也许我应该把这个函数放到一个新的脚本中。这看起来像:
文件 1:
$responseObject = Invoke-Command -ComputerName MININT-OU9K10R -ScriptBlock {
  .\file2.ps1 -filename C:\something } -AsJob
$result = Receive-Job -id $responseObject.Id
echo $result
文件 2:
Param([string] $filename)
这些方法都不会执行该功能,我想知道为什么;或者,我需要做些什么才能让它发挥作用。
function ValidatePath( $file, $fileType = "container" )
{
    $fileExist = $null
    if( -not (test-path $file -PathType $fileType) )
    {               
        throw "The path $file does not exist!"
        $fileExist = false
    }
    else
    {
        echo $filename found!
        $fileExist = true
    }
    return $fileExist
}