我想执行/链接几个PowerShell cmdlet。来自CSV的初始输入。 CSV中的值可能会比初始命令所需的要多,但在链的下游可能需要它们。像这样:
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
然后连锁
import-csv file.txt | new-user -env Dev | set-role | export-csv result.txt
新用户只需要电子邮件,env 设定角色需要电子邮件,clubNo,env和烫发
我认为我的新用户cmdlet应该看起来像这样,但是不起作用:
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
}
}
但是我在出站管道上只看到一条记录
答案 0 :(得分:1)
开始/结束块在cmdlet执行期间被调用一次,因此您只需在开始处创建一个$ record对象,覆盖其在流程块中的属性,然后在管道末尾将其发送一次。您也不会保留csv文件中的其他属性。我猜您只需要在流程块中做所有事情,而不是创建新对象,而只需继续使用输入对象($ _)
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 {}