2

我正在尝试找到一些在 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.

这是一个简单的例子,但我最终希望能够将管道步骤放入循环中并传递不同的 ScriptBlocks 以获取不同的用户集,或者使用更复杂的 ScriptBlocks(例如:){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
}

实际上,这可能涉及哈希表而不是数组,因此我知道如何处理每组结果。

4

3 回答 3

5

语法错误很简单:

# 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 }
于 2013-07-20T03:16:31.323 回答
3

您的脚本块中管道的头部需要有一些东西,您必须将您的脚本块定义为采用管道输入。例如:

[scriptBlock]$sb = {[CmdletBinding()]param([Parameter(ValueFromPipeline=$true)]$obj) `
                   process {
                       foreach ($o in $obj) {
                           $o | Where-DirectlyReportsTo "CN=Colonel Foobar,$OU"}}}

你也不能像那样把 $sb 扔进管道,试试这个:

$users | &$sb | %{$_.DistinguishedName}
于 2013-07-19T21:29:37.547 回答
0

在 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
}
于 2013-07-20T23:07:04.717 回答