none
Advanced function Pipeline parameters RRS feed

  • Question

  • I want to use a CSV file to feed the parameters of powershell cmdlet

    Role, email, fname, lname
    Admin, a@b.com, John, Smith

    I want to process a cmdlet as follows:

    import-csv myFile| mycmdlet | export-csv myresults

    I also want to be able to call the cmdlet like this

    mycmdlet -role x -email j@b.com -fname John -lname Smith

    and see a result as an object like:

    lname: "Smith"
    fname: "John"
    email: "j@b.com"
    role: "X"
    ResultData: "something else"

    I didn't want to have to do this:

    import-csv X.txt | foreach-object { mycmdlet -email $_.email } 

    In the powershell I wanted to do something line this:

    function global:test-Pipeline{
    param(  
    [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][String[]]$role, 
    [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][String[]]$email, 
    [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][String[]]$fname, 
    [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)][String[]]$lname ) 
    
    $result = new-object psobject
    
    process {
       foreach($address in $email)
       {
           Do something with each role, email, fname lname
           Add output to $result
       }
    }
    
    End {
        return $result
    }

    }

    I'm sure this must be possible, how do i do it? Can it be done without having to process the CSV in the cmdlet?



    • Edited by JD Moore Wednesday, November 14, 2018 1:46 AM
    • Moved by Bill_Stewart Thursday, March 14, 2019 8:35 PM Unanswerable
    Wednesday, November 14, 2018 1:41 AM

All replies