PowerShell小技巧之同時使用可選強(qiáng)制參數(shù)
在下面腳本函數(shù)中讓可選參數(shù)和強(qiáng)制參數(shù)必須同時使用。
下面演示當(dāng)可選參數(shù)出現(xiàn),也必須使用這個強(qiáng)制參數(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-08PowerShell函數(shù)參數(shù)設(shè)置成自動識別數(shù)據(jù)類型的例子
這篇文章主要介紹了PowerShell函數(shù)參數(shù)設(shè)置成自動識別數(shù)據(jù)類型的例子,這個方法可以很方便的使用參數(shù)集,而不用每次指定名稱,需要的朋友可以參考下2014-07-07Windows Powershell創(chuàng)建對象
.Net類型中的方法功能很強(qiáng)大??梢酝ㄟ^類型的構(gòu)造函數(shù)創(chuàng)建新的對象,也可以將已存在的對象轉(zhuǎn)換成指定的類型。2014-10-10PowerShell統(tǒng)計文件夾下文件個數(shù)的方法
這篇文章主要介紹了PowerShell統(tǒng)計文件夾下文件個數(shù)的方法,本文介紹了2種訪求實現(xiàn)這個需求,需要的朋友可以參考下2014-08-08Windows Powershell 復(fù)制數(shù)組
這篇文章主要介紹了Windows Powershell 復(fù)制數(shù)組 ,需要的朋友可以參考下2014-09-09Powershell讀取本機(jī)注冊表中的所有軟件關(guān)聯(lián)擴(kuò)展名
這篇文章主要介紹了Powershell讀取本機(jī)注冊表中的所有軟件關(guān)聯(lián)擴(kuò)展名,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03