PowerShell實現(xiàn)參數(shù)互斥示例
在PowerShell的函數(shù)中如果想讓參數(shù)互斥,可以使用 “ParameterSetName”屬性將一個parameter屬性定義在不同的參數(shù)或著參數(shù)集合中。
很多人可能沒有留意,在PowerShell的函數(shù)中,我們可以將多個參數(shù)屬性定義在同一個參數(shù)名上,這樣配合mandatory可以讓一個參數(shù)在某個場景下是強制的,在另外的場景下則是可選的。
function Test-ParameterSet { [CmdletBinding(DefaultParameterSetName='NonCredential')] param ( $id, [Parameter(ParameterSetName='LocalOnly', Mandatory=$false)] $LocalAction, [Parameter(ParameterSetName='Credential', Mandatory=$true)] [Parameter(ParameterSetName='NonCredential', Mandatory=$false)] $ComputerName, [Parameter(ParameterSetName='Credential', Mandatory=$false)] $Credential ) $PSCmdlet.ParameterSetName $PSBoundParameters if ($PSBoundParameters.ContainsKey('ComputerName')) { Write-Warning '遠程調(diào)用' } }
上面的函數(shù)Test-ParameterSet 將演示在參數(shù)”NonCredential”激活時, -ComputerName為可選參數(shù)。而當(dāng)你使用了 -Credential 參數(shù)時, -ComputerName 就變成了強制參數(shù)。而當(dāng)你使用了 -LocalAction 參數(shù)以后,-ComputerName和-Credential均可有可無。
相關(guān)文章
Windows Powershell IF-ELSEIF-ELSE 語句
作為條件判斷,if語句是各大語言都在使用的,當(dāng)然powershell也不例外,今天我們就來看下IF-ELSEIF-ELSE 語句2014-10-10PowerShell查看本機文件關(guān)聯(lián)程序和默認打開程序的方法
這篇文章主要介紹了PowerShell查看本機文件關(guān)聯(lián)程序和默認打開程序的方法,本文給出了查看方法,同時給出了一份讀取結(jié)果,需要的朋友可以參考下2015-06-06