Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Скриптовые языки администрирования Windows (http://forum.oszone.net/forumdisplay.php?f=102)
-   -   [решено] Index was outside the bounds of the array (http://forum.oszone.net/showthread.php?t=286311)

Geliosvamp 11-08-2014 14:30 2387705

Index was outside the bounds of the array
 
В $b помещается массив, например, @("CN=Иванов Иван Иваныч,OU=Users,DC=corp,DC=ulsk,DC=loc", "CN=Семенов Семен Семеныч,OU=Users,DC=corp,DC=ulsk,DC=loc").
Далее необходимо выудить у данных учётных записей значения полей name, title и department. Я решил всё это засунуть в одноименные массивы $name, $title и $department.

Код:

$name = @()
$title = @()
$department = @()

$b = @(Get-ADGroupMember $n | ForEach {$_.distinguishedName})

$nn = $b.Count - 1

for ($i=0;$i -le $nn ;$i++)
                {
                    Get-ADUser -Identity $b[$i] -Properties * | ForEach-Object {$name[$i] = $_.name; $title[$i] = $_.title; $department[$i] = $_.department}
                }


Выскакивает ошибка:
Код:

Index was outside the bounds of the array.
E:\PS.ps1:93 знак:81
+ ... orEach-Object {$name[$i] = $_.name; $title[$i] = $_.title; $department[$i] = $_. ...
+                    ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException
 
Index was outside the bounds of the array.
E:\Docs\PS.ps1:93 знак:102
+ ... $i] = $_.name; $title[$i] = $_.title; $department[$i] = $_.department}
+                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException
 
Index was outside the bounds of the array.
E:\Docs\PS.ps1:93 знак:125
+ ... i] = $_.title; $department[$i] = $_.department}
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException

Собственно 93 знак:81 - это $name[$i] = $_.name.
$name[$i] = @($_.name) - тоже не спасает.

Что я не так делаю?

Kazun 11-08-2014 16:06 2387754

Достаточно одной строки:
Код:

Get-ADGroupMember $n -Recursive | Get-ADUser -Properties Name,Title,Department | Select Name,Title,Department


Время: 13:38.

Время: 13:38.
© OSzone.net 2001-