我試圖將2個參數傳遞給PowerShell腳本,該腳本調用invoke-command
,我嘗試傳入多個參數。但是當我這樣做時,似乎兩個參數都被放入一個變量中,我想知道爲什麼。在PowerShell中將多個參數放入一個變量中
這裏是兩個程序的代碼:
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
validatPath.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
這似乎是重複 http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command – Eris
不,這不是一個那個重複。這是我的代碼。我正在使用-ArguementList,它們不是。他們引用它,但他們得到錯誤。我有一個不同的問題。 – BlackHatSamurai
,但它是http://stackoverflow.com/questions/4988226/powershell-multiple-function-parameters和http://stackoverflow.com/questions/15504980/passing-parameter-to-powershell-function-not工作和可能還有更多:) –