Passing string value

Tagged: 

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 weeks, 1 day ago.

  • Author
    Posts
  • #58331
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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
    Profile photo of Ron
    Ron
    Participant

    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
    Profile photo of Tony Antony
    Tony Antony
    Participant

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

  • #58402
    Profile photo of Rob Simmers
    Rob Simmers
    Participant
    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
    

You must be logged in to reply to this topic.