PowerShell v3 Validating Parameter Sets

As I reworked one of my security module scripts I found I needed to know which parameter set was being called. Having never had to deal with this I started searching for some background information and found a couple of tidbits worth jotting down. First, this PowerShell blog post gave a concise overview of how to use the $PSBoundParameters object to accomplish this task:
Checking for bound parameters
The short, sweet version is to add this sort of test inside your function definition:
  if ($PSBoundParameters.ContainsKey(x))
  {
    write-host X Bound!
  }
If the  $PSBoundParameters object does not contain a key/value pair of the parameters you need to work with you know that the default parameter set is being used.

Another approach Chris Duck (@gpduck) clued me into via Twitter was the  $PSCmdlet object. As I researched this one, I didnt see as much, however, Chris property, $PSCmdlet.ParameterSetName, gave me exactly what I needed to look at. Going back a little further, via search engines, I was able to find this post:
Test-PSCmdlet
from the Windows PowerShell team blog. So, instead of my previous syntax, I can use the  .ParameterSetName and get information more directly. For example, this function shows the default  ParameterSetName used when none is specified,
function Demo-DefaultParameterSet
{
    param(
        $test
    )

    $PSCmdlet.ParameterSetName
}
Demo-DefaultParameterSet -test 1
Interestingly, when I run this,  I get no results. I expect to see something, in particular, the  $PSCmdlets property, ParameterSetName. On a hunch I add [CmdletBinding()] and hit paydirt:
function Demo-DefaultParameterSet
{
    [CmdletBinding()]
    param(
        $test
    )

    $PSCmdlet.ParameterSetName
}
Demo-DefaultParameterSet -test 1
__AllParameterSets
NOTE: if no Parameter sets are specified, there is still one, the __AllParameterSets parameter set. 


To get a better idea of what these two objects are all about I decide to do a little digging. Time to break out some Get-Member:
function Demo-DefaultParameterSet
{
    [CmdletBinding()]
    param(
        $test
    )

    $PSCmdlet| gm | ft -auto -wrap
    $PSBoundParameters| gm | ft -auto -wrap
}
Demo-DefaultParameterSet -test 1
When I run this I get the following:
   TypeName: System.Management.Automation.PSCompiledScriptCmdlet

Name                                MemberType Definition                                                                             
----                                ---------- ----------                                                                             
CurrentProviderLocation             Method     System.Management.Automation.PathInfo CurrentProviderLocation(string providerId)       
Dispose                             Method     System.Void Dispose()                                                                  
Equals                              Method     bool Equals(System.Object obj)                                                          
GetDynamicParameters                Method     System.Object GetDynamicParameters()                                                   
GetHashCode                         Method     int GetHashCode()                                                                       
GetResolvedProviderPathFromPSPath   Method     System.Collections.ObjectModel.Collection[string]                                      
                                               GetResolvedProviderPathFromPSPath(string path,                                         
                                               System.Management.Automation.ProviderInfo&, System.Management.Automation,              
                                               Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 provider)            
GetResourceString                   Method     string GetResourceString(string baseName, string resourceId)                           
GetType                             Method     type GetType()                                                                         
GetUnresolvedProviderPathFromPSPath Method     string GetUnresolvedProviderPathFromPSPath(string path)                                
GetVariableValue                    Method     System.Object GetVariableValue(string name), System.Object GetVariableValue(string     
                                               name, System.Object defaultValue)                                                      
Invoke                              Method     System.Collections.IEnumerable Invoke(), System.Collections.Generic.IEnumerable[T]     
                                               Invoke[T]()                                                                             
PrepareForBinding                   Method     System.Void PrepareForBinding(System.Management.Automation.SessionStateScope,          
                                               System.Management.Automation, Version=3.0.0.0, Culture=neutral,                         
                                               PublicKeyToken=31bf3856ad364e35 scope,                                                 
                                               System.Management.Automation.CommandLineParameters, System.Management.Automation,      
                                               Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 commandLineParameters)
ShouldContinue                      Method     bool ShouldContinue(string query, string caption), bool ShouldContinue(string query,   
                                               string caption, System.Boolean&, mscorlib, Version=4.0.0.0, Culture=neutral,           
                                               PublicKeyToken=b77a5c561934e089 yesToAll, System.Boolean&, mscorlib, Version=4.0.0.0,  

Related Posts by Categories

0 comments:

Post a Comment