locked
Passing pipeline variable to functioin RRS feed

  • Question

  • Hey Scripting Guy,

    How do I pass a pipeline variable to another function?

    Example:

    I have a .csv file with a list of users First and Last name. I need to get the users sAMAccountName and pass it to another function.

    Here is my code:

    $intakelist = Get-Content c:\userlist.csv

    foreach($name in $intakelist){

    Get-QADuser $name -searchroot $search -SearchScopt subtree -IncludedProperties sAMAccountName | .\get-someotherfunctioin.ps1 $_

    }

    When I try this code it calls the get-someotherfunction with an empty variable.

    Friday, August 15, 2014 1:36 PM

Answers

  • Hey Scripting Guy,

    How do I pass a pipeline variable to another function?

    Example:

    I have a .csv file with a list of users First and Last name. I need to get the users sAMAccountName and pass it to another function.

    Here is my code:

    $intakelist = Get-Content c:\userlist.csv

    foreach($name in $intakelist){

    Get-QADuser $name -searchroot $search -SearchScopt subtree -IncludedProperties sAMAccountName | .\get-someotherfunctioin.ps1 $_

    }

    When I try this code it calls the get-someotherfunction with an empty variable.

    Your get-somotherfuction.ps1 appears to be written to take the value as a positional parameter, rather than as pipeline input.  You can make that work with foreach-object:

    $intakelist = Get-Content c:\userlist.csv
    
    foreach($name in $intakelist){
    
    Get-QADuser $name -searchroot $search -SearchScope subtree -IncludedProperties sAMAccountName |
     foreach-object { .\get-someotherfunctioin.ps1 $_ }
    
    }

    I think is likely the script is expecting a value type like sAMAccountName rather than the full object.  If that's the case:

    $intakelist = Get-Content c:\userlist.csv
    
    foreach($name in $intakelist){
    
    Get-QADuser $name -searchroot $search -SearchScope subtree -IncludedProperties sAMAccountName |
     foreach-object { .\get-someotherfunctioin.ps1 $_.SamAccountName }
    
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Friday, August 15, 2014 2:10 PM
    • Proposed as answer by Mike Laughlin Friday, August 15, 2014 3:36 PM
    • Marked as answer by mjolinor Friday, August 15, 2014 6:30 PM
    Friday, August 15, 2014 2:06 PM

All replies

  • Hi,

    Try explicitly specifying the SamAccountName property:

    $_.SamAccountName

    Or perhaps something like this:

    (Get-QADuser $name -searchroot $search -SearchScopt subtree -IncludedProperties sAMAccountName).SamAccountName | .\get-someotherfunctioin.ps1 $_


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Friday, August 15, 2014 1:41 PM
  • Hey Scripting Guy,

    How do I pass a pipeline variable to another function?

    Example:

    I have a .csv file with a list of users First and Last name. I need to get the users sAMAccountName and pass it to another function.

    Here is my code:

    $intakelist = Get-Content c:\userlist.csv

    foreach($name in $intakelist){

    Get-QADuser $name -searchroot $search -SearchScopt subtree -IncludedProperties sAMAccountName | .\get-someotherfunctioin.ps1 $_

    }

    When I try this code it calls the get-someotherfunction with an empty variable.

    Your get-somotherfuction.ps1 appears to be written to take the value as a positional parameter, rather than as pipeline input.  You can make that work with foreach-object:

    $intakelist = Get-Content c:\userlist.csv
    
    foreach($name in $intakelist){
    
    Get-QADuser $name -searchroot $search -SearchScope subtree -IncludedProperties sAMAccountName |
     foreach-object { .\get-someotherfunctioin.ps1 $_ }
    
    }

    I think is likely the script is expecting a value type like sAMAccountName rather than the full object.  If that's the case:

    $intakelist = Get-Content c:\userlist.csv
    
    foreach($name in $intakelist){
    
    Get-QADuser $name -searchroot $search -SearchScope subtree -IncludedProperties sAMAccountName |
     foreach-object { .\get-someotherfunctioin.ps1 $_.SamAccountName }
    
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Friday, August 15, 2014 2:10 PM
    • Proposed as answer by Mike Laughlin Friday, August 15, 2014 3:36 PM
    • Marked as answer by mjolinor Friday, August 15, 2014 6:30 PM
    Friday, August 15, 2014 2:06 PM
  • Thank you

    foreach-object { .\get-someotherfunctioin.ps1 $_.SamAccountName }
    Worked Perfect!!

    Friday, August 15, 2014 3:23 PM
  • Welcome!

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, August 15, 2014 6:30 PM