Piping to a cmdlet parameter which wont accept pipeline

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of A K P A K P 2 years, 11 months ago.

  • Author
    Posts
  • #15902
    Profile photo of A K P
    A K P
    Participant

    Hi All,

    Was trying to do this..

    $TsProfilePath = \\Server\Share\'$User\TS\Profile

    import-csv $PathtoCSV | foreach-object {get-qaduser $_.Name | set-qaduser -TsProfilePath '$TsProfilePath'}

    I initially had profile path in the csv file but the cmdlet doesnot not accept pipeline for this paramater which for me as a new student to powershell I have not yet solved.

    If anyone could shine some light to resolve this currnet problem.

    BTW – I am trying to figure out how to do inception based powershell i.e. pipe to a variable within a variable
    Any deeper and I might not make it out!

    Thanks

    Cheers,
    AKP

  • #15904
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You're not actually piping Import-Csv to anything other than ForEach-Object here, so you could put the path in the CSV file if you like. That would look something like this:

    Import-Csv $PathtoCSV |
    ForEach-Object {
        $csvRecord = $_
    
        Get-QADUser $csvRecord.Name |
        Set-QADUser -TsProfilePath $csvRecord.TSProfilePath
    }
    

    This code assumes that the column in your CSV is named TSProfilePath; you'd need to change the argument to Set-QADUser if it's something different.

    I'm not sure what you mean by "pipe to a variable within a variable".

  • #15906
    Profile photo of A K P
    A K P
    Participant

    Thanks Dave –

    Please ignore all I said about variable within variables.

  • #15908
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    Sometimes piping data into ForEach-Object, and then piping further within the foreach will fail catastrophically. For example, doing


    Get-Mailbox | ForEach-Object {
    $_ | Get-MailboxStatistics
    }

    will break with the following error:


    Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], PSInvalidOperationEx
    ception
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed

    For this reason, and because piping is actually quite slow when dealing with larger quantities of data, and it makes code look like Perl (Perl isn't bad, it's just ugly), I've found myself using it less and less in favor of saving data in variables and then using a proper foreach($item in $list) { } structure.

  • #15956
    Profile photo of A K P
    A K P
    Participant

    100% agreed Martin.
    Still lots to learn, but it is $fun

You must be logged in to reply to this topic.