无法将值传递给函数
Can't pass the value to the function
我正在尝试设置一个条件,即如果列中的值等于我将创建的新列,则 post 来自我的其他值。但是我的函数无法从我的数据中获取值。有什么建议吗?
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
我遇到了这个错误
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.
我已经在下面回答了你的问题,但我觉得有义务告诉你,这个单行代码可能会完成与下面所有代码相同的事情:
$nestedGroups |Select Value*,@{Name='MainGroup';Expression={if($_.value1 -eq $_.value2){$_.choice1}else{$_.choice2}}}
看起来你想要:
- 声明一个参数来接收整个输入对象(这样你可以稍后修改and/or return),然后
- 将其余参数标记为
ValuefromPipeline<strong>ByPropertyName</strong>
(而不是ValuefromPipeline
),最后
- 将代码移动到显式
process {}
块中 - 这将确保它被执行一次 通过管道绑定的每个输入对象
function 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
}
}
}
使用一些模拟输入进行测试:
@(
[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
... 我们应该期待这样的输出:
Value1 Value2 MainGroup
------ ------ ---------
123 123 Pick me!
123 456 Pick me!
我正在尝试设置一个条件,即如果列中的值等于我将创建的新列,则 post 来自我的其他值。但是我的函数无法从我的数据中获取值。有什么建议吗?
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
我遇到了这个错误
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.
我已经在下面回答了你的问题,但我觉得有义务告诉你,这个单行代码可能会完成与下面所有代码相同的事情:
$nestedGroups |Select Value*,@{Name='MainGroup';Expression={if($_.value1 -eq $_.value2){$_.choice1}else{$_.choice2}}}
看起来你想要:
- 声明一个参数来接收整个输入对象(这样你可以稍后修改and/or return),然后
- 将其余参数标记为
ValuefromPipeline<strong>ByPropertyName</strong>
(而不是ValuefromPipeline
),最后 - 将代码移动到显式
process {}
块中 - 这将确保它被执行一次 通过管道绑定的每个输入对象
function 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
}
}
}
使用一些模拟输入进行测试:
@(
[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
... 我们应该期待这样的输出:
Value1 Value2 MainGroup
------ ------ ---------
123 123 Pick me!
123 456 Pick me!