Reputation: 69
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
[email protected], John, Smith, 12, R
[email protected], Jean, Smith, 12, R
[email protected], 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
Upvotes: 0
Views: 136
Reputation: 2365
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 {}
Upvotes: 1