2015-12-09 8 views
0

я следующий кодУдаление пустых строк из массива

$array = New-Object System.Collections.ArrayList 

ForEach($domain in ((Get-ADForest).Domains)){ 

ForEach($group in (Get-ADGroup -Filter * -Properties Members -Server $domain | ? {-not $_.members})){ 

    $temp = [PSCustomObject] @{'1 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*1*"} | Select -ExpandProperty Name 
           '1 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*1*"} | Select -ExpandProperty Name 
           '2 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*2*"} | Select -ExpandProperty Name 
           '2 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*2*"} | Select -ExpandProperty Name 
           '3 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*3*"} | Select -ExpandProperty Name 
           '3 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*3*"} | Select -ExpandProperty Name   
           } 

    $array.Add($temp) | Out-Null 

} 

} 


$array 

Это работает с несколькими оговорками, первый есть пустые строки в элементах массива, как я могу удалить их? Второй в конце элемента массива я хочу добавить еще одну строку, которая будет подсчитывать общее количество элементов в элементе. Проблема, связанная с подсчетом, который я имею, но добавлением к последней строке массива, является проблемой.

+0

Добавить '$ array.Count' в качестве последнего оператора в скрипт? –

ответ

0

Несколько вещей. Если вам не нужен только ArrayList, вы можете просто придерживаться стандартных массивов массивов. Их легче сделать, и больше людей знакомы с ними, поэтому любые проблемы, которые вы используете, будут легче поддерживать. Что касается удаления пробелов, вы можете просто использовать инструкцию Where.

$array = $array | Where{$_.'1 Security' -or $_.'1 Distro' -or $_.'2 Security' -or $_.'2 Distro' -or $_.'3 Security' -or $_.'3 Distro'} 

Затем вы добавляете только один последний объект в конце, который включает в себя подсчеты для каждого свойства.

$Counts = [PSCustomObject]@{ 
          '1 Security' = $array.'1 Security'.count 
          '1 Distro' = $array.'1 Distro'.count 
          '2 Security' = $array.'2 Security'.count 
          '2 Distro' = $array.'2 Distro'.count 
          '3 Security' = $array.'3 Security'.count 
          '3 Distro' = $array.'3 Distro'.count 
          } 
$array += $Counts 

Так меняется использовать стандартный массив вместо ArrayList, и добавление в этих изменений все это выглядит следующим образом:

ForEach($domain in ((Get-ADForest).Domains)){ 

    ForEach($group in (Get-ADGroup -Filter * -Properties Members -Server $domain | ? {-not $_.members})){ 

     [array]$array += [PSCustomObject] @{'1 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*1*"} | Select -ExpandProperty Name 
            '1 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*1*"} | Select -ExpandProperty Name 
            '2 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*2*"} | Select -ExpandProperty Name 
            '2 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*2*"} | Select -ExpandProperty Name 
            '3 Security' = $group | ? {$_.GroupCategory -like "Security" -and $_.DistinguishedName -like "*3*"} | Select -ExpandProperty Name 
            '3 Distro' = $group | ? {$_.GroupCategory -like "Distribution" -and $_.DistinguishedName -like "*3*"} | Select -ExpandProperty Name   
            } 

    } 

} 

$array = $array | Where{$_.'1 Security' -or $_.'1 Distro' -or $_.'2 Security' -or $_.'2 Distro' -or $_.'3 Security' -or $_.'3 Distro'} 

$Counts = [PSCustomObject]@{ 
          '1 Security' = $array.'1 Security'.count 
          '1 Distro' = $array.'1 Distro'.count 
          '2 Security' = $array.'2 Security'.count 
          '2 Distro' = $array.'2 Distro'.count 
          '3 Security' = $array.'3 Security'.count 
          '3 Distro' = $array.'3 Distro'.count 
          } 
$array += $Counts 
$array 
Смежные вопросы