2013-07-12 80 views
0

我試圖將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 
+0

這似乎是重複 http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command – Eris

+1

不,這不是一個那個重複。這是我的代碼。我正在使用-ArguementList,它們不是。他們引用它,但他們得到錯誤。我有一個不同的問題。 – BlackHatSamurai

+0

,但它是http://stackoverflow.com/questions/4988226/powershell-multiple-function-parameters和http://stackoverflow.com/questions/15504980/passing-parameter-to-powershell-function-not工作和可能還有更多:) –

回答

2

PowerShell功能的參數在被調用時不應將離子放在括號內。它應該是ValidatePath $filename $user。你寫的結果只用一個參數調用ValidatePath,而$ filename是兩個值的數組,即文件名和用戶。

順便說一句:當在PowerShell中調用.Net方法時,您確實需要括號。 :)

+0

工作。謝謝:)我會接受一次,我可以。 – BlackHatSamurai

相關問題