管道插入ScriptBlock

时间:2013-07-19 21:06:38

标签: powershell pipe scriptblock

我正试图在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}

我想要返回向Colonel Foobar报告的所有用户的DN,但它给了我错误Expressions are only allowed as the first element of a pipeline.

这是一个简单的例子,但我最终希望能够将管道步骤放在循环中并将其传递给不同的ScriptBlocks来获取不同的用户集,或者使用更复杂的ScriptBlocks(例如:{{1} })。

我意识到我可能会把这一切都搞错了,我非常感谢被指向正确的方向。

编辑:澄清一下,这里是我想要完成的大致概述:

{Where-IsEmployee | Where-IsInDepartment "Finance" | Where-LikesIceCream}

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

3 个答案:

答案 0 :(得分:4)

语法错误很简单:

# 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 }

答案 1 :(得分:3)

您的scriptblock中需要在管道的顶部有一些东西,您必须将您的scriptblock定义为接受管道输入....:

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

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

$users | &$sb | %{$_.DistinguishedName}

答案 2 :(得分: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
}