0

guys. I writing a simple powershell script, and have strange behaviour of returning value.

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

When i check $Resources value - it's ok, when i check $Temp - size is doubled, and array contains a lot of "True" at the begining.

P.S. Sorry for my terrible english ((

Alex Art
  • 3
  • 4

1 Answers1

1

You are not assigning the result of this match to any variable. I assume that you would add to $Resource if its matching your regex so:

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

You were assigning every return of cmdlet which is not assigned to variable or anyway handled from the function get-ResourcesForCsv to the variable $Temp including the result from matching the regex.

note: tested in my organization

pandemic
  • 1,135
  • 1
  • 22
  • 39
  • Oh... My main language is С++ and this behavior looks very illogical and strange. Thank you for opening my eyes. – Alex Art Mar 21 '17 at 15:16