Passing string value

Welcome Forums General PowerShell Q&A Passing string value

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

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #58331

    Participant
    Points: 0
    Rank: Member

    Hello,

    I have the function below that finds out what type of form is from a CSV. We have Transfer, New Hire, and Deletion forms from HR.

    How would I pass, Transfer for example?

    function Type_Of_Form
    {    
        param ([string]$Args)
        $Employees_CSV."Type of Form".GetValue[$People] -eq $Args
    }
  • #58334
    Ron

    Participant
    Points: 0
    Rank: Member

    Not positive I understand the question, but, you've used a reserved word for the argument variable.

    Try this and see if it applies to your problem.

    function Type_Of_Form
    {    
        param ([string]$FType)
        "test" -eq $FType
    }
    
    Type_of_Form("test")
    Type_of_Form("nottest")
    
  • #58340

    Participant
    Points: 0
    Rank: Member

    Thank you, I didn't know $Args was a reserved word. I'll try another and test

  • #58402

    Participant
    Points: 0
    Rank: Member
    function Get-HRForm  {    
        param (
        [ValidateSet("Transfer", "New Hire", "Deletion")]
        [string]$Type = "Transfer"
        )
        
        "Processing CSV as {0}" -f $Type
    }
    

    Output:

    Get-HRForm
    
    Processing CSV as Transfer
    
    Get-HRForm -Type 'New Hire'
    
    Processing CSV as New Hire
    
    Get-HRForm -Type 'Foo'
    
    Get-HRForm : Cannot validate argument on parameter 'Type'. The argument "Foo" does not belong to the set "Transfer,New Hire,Deletion" specified by the ValidateSet 
    attribute. Supply an argument that is in the set and then try the command again.
    At line:11 char:18
    + Get-HRForm -Type 'Foo'
    +                  ~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-HRForm], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-HRForm
    

The topic ‘Passing string value’ is closed to new replies.