All, first post here. For an exercise I wanted to write an API wrapper in Powershell. Below is the first Get function. It basically works but
I’m wondering if there’s a more elegant way to do the process block. Seems like basically duplicate Foreach loops, but I couldn’t figure out
a better way to handle the two different parameters. I thought I could use $PSItem to
refer to the current object in the pipeline but that doesn’t seem to work. any suggestions?
Also, I’m blind and using a screen reader, so please let me know if there are any glaring formatting issues in this post. Thanks.
Function Get-RLCompany {
[CmdletBinding(DefaultParameterSetName = 'ByID')]
Param(
[Parameter(ValueFromPipelineByPropertyName,
ParameterSetName = 'ByName')]
[String[]]$Name,
[Parameter(ValueFromPipelineByPropertyName,
ParameterSetName = 'ByID')]
[String[]]$ID
)
Begin {
$Entity = 'companies'
$Output = [System.Collections.Generic.LIST[Object]]::New()
}
Process {
If ($PSCmdlet.ParameterSetName -eq 'ByID') {
$ID | ForEach-Object {
$Response = Invoke-RLRestMethod -Entity $Entity -Query "id=$($_)"
$Output.Add($Response)
}
}
elseif ($PSCmdlet.ParameterSetName -eq 'ByName') {
$Name | ForEach-Object {
$Response = Invoke-RLRestMethod -Entity $Entity -Query "name=$($_)"
$Output.Add($Response)
}
}
}
End {
$Output | Write-Output
}
}