September 7, 2012

How to use ParameterSets in a CmdLet ?

Here’s a quick sample :

function test-param 
{ 
[CmdletBinding(DefaultParametersetName="p2")] 
param( 
[Parameter(ParameterSetName="p1",Position=0)] 
[String] 
$d, 

[Parameter(ParameterSetName="p2", Position=0)] 
[String]$i 
) 
    switch ($PsCmdlet.ParameterSetName) 
    { 
    "p1"  { Write-Host ([DateTime]$d); break} 
    "p2"  { Write-Host ([INT]$i); break} 
    } 
}

For the complete explanation : PowerShell V2: ParameterSets From Jeffrey Snover [MSFT] @ Windows PowerShell Blog

No comments:

Post a Comment