欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PowerShell函數(shù)實現(xiàn)類似重載功能實例

 更新時間:2014年07月31日 10:00:48   作者:洪哥  
這篇文章主要介紹了PowerShell函數(shù)實現(xiàn)類似重載功能實例,PowerShell函數(shù)是不支持重載的,本文介紹的是類似功能,需要的朋友可以參考下

本文介紹PowerShell自定義函數(shù)是否支持重載,如果支持,如何重載?如果不支持,如何實現(xiàn)與重載相似的效果?

鄭重聲明:PowerShell自定義函數(shù)不支持重載!也就是說,你不能定義兩個同名的PowerShell函數(shù),不管參數(shù)個數(shù)、順序、類型是否相同。既然PowerShell自定義函數(shù)不支持重載功能,那么有沒有什么辦法來實現(xiàn)與重載相似的效果呢?有,當(dāng)然有,那就是參數(shù)集(Parameters Set)

PowerShell自定義函數(shù)的參數(shù)集是可以為一個函數(shù)定義一個參數(shù)集,在調(diào)用函數(shù)時可以從參數(shù)集中選擇一個參數(shù)進(jìn)行使用。注意,只能從參數(shù)集中選擇一個來使用。先看看這個示例,對參數(shù)集好有一個感性的認(rèn)識。

復(fù)制代碼 代碼如下:

function Add-User
{
    [CmdletBinding(DefaultParameterSetName='A')]
    param
    (
        [Parameter(ParameterSetName='A',Mandatory=$true)]
        $Name,
        [Parameter(ParameterSetName='B',Mandatory=$true)]
        $SAMAccountName,
        [Parameter(ParameterSetName='C',Mandatory=$true)]
        $DN
    )
    $chosen = $PSCmdlet.ParameterSetName
    “You have chosen $chosen parameter set.”
}

上面Add-User函數(shù)定義了一個參數(shù)集,參數(shù)集中有三個參數(shù):Name、SAMAccountName、DN,可以選擇其中任何一個使用。但Add-User函數(shù)只能傳一個參數(shù)。
復(fù)制代碼 代碼如下:
PS> Add-User -Name test
You have chosen A parameter set.
PS> Add-User -SAMAccountName test
You have chosen B parameter set.
PS> Add-User -DN test
You have chosen C parameter set.
PS> Add-User -DN test -Name test
Add-User : Parameter set cannot be resolved using the specified named parameters.

洪哥再舉個例子,我們要做一個函數(shù),想通過新聞ID或新聞標(biāo)題來輸出新聞的內(nèi)容。那么應(yīng)該怎么實現(xiàn)呢?
復(fù)制代碼 代碼如下:

function Get-NewsContent
{
    [CmdletBinding(DefaultParameterSetName='A')]
    [Parameter(ParameterSetName='A',Mandatory=$true)]
    $NewsID,
    [Parameter(ParameterSetName='B',Mandatory=$true)]
    $NewsTitle
   
    $chosen = $PSCmdlet.ParameterSetName
    If($chosen -eq "A"){
        "News Content by NewsID"
    }else{
        "News Content by NewsTitle"
    }
}

關(guān)于PowerShell函數(shù)支持重載嗎,本文就介紹這么多,希望對您有所幫助,謝謝!

相關(guān)文章

  • 使用PowerShell修改注冊表

    使用PowerShell修改注冊表

    本文通過具體的實例給大家具體講解了使用PowerShell操作注冊表的方法,非常的簡單實用,有需要的小伙伴可以參考下。
    2015-09-09
  • Windows Powershell 自定義控制臺

    Windows Powershell 自定義控制臺

    這篇文章主要介紹了Windows Powershell 自定義控制臺,包括選項、字體、布局和顏色四個方面的自定義風(fēng)格,希望對大家有所幫助
    2014-08-08
  • PowerShell多線程執(zhí)行前后臺作業(yè)的例子

    PowerShell多線程執(zhí)行前后臺作業(yè)的例子

    使用后臺作業(yè)執(zhí)行多個任務(wù)從先前的技巧中看不是非常高效,它在處理每個后臺作業(yè)返回結(jié)果時將會浪費很多性能。一個更有效的方法是使用進(jìn)程內(nèi)的任務(wù)。他能分別單獨的執(zhí)行任務(wù)與Powershell類似,所以它不是按順序返回值的
    2014-04-04
  • Windows Powershell Foreach 循環(huán)

    Windows Powershell Foreach 循環(huán)

    Foreach-object 為cmdlet命令,使用在管道中,對管道結(jié)果逐個處理,foreach為遍歷集合的關(guān)鍵字。
    2014-10-10
  • Powershell互斥參數(shù)使用實例

    Powershell互斥參數(shù)使用實例

    這篇文章主要介紹了Powershell互斥參數(shù)使用實例,本文給出了兩個代碼示例來講解互斥參數(shù)的使用,需要的朋友可以參考下
    2015-06-06
  • Windows Powershell 介紹和安裝

    Windows Powershell 介紹和安裝

    Powershell 是運行在windows機(jī)器上實現(xiàn)系統(tǒng)和應(yīng)用程序管理自動化的命令行腳本環(huán)境。微軟之所以將Powershell 定位為Power,并不是夸大其詞,因為它完全支持對象。其可讀性,易用性,可以位居當(dāng)前所有shell之首。
    2014-08-08
  • PowerShell腳本實現(xiàn)添加、修改任務(wù)計劃的例子

    PowerShell腳本實現(xiàn)添加、修改任務(wù)計劃的例子

    這篇文章主要介紹了PowerShell腳本實現(xiàn)添加、修改任務(wù)計劃的例子,PowerShell操作、設(shè)置任務(wù)計劃實例,需要的朋友可以參考下
    2014-08-08
  • 探索PowerShell(十) 循環(huán)語句介紹

    探索PowerShell(十) 循環(huán)語句介紹

    本節(jié)所要討論的內(nèi)容的實質(zhì)更多的偏向于程序設(shè)計方面,所以在此不做過多詳細(xì)講解,只針對PowerShell中的應(yīng)用進(jìn)行具體講解
    2012-12-12
  • PowerShell腳本開發(fā)之收發(fā)UDP消息包

    PowerShell腳本開發(fā)之收發(fā)UDP消息包

    上篇文章我們介紹了使用PowerShell收發(fā)TCP消息包,今天我們來介紹下使用Powershell收發(fā)UDP小細(xì)胞的方法
    2014-10-10
  • PowerShell獲取Windows用戶列表、用戶信息的方法

    PowerShell獲取Windows用戶列表、用戶信息的方法

    這篇文章主要介紹了PowerShell獲取Windows用戶列表、用戶信息的方法,一個簡單的入門例子,需要的朋友可以參考下
    2014-08-08

最新評論