I am trying to make a condition that if the value in the column is equal to the new column that I will be making is post the value from my else. But my function can't get the values from my data. Any advice?
function MainGroupChoice{
param (
[Parameter(ValuefromPipeline = $true)][String] $value1,
[Parameter(ValuefromPipeline = $true)][String] $value2,
[Parameter(ValuefromPipeline = $true)][String] $choice1,
[Parameter(ValuefromPipeline = $true)][String] $choice2
)
if ($value1 -eq $value2) {
return
Add-Member -NotePropertyName MainGroup -NotePropertyValue $choice1 -PassThru -Force
}
else {
return
Add-Member -NotePropertyName MainGroup -NotePropertyValue $choice2 -PassThru -Force
}
}
$table2 = $NestedGroupUsers | MainGroupChoice -choice1 $ADgroupname.name -choice2 $groupNestedName -value1 $table2.ParentGroup -value2 $nestedmember.distinguishedName
I got this error
The input object cannot
| be bound to any
| parameters for the
| command either because
| the command does not
| take pipeline input or
| the input and its
| properties do not match
| any of the parameters
| that take pipeline
| input.
I've answered your question below, but I feel obligated to tell you that this one-liner can likely accomplish the same as all the code below:
$nestedGroups |Select Value*,@{Name='MainGroup';Expression={if($_.value1 -eq $_.value2){$_.choice1}else{$_.choice2}}}
It looks like you'll want to:
ValuefromPipelineByPropertyName
(instead of ValuefromPipeline
), and finallyprocess {}
block - this will ensure it's executed once for each input object bound via the pipelinefunction MainGroupChoice {
param (
[Parameter(ValueFromPipeline)]$InputObject,
[Parameter(ValuefromPipelineByPropertyName = $true)][String] $Value1,
[Parameter(ValuefromPipelineByPropertyName = $true)][String] $Value2,
[Parameter(ValuefromPipelineByPropertyName = $true)][String] $Choice1,
[Parameter(ValuefromPipelineByPropertyName = $true)][String] $Choice2
)
process {
if ($value1 -eq $value2) {
return $InputObject |Add-Member -NotePropertyName MainGroup -NotePropertyValue $choice1 -PassThru -Force
}
else {
return $InputObject |Add-Member -NotePropertyName MainGroup -NotePropertyValue $choice2 -PassThru -Force
}
}
}
Testing with some mock input:
@(
[pscustomobject]@{
Value1 = 123
Value2 = 123
Choice1 = "Pick me!"
Choice2 = "Not me..."
}
[pscustomobject]@{
Value1 = 123
Value2 = 456
Choice1 = "Not me..."
Choice2 = "Pick me!"
}
) |MainGroupChoice |Select Value*,MainGroup
... and we should expect output like:
Value1 Value2 MainGroup
------ ------ ---------
123 123 Pick me!
123 456 Pick me!