Windows Powershell中的函數(shù)參數(shù)
在先前關(guān)于用戶自定義的Windows PowerShell的的文章中,我已經(jīng)說過PowerShell中的最大特點(diǎn)之一是函數(shù)使用上的可擴(kuò)展性強(qiáng)。在這篇文章中,我們將仔細(xì)看一下專業(yè)類型的函數(shù):產(chǎn)品質(zhì)量函數(shù)。
你問有什么區(qū)別?產(chǎn)品質(zhì)量函數(shù)花力氣來測(cè)試輸入并在提供信息輸出的情況下為算是錯(cuò)誤進(jìn)行穩(wěn)固工作。通常當(dāng)在為產(chǎn)品運(yùn)用函數(shù)時(shí),你想知道它是否中斷-- 同時(shí)你也一定很想知道為什么。其它的語言需要你自己來設(shè)計(jì)參數(shù)和處理錯(cuò)誤。我們是幸運(yùn)的,Windows PowerShell有許多類似的內(nèi)置函數(shù)。
PowerShell的參數(shù)
當(dāng)我們談?wù)揥indows PowerShell函數(shù)的時(shí)候,我們需要考慮三件事情:輸入、輸出和錯(cuò)誤。這篇文章將重點(diǎn)說明輸入,也被稱為參數(shù)。PowerShell有許多參數(shù)選項(xiàng),并且可以通過以下三種方式之一來進(jìn)行運(yùn)用:
位置參數(shù)
PowerShell可以創(chuàng)建一個(gè)數(shù)值數(shù)組傳遞給函數(shù)的$args變量。傳遞給函數(shù)的每一個(gè)值從0開始被添加到這個(gè)數(shù)組中。例如:
function foo { Write-Host $args[0] $args[1] } foo "This is parameter 1" "This is parameter 2"
名字參數(shù)
PowerShell輸入的參數(shù)也可以命名,這就意味著它們可以通過名字傳遞,并且值被放置在相應(yīng)的變量里。例如(注意當(dāng)這個(gè)函數(shù)被調(diào)用的時(shí)候,參數(shù)顛倒,但是數(shù)值能正確的返回):
Example (notice the parameters are reversed when the function is called, but the values are returned correctly): function foo { Param($param1,$param2) Write-Host $param1 $param2 } foo -param2 "This is parameter 2" -param1 "This is parameter 1"
Splatting參數(shù)
在PowerShell的參數(shù)傳遞中,這個(gè)或許是最常用的方法。它包含創(chuàng)建一個(gè)數(shù)組或哈希表作為傳遞給函數(shù)的參數(shù)組。這個(gè)讓你可以動(dòng)態(tài)地創(chuàng)建整個(gè)腳本的參數(shù),然后當(dāng)你準(zhǔn)備好后即可調(diào)用函數(shù)。例如:
function foo { Param($param1,$param2) Write-Host $param1 $param2 } Create Hash table $blah = @{"Param1"="This is parameter 1"; "Param2"="This is parameter 2"} # Pass hash table to function foo @Blah
PowerShell 參數(shù)的屬性
Mandatory – 這個(gè)屬性在PowerShell參數(shù)選項(xiàng)里是默認(rèn)的,但是如果你知道你所需要的參數(shù)類型,你可以使用這個(gè)屬性來強(qiáng)制用戶傳遞這種類型的參數(shù)。如果它們沒有這樣做,PowerShell將報(bào)錯(cuò)給它們,并且強(qiáng)迫的它們提供這種類型的值,以便函數(shù)能夠正常的運(yùn)行。例如:
function foo { Param( [Parameter(Mandatory=$True)] $param1 ) Write-Host $param1 }
ParameterSetName --我們常常需要一起傳遞一組參數(shù)(通常因?yàn)橐恍┮馔馑袛啵?。例如,你有一個(gè)函數(shù)要獲得一個(gè)活動(dòng)目錄對(duì)象,如果它是一個(gè)用戶或是一個(gè)計(jì)算機(jī),你就需要知道帳戶:
function Get-ADObject { Param( [Parameter(Mandatory=$True, ParameterSetName="User")] $User, [Parameter(Mandatory=$True, ParameterSetName="Computer")] $Computer ) $PScmdlet.ParameterSetName } Get-ADObject --# This will throw an error because no parameters passed Get-ADObject –user "joe" # Will return 'User' Get-ADObject –Computer "joe" # Will return 'Computer' Get-ADObject –User "joe" –Computer "joe" # Will return an error
ValueFromPipeline -- 這個(gè)屬性告訴函數(shù)某個(gè)特定的參數(shù)值可以通過管道來傳遞參數(shù)。例如:
function Get-ADUserObject { Param( [Parameter(ValueFromPipeline=$true)] $User, ) Process { $User } } } $ListofUsers | Get-ADUserObject
ValueFromPipelineByPropertyName -- 這個(gè)屬性似乎與ValueFromPipeline有點(diǎn)相似,但是并不是使用“類型”,它使用的是傳入對(duì)象的屬性名稱。例如,如果你有一個(gè)叫做UserName的用戶對(duì)象的屬性。
function Get-ADUserObject { Param( [Parameter(ValueFromPipeline ByPropertyName=$true)] $Username, ) Process { $UserName } } $ListofUserObjects | Get-ADUserObject
HelpMessage -- 這允許你給用戶添加一個(gè)幫助信息。如果他們沒有指定mandatory屬性來調(diào)用你的函數(shù),這可以給他們解釋需要輸入用戶名:
function Get-ADComputerObject { Param( [Parameter(Mandatory=$True,HelpMessage= "Enter computer name.")] $ComputerName, ) $ComputerName }
以上這些信息應(yīng)該能夠幫助你開始寫一些產(chǎn)品質(zhì)量函數(shù),但是請(qǐng)記住,這僅僅是冰山的一角。
【編輯推薦】