PowerShell小技巧之同時使用可選強制參數(shù)
更新時間:2014年09月11日 11:24:17 投稿:hebedich
本文主要講訴了在腳本函數(shù)中讓可選參數(shù)和強制參數(shù)必須同時使用,有需要的朋友可以參考下。
在下面腳本函數(shù)中讓可選參數(shù)和強制參數(shù)必須同時使用。
下面演示當可選參數(shù)出現(xiàn),也必須使用這個強制參數(shù)。
function Connect-Somewhere
{
[CmdletBinding(DefaultParameterSetName='A')]
param
(
[Parameter(ParameterSetName='A',Mandatory=$false)]
[Parameter(ParameterSetName='B',Mandatory=$true)]
$ComputerName,
[Parameter(ParameterSetName='B',Mandatory=$false)]
$Credential
)
$chosen = $PSCmdlet.ParameterSetName
"You have chosen $chosen parameter set."
}
# -Computername is optional
Connect-Somewhere
# here, -Computername is mandatory
Connect-Somewhere -Credential test
支持所有PS版本
相關(guān)文章
PowerShell中使用Out-String命令把對象轉(zhuǎn)換成字符串輸出的例子
這篇文章主要介紹了PowerShell中使用Out-String命令把對象轉(zhuǎn)換成字符串輸出的例子,即把對象轉(zhuǎn)為字符串的方法,需要的朋友可以參考下2014-08-08
PowerShell函數(shù)參數(shù)設(shè)置成自動識別數(shù)據(jù)類型的例子
這篇文章主要介紹了PowerShell函數(shù)參數(shù)設(shè)置成自動識別數(shù)據(jù)類型的例子,這個方法可以很方便的使用參數(shù)集,而不用每次指定名稱,需要的朋友可以參考下2014-07-07
Windows Powershell創(chuàng)建對象
.Net類型中的方法功能很強大??梢酝ㄟ^類型的構(gòu)造函數(shù)創(chuàng)建新的對象,也可以將已存在的對象轉(zhuǎn)換成指定的類型。2014-10-10
PowerShell統(tǒng)計文件夾下文件個數(shù)的方法
這篇文章主要介紹了PowerShell統(tǒng)計文件夾下文件個數(shù)的方法,本文介紹了2種訪求實現(xiàn)這個需求,需要的朋友可以參考下2014-08-08
Powershell讀取本機注冊表中的所有軟件關(guān)聯(lián)擴展名
這篇文章主要介紹了Powershell讀取本機注冊表中的所有軟件關(guān)聯(lián)擴展名,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03

