Author Posts

January 15, 2018 at 4:11 pm

Hi,

I am wondering if there is an existing parameter attribute I can add to my function which will take the passed in value and make it lowercase. Here is an example function.

function Set-LowerCase {
  param(
    [string]
    $Name
  )
  $Name
}


Set-LowerCase -Name "HI"

Expected Output: "hi"

Now I understand that this can be easily done by updating the variable within the script of the function, but if there is a predefined parameter attribute that I can add to the param then I would like to use it instead.

P.S. – I have looked at MFST documentation and also tried a few methods myself to try and figure this out, but unfortunately I did not have any success.

Thanks in advance!
-Michael

January 15, 2018 at 5:22 pm

Hi Michael,

Since you are defining your parameter as a [String], you can just use the

ToLower()

method.

function Set-LowerCase {
param(
[string]$Name
)
$Name.ToLower()
}

Set-LowerCase -Name "HI"

Output: "hi"

January 15, 2018 at 6:34 pm

The function seems redundant in view of the existing .ToLower() method of the String data type.
Any value will the function add to .ToLower() ?

January 15, 2018 at 7:34 pm

Hi, Shane. Yes I agree with you completely and I am aware of this available method. However, I was looking for an Parameter attribute that may be available to me to do this. What you suggested is what I do today.

Hi Sam, I have a function that accepts a parameter which ultimately is used as information to invoke an API. However, the value that gets passed must be in lower case otherwise the API call doesn't return anything. I was just wondering if there was an Parameter Attribute that I could use to control the param value in case someone else changes this down the road.

January 15, 2018 at 8:32 pm

Did you try to find something fitting from the help About Functions Advanced Parameters.
ValidatePattern or ValidateScript could be something for you.

January 15, 2018 at 8:59 pm

Hi Olaf,

Yes I have. I thought about using both. However, I prefer to have people type in upper or lowercase node name and not have to worry about how they might copy and paste from another location.

Thank you for your considerations.

-Michael

January 15, 2018 at 9:17 pm

The point remains, within your function, you'd use .ToLower() method to ensure input is lower case as opposed to calling a function to so the same..

function Get-Martians {

    [CmdletBinding(ConfirmImpact='Low')] 
    Param(
        [Parameter(Mandatory=$false)][String]$USERNAME = $env:USERNAME
    )

    Begin { }

    Process {
        $USERNAME = $USERNAME.ToLower() # ensures value is lower case
        $USERNAME
    }

    End { }
}

and sample output:

Get-Martians 'hElLo ThErE'
hello there

January 15, 2018 at 9:33 pm

Sam,

Yes. I think that will have to be the method (which I already use) to keep since there is not a Parameter Attribute that I am aware of that will do this.

-Michael