偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Windows Powershell中的函數(shù)參數(shù)

系統(tǒng) Windows
PowerShell中的最大特點(diǎn)之一是函數(shù)使用上的可擴(kuò)展性強(qiáng)。在這篇文章中,我們將仔細(xì)看一下專(zhuān)業(yè)類(lèi)型的函數(shù):產(chǎn)品質(zhì)量函數(shù)。

在先前關(guān)于用戶(hù)自定義的Windows PowerShell的的文章中,我已經(jīng)說(shuō)過(guò)PowerShell中的最大特點(diǎn)之一是函數(shù)使用上的可擴(kuò)展性強(qiáng)。在這篇文章中,我們將仔細(xì)看一下專(zhuān)業(yè)類(lèi)型的函數(shù):產(chǎn)品質(zhì)量函數(shù)。

你問(wèn)有什么區(qū)別?產(chǎn)品質(zhì)量函數(shù)花力氣來(lái)測(cè)試輸入并在提供信息輸出的情況下為算是錯(cuò)誤進(jìn)行穩(wěn)固工作。通常當(dāng)在為產(chǎn)品運(yùn)用函數(shù)時(shí),你想知道它是否中斷-- 同時(shí)你也一定很想知道為什么。其它的語(yǔ)言需要你自己來(lái)設(shè)計(jì)參數(shù)和處理錯(cuò)誤。我們是幸運(yùn)的,Windows PowerShell有許多類(lèi)似的內(nèi)置函數(shù)。

PowerShell的參數(shù)

當(dāng)我們談?wù)揥indows PowerShell函數(shù)的時(shí)候,我們需要考慮三件事情:輸入、輸出和錯(cuò)誤。這篇文章將重點(diǎn)說(shuō)明輸入,也被稱(chēng)為參數(shù)。PowerShell有許多參數(shù)選項(xiàng),并且可以通過(guò)以下三種方式之一來(lái)進(jìn)行運(yùn)用:

位置參數(shù)

PowerShell可以創(chuàng)建一個(gè)數(shù)值數(shù)組傳遞給函數(shù)的$args變量。傳遞給函數(shù)的每一個(gè)值從0開(kāi)始被添加到這個(gè)數(shù)組中。例如:

function foo
{
Write-Host $args[0] $args[1]
}
foo "This is parameter 1" "This is parameter 2"

名字參數(shù)

PowerShell輸入的參數(shù)也可以命名,這就意味著它們可以通過(guò)名字傳遞,并且值被放置在相應(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ù)類(lèi)型,你可以使用這個(gè)屬性來(lái)強(qiáng)制用戶(hù)傳遞這種類(lèi)型的參數(shù)。如果它們沒(méi)有這樣做,PowerShell將報(bào)錯(cuò)給它們,并且強(qiáng)迫的它們提供這種類(lèi)型的值,以便函數(shù)能夠正常的運(yùn)行。例如:

function foo
{
Param(
[Parameter(Mandatory=$True)]
$param1
)
Write-Host $param1
}

ParameterSetName --我們常常需要一起傳遞一組參數(shù)(通常因?yàn)橐恍┮馔馑袛啵@?,你有一個(gè)函數(shù)要獲得一個(gè)活動(dòng)目錄對(duì)象,如果它是一個(gè)用戶(hù)或是一個(gè)計(jì)算機(jī),你就需要知道帳戶(hù):

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ù)值可以通過(guò)管道來(lái)傳遞參數(shù)。例如:

function Get-ADUserObject
{
Param(
[Parameter(ValueFromPipeline=$true)]
$User,
)
Process
{
$User
}
}
}
$ListofUsers | Get-ADUserObject

ValueFromPipelineByPropertyName -- 這個(gè)屬性似乎與ValueFromPipeline有點(diǎn)相似,但是并不是使用“類(lèi)型”,它使用的是傳入對(duì)象的屬性名稱(chēng)。例如,如果你有一個(gè)叫做UserName的用戶(hù)對(duì)象的屬性。

function Get-ADUserObject
{
Param(
[Parameter(ValueFromPipeline
ByPropertyName=$true)]
$Username,
)
Process
{
$UserName
}
}
$ListofUserObjects | Get-ADUserObject

HelpMessage -- 這允許你給用戶(hù)添加一個(gè)幫助信息。如果他們沒(méi)有指定mandatory屬性來(lái)調(diào)用你的函數(shù),這可以給他們解釋需要輸入用戶(hù)名:

function Get-ADComputerObject
{
Param(
[Parameter(Mandatory=$True,HelpMessage=
"Enter computer name.")]
$ComputerName,
)
$ComputerName
}

以上這些信息應(yīng)該能夠幫助你開(kāi)始寫(xiě)一些產(chǎn)品質(zhì)量函數(shù),但是請(qǐng)記住,這僅僅是冰山的一角。

【編輯推薦】

  1. 使用PowerShell實(shí)現(xiàn)常用網(wǎng)絡(luò)命令
  2. PowerShell基礎(chǔ)介紹
  3. 詳解Win Server2008 R2中的PowerShell
  4. 巧用PowerShell管理Win Server 2008 R2
  5. PowerShell命令輸出的控制方法
責(zé)任編輯:張浩 來(lái)源: TT中國(guó)
相關(guān)推薦

2010-12-31 14:30:35

PowerShell

2011-02-14 10:21:04

Windows PowWMI

2023-12-20 07:12:00

PowerShellCmdletNamed類(lèi)型

2024-01-03 07:57:11

高級(jí)參數(shù)PowerShellVerbose 參數(shù)

2010-12-21 14:08:50

PowerShell

2011-04-20 10:02:22

PowerShell

2012-01-16 09:18:08

虛擬化桌面虛擬化PowerShell

2012-02-01 10:32:07

PowerShellWindows 7

2016-12-05 16:09:08

Windows 10PowerShell任務(wù)

2021-12-28 00:21:29

Windows 10Windows微軟

2025-02-12 10:51:51

2021-02-22 11:48:19

Windows 10Windows微軟

2013-02-25 14:17:16

2009-07-03 08:38:44

微軟Windows 7PowerShell

2013-11-07 15:55:29

PowerShellVDI

2013-02-25 15:00:50

Windows Ser

2014-05-19 10:34:03

Windows Pow

2015-08-19 16:27:39

PowerShell更新Windows Def

2009-05-07 09:56:46

PowerShellWinForm微軟

2010-10-22 11:01:42

Windows Pow
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)