我正在尝试找到一些方法来灵活地更改/替换PowerShell中的管道元素:
Function Where-DirectlyReportsTo {
Param (
[Parameter(
ValueFromPipeline = $true,
HelpMessage = "The ADUser object to be tested"
)]
[Microsoft.ActiveDirectory.Management.ADUser] $ADUser,
[Parameter(
Mandatory = $true,
ValueFromPipeline = $false,
Position = 0
)]
[String] $mgrDN
)
Process {
If ($ADUser) {
If ($ADUser.Manager -eq $mgrDN) { Return $ADUser }
}
}
}
$Properties = @("Manager")
$users = Get-ADUser -Filter * -SearchBase $OU -Properties $Properties
[ScriptBlock] $sb = {Where-DirectlyReportsTo "CN=Colonel Foobar,$OU"}
$DNs = $users | $sb | %{$_.DistinguishedName}
我想返回所有向Foobar上校报告的用户的DN,但它给了我错误Expressions are only allowed as the first element of a pipeline.
这是一个微不足道的例子,但我最终希望能够将管道步骤放入循环中,并向其传递不同的ScriptBlock以获得不同的用户集,或者使用更复杂的ScriptBlock(例如:{Where-IsEmployee | Where-IsInDepartment "Finance" | Where-LikesIceCream}
)。
我意识到我可能完全错了,我非常感谢你给我指明正确的方向。
编辑:为了澄清,以下是我想完成的任务的大致轮廓:
[ScriptBlock[]] $arrBlocks = @( # lots of different cases
)
ForEach ($sb In $arrBlocks) {
$DNs = $users | $sb | %{$_.DistinguishedName}
# Then do something with the DNs
}
实际上,这可能涉及一个哈希表而不是数组,这样我就知道如何处理每组结果。
语法错误很简单:
# Wrong:
$DNs = $users | $sb | %{$_.DistinguishedName}
# Correct; note the call operator in front of $sb:
$DNs = $users | &$sb | %{$_.DistinguishedName}
这仍然会让你的管道陷入死胡同。你不需要在这里花枝招展;只需将$Input
管道传输到下一个函数:
[ScriptBlock] $sb = {$Input | Where-DirectlyReportsTo "CN=Colonel Foobar,$OU"}
您不应该让$sb
本身枚举输入。这是额外的开销,如果使用param()
,则会将脚本块提升为cmdlet。你真的不需要。
事实上,你可以将整个事情简化为四行,甚至一行很长的行:
$properties = @("Manager")
$managers = @(
"CN=Colonel Foobar,$OU"
"CN=Sergeant Foobar,$OU"
"CN=Random Manager,$OU"
)
$users = Get-ADUser -Filter * -SearchBase $OU -Properties $properties
$DNs = $users | ?{ $managers -contains $_.Manager } | %{ $_.DistinguishedName }
我用这个代码测试了它:
$OU = 'OU=test'
$users = @(
@{
Manager = "CN=Colonel Foobar,$OU";
DistinguishedName = "Show me!"
}
@{
Manager = "CN=Anon Y Mous,$OU";
DistinguishedName = "Don't show me!"
}
'rabbit'
42
$null
@{
DistinguishedName = "Don't show me, either!"
}
@{
Manager = "CN=Random Manager,$OU";
DistinguishedName = "Show me, too!"
}
)
$managers = @(
"CN=Colonel Foobar,$OU"
"CN=Sergeant Foobar,$OU"
"CN=Random Manager,$OU"
)
$DNs = $users | ?{ $managers -contains $_.Manager } | %{ $_.DistinguishedName }
$DNs | Write-Host
如果你想的话,你可以把它写得更详细一点:
$properties = @("Manager")
$managers = @(
"CN=Colonel Foobar,$OU"
"CN=Sergeant Foobar,$OU"
"CN=Random Manager,$OU"
)
$filter = { $managers -eq $_.Manager }
$selector = { $_.DistinguishedName }
$users = Get-ADUser -Filter * -SearchBase $OU -Properties $properties
$DNs = $users | ?{ &$filter } | %{ &$selector }
听起来你想最终拥有多个过滤器。这也很容易:
$properties = @("Manager")
$managers = @(
"CN=Colonel Foobar,$OU"
"CN=Sergeant Foobar,$OU"
"CN=Random Manager,$OU"
)
$filters = @(
{ $managers -contains $_.Manager }
{ ![string]::IsNullOrWhiteSpace($_.DistinguishedName) }
)
$selector = { $_.DistinguishedName }
$users = Get-ADUser -Filter * -SearchBase $OU -Properties $properties
$DNs = $users | ?{ $filters.Invoke() -notcontains $false } | %{ &$selector }
脚本块中的管道头需要有一些东西,您必须将脚本块定义为接受管道输入。例如:
[scriptBlock]$sb = {[CmdletBinding()]param([Parameter(ValueFromPipeline=$true)]$obj) `
process {
foreach ($o in $obj) {
$o | Where-DirectlyReportsTo "CN=Colonel Foobar,$OU"}}}
你也不能像那样把$sb扔进管道,试试这个:
$users | &$sb | %{$_.DistinguishedName}
在PS 2.0中,这种更简洁(可读?)的语法起作用。(在PS 3.0中不起作用,设置isFilter会引发异常)
$filterSet =
{$_.property -eq "desired value"},
{$_.method() -eq "I like the return value"},
{$_.gettype() -eq [special.type.I.like]},
{arbritraryBoolPSfunction $_}
$filterSet | % {$_.isFilter = $true} # make 'em all filters
ForEach ($filter In $filterSet) {
$DNs = $users | & $filter | %{$_.DistinguishedName}
# Then do something with the DNs
}