我正在尝试将 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