Take Pipeline input write directly to pipeline output












0














I want to execute / chain several PowerShell cmdlets. Initial input from a CSV. There may be more values in the CSV than the initial command needs but they may be needed further down the chain. Like this:



 CSV
email, fname, lname, clubNo, permission
A@email.com, John, Smith, 12, R
B@email.com, Jean, Smith, 12, R
C@email.com, Jack, Smith, 12, R


Then chain



 import-csv file.txt | new-user -env Dev | set-role | export-csv result.txt


new-user only needs email, env
set-role needs email, clubNo, env, and perm



I thought my new-user cmdlet should look something like this but its not working:



function global:new-user {
param(
[Parameter(ValueFromPipelineByPropertyName = $true, mandatory = $true)][validateset('DEV', 'QA', 'PT', 'PLT', 'SIT', 'APIS', 'PD', 'Sandbox')][STRING]$env,
[Parameter( mandatory = $True, ValueFromPipelineByPropertyName = $true)][ValidateScript({ValidateEmail($_)})][String]$Email,
[String]$userKey="xyz",
[String]$secret="abc"
)

Begin {
$dc = "domain.com"
$auth = "env=$env&userkey=$userKey&secret=$secret"
$record = New-Object psobject
$record | add-member env $env
}

process {
$result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
If (-not $result.errorCode ) {
$record | Add-Member uid $result.uid
}
$record | Add-Member ErrorCode $result.errorCode -Force
}

end {
Write-Output $rescord
}


}



But I'm only seeing one record on the outbound pipeline










share|improve this question
























  • I think you just need this: import-csv file.txt | foreach { do what you need to do } | export-csv result.txt
    – Mike Twc
    Nov 17 '18 at 17:03










  • I modified my original to more reflect what I'm actually reflect what my code looks like
    – James Moore
    Nov 18 '18 at 14:11
















0














I want to execute / chain several PowerShell cmdlets. Initial input from a CSV. There may be more values in the CSV than the initial command needs but they may be needed further down the chain. Like this:



 CSV
email, fname, lname, clubNo, permission
A@email.com, John, Smith, 12, R
B@email.com, Jean, Smith, 12, R
C@email.com, Jack, Smith, 12, R


Then chain



 import-csv file.txt | new-user -env Dev | set-role | export-csv result.txt


new-user only needs email, env
set-role needs email, clubNo, env, and perm



I thought my new-user cmdlet should look something like this but its not working:



function global:new-user {
param(
[Parameter(ValueFromPipelineByPropertyName = $true, mandatory = $true)][validateset('DEV', 'QA', 'PT', 'PLT', 'SIT', 'APIS', 'PD', 'Sandbox')][STRING]$env,
[Parameter( mandatory = $True, ValueFromPipelineByPropertyName = $true)][ValidateScript({ValidateEmail($_)})][String]$Email,
[String]$userKey="xyz",
[String]$secret="abc"
)

Begin {
$dc = "domain.com"
$auth = "env=$env&userkey=$userKey&secret=$secret"
$record = New-Object psobject
$record | add-member env $env
}

process {
$result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
If (-not $result.errorCode ) {
$record | Add-Member uid $result.uid
}
$record | Add-Member ErrorCode $result.errorCode -Force
}

end {
Write-Output $rescord
}


}



But I'm only seeing one record on the outbound pipeline










share|improve this question
























  • I think you just need this: import-csv file.txt | foreach { do what you need to do } | export-csv result.txt
    – Mike Twc
    Nov 17 '18 at 17:03










  • I modified my original to more reflect what I'm actually reflect what my code looks like
    – James Moore
    Nov 18 '18 at 14:11














0












0








0







I want to execute / chain several PowerShell cmdlets. Initial input from a CSV. There may be more values in the CSV than the initial command needs but they may be needed further down the chain. Like this:



 CSV
email, fname, lname, clubNo, permission
A@email.com, John, Smith, 12, R
B@email.com, Jean, Smith, 12, R
C@email.com, Jack, Smith, 12, R


Then chain



 import-csv file.txt | new-user -env Dev | set-role | export-csv result.txt


new-user only needs email, env
set-role needs email, clubNo, env, and perm



I thought my new-user cmdlet should look something like this but its not working:



function global:new-user {
param(
[Parameter(ValueFromPipelineByPropertyName = $true, mandatory = $true)][validateset('DEV', 'QA', 'PT', 'PLT', 'SIT', 'APIS', 'PD', 'Sandbox')][STRING]$env,
[Parameter( mandatory = $True, ValueFromPipelineByPropertyName = $true)][ValidateScript({ValidateEmail($_)})][String]$Email,
[String]$userKey="xyz",
[String]$secret="abc"
)

Begin {
$dc = "domain.com"
$auth = "env=$env&userkey=$userKey&secret=$secret"
$record = New-Object psobject
$record | add-member env $env
}

process {
$result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
If (-not $result.errorCode ) {
$record | Add-Member uid $result.uid
}
$record | Add-Member ErrorCode $result.errorCode -Force
}

end {
Write-Output $rescord
}


}



But I'm only seeing one record on the outbound pipeline










share|improve this question















I want to execute / chain several PowerShell cmdlets. Initial input from a CSV. There may be more values in the CSV than the initial command needs but they may be needed further down the chain. Like this:



 CSV
email, fname, lname, clubNo, permission
A@email.com, John, Smith, 12, R
B@email.com, Jean, Smith, 12, R
C@email.com, Jack, Smith, 12, R


Then chain



 import-csv file.txt | new-user -env Dev | set-role | export-csv result.txt


new-user only needs email, env
set-role needs email, clubNo, env, and perm



I thought my new-user cmdlet should look something like this but its not working:



function global:new-user {
param(
[Parameter(ValueFromPipelineByPropertyName = $true, mandatory = $true)][validateset('DEV', 'QA', 'PT', 'PLT', 'SIT', 'APIS', 'PD', 'Sandbox')][STRING]$env,
[Parameter( mandatory = $True, ValueFromPipelineByPropertyName = $true)][ValidateScript({ValidateEmail($_)})][String]$Email,
[String]$userKey="xyz",
[String]$secret="abc"
)

Begin {
$dc = "domain.com"
$auth = "env=$env&userkey=$userKey&secret=$secret"
$record = New-Object psobject
$record | add-member env $env
}

process {
$result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
If (-not $result.errorCode ) {
$record | Add-Member uid $result.uid
}
$record | Add-Member ErrorCode $result.errorCode -Force
}

end {
Write-Output $rescord
}


}



But I'm only seeing one record on the outbound pipeline







powershell pipeline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 18:51

























asked Nov 17 '18 at 16:33









James Moore

212




212












  • I think you just need this: import-csv file.txt | foreach { do what you need to do } | export-csv result.txt
    – Mike Twc
    Nov 17 '18 at 17:03










  • I modified my original to more reflect what I'm actually reflect what my code looks like
    – James Moore
    Nov 18 '18 at 14:11


















  • I think you just need this: import-csv file.txt | foreach { do what you need to do } | export-csv result.txt
    – Mike Twc
    Nov 17 '18 at 17:03










  • I modified my original to more reflect what I'm actually reflect what my code looks like
    – James Moore
    Nov 18 '18 at 14:11
















I think you just need this: import-csv file.txt | foreach { do what you need to do } | export-csv result.txt
– Mike Twc
Nov 17 '18 at 17:03




I think you just need this: import-csv file.txt | foreach { do what you need to do } | export-csv result.txt
– Mike Twc
Nov 17 '18 at 17:03












I modified my original to more reflect what I'm actually reflect what my code looks like
– James Moore
Nov 18 '18 at 14:11




I modified my original to more reflect what I'm actually reflect what my code looks like
– James Moore
Nov 18 '18 at 14:11












1 Answer
1






active

oldest

votes


















1














Begin/End block are invoked once during the cmdlet execution, so you just create a $record object in the beginning, overwrite it's properties in process block and send it down the pipe once at the end. You also do not keep other properties from the csv file. I guess you just need to do everything in the process block, and instead of creating new object, just keep using input object ($_)



begin {
$dc = "domain.com"
$auth = "env=$env&userkey=$userKey&secret=$secret"
}
process {

$_ | add-member env $env
$result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
If (-not $result.errorCode ) {
$_ | Add-Member uid $result.uid
}
$_ | Add-Member ErrorCode $result.errorCode -Force
Write-Output $_
}
end {}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353211%2ftake-pipeline-input-write-directly-to-pipeline-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Begin/End block are invoked once during the cmdlet execution, so you just create a $record object in the beginning, overwrite it's properties in process block and send it down the pipe once at the end. You also do not keep other properties from the csv file. I guess you just need to do everything in the process block, and instead of creating new object, just keep using input object ($_)



    begin {
    $dc = "domain.com"
    $auth = "env=$env&userkey=$userKey&secret=$secret"
    }
    process {

    $_ | add-member env $env
    $result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
    If (-not $result.errorCode ) {
    $_ | Add-Member uid $result.uid
    }
    $_ | Add-Member ErrorCode $result.errorCode -Force
    Write-Output $_
    }
    end {}





    share|improve this answer




























      1














      Begin/End block are invoked once during the cmdlet execution, so you just create a $record object in the beginning, overwrite it's properties in process block and send it down the pipe once at the end. You also do not keep other properties from the csv file. I guess you just need to do everything in the process block, and instead of creating new object, just keep using input object ($_)



      begin {
      $dc = "domain.com"
      $auth = "env=$env&userkey=$userKey&secret=$secret"
      }
      process {

      $_ | add-member env $env
      $result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
      If (-not $result.errorCode ) {
      $_ | Add-Member uid $result.uid
      }
      $_ | Add-Member ErrorCode $result.errorCode -Force
      Write-Output $_
      }
      end {}





      share|improve this answer


























        1












        1








        1






        Begin/End block are invoked once during the cmdlet execution, so you just create a $record object in the beginning, overwrite it's properties in process block and send it down the pipe once at the end. You also do not keep other properties from the csv file. I guess you just need to do everything in the process block, and instead of creating new object, just keep using input object ($_)



        begin {
        $dc = "domain.com"
        $auth = "env=$env&userkey=$userKey&secret=$secret"
        }
        process {

        $_ | add-member env $env
        $result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
        If (-not $result.errorCode ) {
        $_ | Add-Member uid $result.uid
        }
        $_ | Add-Member ErrorCode $result.errorCode -Force
        Write-Output $_
        }
        end {}





        share|improve this answer














        Begin/End block are invoked once during the cmdlet execution, so you just create a $record object in the beginning, overwrite it's properties in process block and send it down the pipe once at the end. You also do not keep other properties from the csv file. I guess you just need to do everything in the process block, and instead of creating new object, just keep using input object ($_)



        begin {
        $dc = "domain.com"
        $auth = "env=$env&userkey=$userKey&secret=$secret"
        }
        process {

        $_ | add-member env $env
        $result = Invoke-RestMethod "https://accounts.$dc/accounts.search?$auth" -Method Post -Body @{"query" = "select UID FROM accounts where profile.email contains '$Email'"}
        If (-not $result.errorCode ) {
        $_ | Add-Member uid $result.uid
        }
        $_ | Add-Member ErrorCode $result.errorCode -Force
        Write-Output $_
        }
        end {}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 '18 at 19:21

























        answered Nov 18 '18 at 18:25









        Mike Twc

        1,081312




        1,081312






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353211%2ftake-pipeline-input-write-directly-to-pipeline-output%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?