I have an advanced function that I created and it works great, but the only problem I seem to be having is that when I call on the function without explicitly using any parameters, the $ComputerName parameter that is set to a default of $env:COMPUTERNAME, is not even being processed by the function whatsoever.
Take the following ‘Do-SomeThing’ function example for instance:
[CmdletBinding(DefaultParameterSetName = 'Set4')] Param ( [Parameter(Position = 0)] [ValidateNotNullOrEmpty()] [Alias('CN')] $ComputerName = $env:COMPUTERNAME, [Parameter(Position = 1)] [ValidateNotNullOrEmpty()] [Alias('Cred')] [System.Management.Automation.PSCredential]$Credential, [Parameter(ParameterSetName = 'Set1', Position = 2)] [ValidateNotNullOrEmpty()] $Param1, [Parameter(ParameterSetName = 'Set2', Position = 2)] [ValidateNotNullOrEmpty()] [String[]]$Param2, [Parameter(ParameterSetName = 'Set3', Position = 2)] [ValidateNotNullOrEmpty()] $Param3, [Parameter(ParameterSetName = 'Set4', Position = 2)] [ValidateNotNullOrEmpty()] $Param4 )
Now, if I use Do-Something -ComputerName ‘MyComputer’, it works GREAT and uses the default $Param4 parameter.
However, if I just use Do-Something without any parameters at all, it does NOTHING, like the function didn’t even run at all. What I want it to do is to use the local computer name that I defined in the $ComputerName parameter as the default, but it is not doing that.
So the intention is for the function to use the local computer name and then also process $Param4 as the default parameter set along with it to get the data that I am looking for.
If I use the following test function, I see that there is no key listed for the ComputerName property and the value is listed instead.
Here is an example of what I am referring to.
Take this TestIt function for example:
function testit{ [CmdletBinding(DefaultParameterSetName = 'Set4')] Param( [Parameter(Position = 0)] [ValidateNotNullOrEmpty()] [Alias('CN')] $ComputerName = $env:COMPUTERNAME, [Parameter(Position = 1)] [ValidateNotNullOrEmpty()] [Alias('Cred')] [System.Management.Automation.PSCredential]$Credential, [Parameter(ParameterSetName = 'Set1', Position = 2)] [ValidateNotNullOrEmpty()] $Param1, [Parameter(ParameterSetName = 'Set2', Position = 2)] [ValidateNotNullOrEmpty()] [String[]]$Param2, [Parameter(ParameterSetName = 'Set3', Position = 2)] [ValidateNotNullOrEmpty()] $Param3, [Parameter(ParameterSetName = 'Set4', Position = 2)] [ValidateNotNullOrEmpty()] $Param4 ) Process{ $PSBoundParameters } } TestIt -Param1 'Testing'
If I run TestIt -Param1 ‘Testing’, I get the following output:
Key Value
Param1 Testing
Shouldn’t the ‘ComputerName’ parameter show up under the Key property and ‘MyComputer’ show up under the Value property?
When I use the following command:
TestIt -ComputerName $env:COMPUTERNAME -Param1 ‘Testing’
I get the following output:
Key Value
ComputerName MyComputer
Param1 Testing
I simply want the function to be able to use the default $env:COMPUTERNAME variable that is assigned to my ‘ComputerName’ parameter without me having to explicitly use the ‘ComputerName’ parameter in my command.
How can I go about doing this?
Should I not use Parameter Sets since it appears to be the reason behind this issue?
Thanks