返回的数组包含很多" True"值

时间:2017-03-21 13:58:44

标签: powershell

人。 我编写了一个简单的PowerShell脚本,并且返回值有奇怪的行为。

function get-ResourcesForCsv {
    param([string]$Container)

    $Groups = Get-ADGroup -filter * -SearchBase $Container | 
            select-object Name | where-object { $_.Name -notlike "*`$All*" }
    $Resources = @()    
    foreach( $Group in $Groups ) {
        $Group.Name -match "((\w+)_)(.+)(_([RW]))"     
        $Resource = @{
            Name = $Matches[3]
            Access = $Matches[5]
            Group = $Group.Name
        }
        $Resources += New-Object PSObject -Property $Resource
    }
    write-host $Resources # Here's ok
    $Resources
}

function get-ResourcesForHtml {
    param([string]$Container)
    $Temp = get-ResourcesForCsv $Container
    write-host $Resources # Here's things goes terrible
    ...
}

当我检查$ Resources值时 - 它没关系,当我检查$ Temp - size加倍时,数组包含很多" True"在开始。

P.S。对不起我可怕的英语((

1 个答案:

答案 0 :(得分:1)

您没有将此匹配的结果分配给任何变量。我假设如果匹配你的正则表达式,你会添加$ Resource:

if($Group.Name -match "((\w+)_)(.+)(_([RW]))" )
{
        $Resource = @{
            Name = $Matches[3]
            Access = $Matches[5]
            Group = $Group.Name
        }
        $Resources += New-Object PSObject -Property $Resource
}

您分配的每个cmdlet返回都未分配给变量,或者从函数get-ResourcesForCsv处理到变量$Temp,包括匹配正则表达式的结果。

注意:在我的组织中测试