Modify passed in function parameter value

Welcome Forums General PowerShell Q&A Modify passed in function parameter value

This topic contains 7 replies, has 4 voices, and was last updated by

 
Participant
10 months ago.

  • Author
    Posts
  • #91501

    Participant
    Points: 0
    Rank: Member

    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

  • #91516

    Participant
    Points: 22
    Rank: Member

    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"

  • #91523

    Participant
    Points: 59
    Rank: Member

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

  • #91525

    Participant
    Points: 0
    Rank: Member

    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.

    • #91534

      Participant
      Points: 135
      Helping Hand
      Rank: Participant

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

    • #91537

      Participant
      Points: 0
      Rank: Member

      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

    • #91540

      Participant
      Points: 59
      Rank: Member

      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
      
    • #91541

      Participant
      Points: 0
      Rank: Member

      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

The topic ‘Modify passed in function parameter value’ is closed to new replies.