Checking for bound parametersThe short, sweet version is to add this sort of test inside your function definition:
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.if ($PSBoundParameters.ContainsKey(x))
{
write-host X Bound!
}
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-PSCmdletfrom 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,
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{param($test)$PSCmdlet.ParameterSetName}Demo-DefaultParameterSet -test 1
NOTE: if no Parameter sets are specified, there is still one, the __AllParameterSets parameter set.function Demo-DefaultParameterSet{[CmdletBinding()]param($test)$PSCmdlet.ParameterSetName}Demo-DefaultParameterSet -test 1__AllParameterSets
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:
When I run this I get the following:function Demo-DefaultParameterSet{[CmdletBinding()]param($test)$PSCmdlet| gm | ft -auto -wrap$PSBoundParameters| gm | ft -auto -wrap}Demo-DefaultParameterSet -test 1
TypeName: System.Management.Automation.PSCompiledScriptCmdletName 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(stringname, 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 Post:
0 comments:
Post a Comment