0

我正在尝试将 2 个参数传递给调用的 PowerShell 脚本,invoke-command并且我正在尝试传递多个参数。但是当我这样做时,似乎两个参数都放入了一个变量中,我想知道为什么。

这是两个程序的代码:

POC.ps1:

param($filename, $user)

echo $filename
echo "This"
echo $user

$responseObject = Invoke-Command CAPTESTPK01 -FilePath .\validatePath.ps1  -ArgumentList($filename, $user) -AsJob 




while($responseObject.State -ne "Completed")
{

}

$result = Receive-Job -Id $responseObject.Id -Keep
echo $result

验证路径.ps1:

Param([string] $filename,
      [string] $user)

function ValidatePath( $filename, $user, $fileType = "container" )
{
    Write-Host "This is the file name: $filename"
    echo "This is user: $user"
    $fileExist = $null
    if( -not (test-path $filename -PathType $fileType) )
    {               
        throw "$user, the path $filename does not exist!"

    }
    else
    {
         Write-Host "This is the second part"
         echo $filename found!
    }
     Write-Host "This is the third part"
    return $fileExist
}


try
{

    ValidatePath($filename, $user)
}
catch
{
    $e = $_.Exception
    echo $e
}

这是输出:

C:\Users
This
Blaine
This is the file name: C:\Users Blaine <--- This indicated both arguments are in one variable
This is user: 
This is the second part
This is the third part
C:\Users
Blaine
4

1 回答 1

2

PowerShell 函数的参数在调用时不应放在括号中。应该是ValidatePath $filename $user。你所写的结果是调用 ValidatePath 时只有一个参数,而 $filename 是一个包含两个值的数组,即文件名和用户。

顺便说一句:在 PowerShell 中调用 .Net 方法时,您确实需要括号。:)

于 2013-07-12T20:47:26.207 回答