0

Hi all I get the following error when running my script:

 Vorgesetzer: 1
Index was outside the bounds of the array.
At line:87 char:20
+         for ($z=0; $z -le $lst_ma_vrg_grp[$i].count-1;$z++){
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException 

The Code is:

#List of Teams and ther Supervisor , "1"  is the Team number

$lst_ma_vrg_grp = @(("1",("Department","Supervisor","Supervisor2","Supervisor3"),("Department2","Supervisor","Supervisor2","Supervisor3","Supervisor4"),("Department3","Supervisor","Supervisor2","Supervisor3","Supervisor4"),
("2",("Department","Supervisor","Supervisor2","Supervisor3"),("Department2","Supervisor","Supervisor2","Supervisor3","Supervisor4"),("Department3","Supervisor","Supervisor2","Supervisor3","Supervisor4"),("Department4","Supervisor","Supervisor2","Supervisor3","Supervisor4"),
("3",("Department4","Supervisor","Supervisor2","Supervisor3","Supervisor4"),("Department3","Supervisor","Supervisor2","Supervisor3","Supervisor4"),
("4",("Department","Supervisor")))


#All supervisors from all teams with their AD location

$lst_ma_path = @(
("Supervirsor name 1","CN=,OU=,OU=,OU=,DC=,DC=,DC="),
("Supervirsor name 2","CN=,OU=,OU=,OU=,DC=,DC=,DC="),
("Supervirsor name 3","CN=,OU=,OU=,OU=,DC=,DC=,DC="))


#All Departments with their deafult Job Title

$lst_title = @(("Department","Jobtitle1","AJobtitle2","Jobtitle3"),("Department2",""Jobtitle1","AJobtitle2","Jobtitle3","Jobtitle4","AJobtitle5","Jobtitle6"),("Department3",""Jobtitle1","AJobtitle2","Jobtitle3","Jobtitle4","AJobtitle5","Jobtitle6"))
[And so on, it was cut]

cls
$auswahlTeam = Read-Host -Prompt "Welches Team? (1,2,3,4)"
$externer = Read-Host -Prompt "Freelancer? (yes / no): "
$vorname = Read-Host -Prompt "Firstname"
$nachname = Read-Host -Prompt "Lastname"
for ($i=0;$i -le $lst_title.Length-1; $i++) 
    {Write-Host $i":" $lst_title[$i][0]}
Write-Output "","----------------------------------"
$numabt = Read-Host -Prompt "Department"
$global:abteilung = $lst_title[$numabt][0]


for ( $i=0;$i -le $lst_ma_vrg_grp.Length-1;$i++){
    if ($auswahlTeam -eq $lst_ma_vrg_grp[$i][0]){
        for ($z=0; $z -le $lst_ma_vrg_grp[$i].count-1;$z++){
            if ($abteilung -eq $lst_ma_vrg_grp[$i][$z][0]){
                for ($c=1;$c -le $lst_ma_vrg_grp[$i][$z].count-1; $c++) {
                    Write-Host $c":" $lst_ma_vrg_grp[$i][$z][$c]
                    }
                Write-Output "","----------------------------------"
                $vorgesetzer = Read-Host -Prompt "Vorgesetzer"
                $vorgesetzer = $lst_ma_vrg_grp[$i][$z][$vorgesetzer]
               
                for ($i=0;$i -le $lst_ma_path.Length-1;$i++){
                    if ($vorgesetzer -eq $lst_ma_path[$i][0]){
                     $global:vorgesetzer = $lst_ma_path[$i][1]
}}}}}}

This script should automatically create users based on the entries you have previously made.

We have different teams that have different departments. The departments have different people who can be a supervisor.

I hope you can help me with the error message, I do not know how to fix it.

mklement0
  • 382,024
  • 64
  • 607
  • 775
ITGUYGER
  • 1
  • 1
  • `for ($z=0; $z -lt $lst_ma_vrg_grp[$i].count; $z++)` – Theo Jan 06 '23 at 14:40
  • 1
    on my end `$lst_ma_vrg_grp` has syntax errors. it looks like you are trying to define a dictionary but the elements `("1", (...), ("2", (...)` etc are left open. surprised you did not get errors earlier in the script. Both `Count` and `Length` will return the same thing here but for arrays it's better to use `Count`. – crstn.udrea Jan 06 '23 at 14:58
  • I guess you enabled [`Set-StrictMode`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/set-strictmode). See also: [how to determine programmatically whether StrictMode is set?](https://stackoverflow.com/q/63089871/1701026) – iRon Jan 06 '23 at 19:16

0 Answers0