Receive "Index operation failed; the array index evaluated to null. Stack trace: at , : line 104>" and "Index operation failed; the array index evaluated to null. Stack trace: at , : line 108>"

Can't make sense of it. This runs fine outside of Adaxes in PS terminal.

# AdaxesNewUserGroups.ps1 (7-1)
#
# **  New user created from Adaxes, triggers to read AD groups from the       **
# **  "Job Title and Permissions.csv" sheet and assign them to the new user. **
#


Param (
[string]$UserName
)

Start-Sleep -Seconds 5

#*** Declarations ***

$UserName = "%username%"
$JobTitle = (Get-ADUser $UserName -Properties Title).Title
$Branch = (Get-ADUser $UserName -Properties Office).Office

Write-Output "Username = $UserName"
Write-Output "JobTitle = $JobTitle"

#'Job Title and Permissions' file to be read
$FileCSV = "C:\scripts\Job Title and Permissions.csv"

$NewUser = @{
    UserName = $UserName;
    JobTitle = $JobTitle;
    branch = $branch;
    groups = @()
    DistName = $DistName
}

$BranchCol = @() # Branch Location (not in csv)
$TitleCol = @() # "Job Title" Column
$GroupCol = @() # "AD:Member Of" Column
$groups = @()

$domain = "myfancydomain.com"

#Logging
$LogPath = "C:\Scripts\Logs\AdaxesNewUserGroups_Log_$(Get-Date -format "yyyyMMddHHmmss").txt"
Start-Transcript -Path $LogPath

Write-Output "LogPath = $LogPath"
Write-Output ""
Write-Output "username = $UserName", "JobTitle = $JobTitle", "branch = $branch"
Write-Output ""


#*** Read from CSV and load each table ***

Write-Output "*** Starting Table Load ***"
$i=0
$j=0
#Import-Csv 'C:\scripts\Job Title and Permissions.csv' |`
Import-Csv $FileCSV |`
    ForEach-Object {
        $TitleCol += $_."Job Title"
        $GroupCol += $_."AD: Member Of"
        $i++
    }
Import-Csv 'C:\scripts\Branches.csv' |`
    ForEach-Object {
        $BranchCol += $_.branch
    $j++
    }

$EndCSV1 = $i
$EndCSV2 = $j

Write-Output " "
Write-Output "*** End Table Load ***"
Write-Output " "


#*** Traverse the table to find the New User's Job Title ***

$i = 0
    ForEach ($Title in $TitleCol) {
        If ($TitleCol[$i] -eq $NewUser["JobTitle"]) {
           $iRange = $i # Found the target JobTitle - save the index where the AD Groups start
            continue
        } #if
        $i++
    }


# ** Assign the AD Group Names to the New User **

$j = 0
Write-Output ""
Write-Output "*** AD Groups to Assign ***"
Write-Output ""
do {
    write-Output $GroupCol[$iRange]

    # Replace "(Branch)" with the actual branch of the New User

    if ($GroupCol[$iRange].StartsWith("(Branch)")) {
        write-Output "starts with Branch"
        Write-Output ""
        $NewUser.groups += $GroupCol[$iRange].Replace("(Branch)", $NewUser.branch)
    } else {
        $NewUser.groups += $GroupCol[$iRange]
    } #if-else

    $iRange++ #Column number + 1
    $j++      #Absolute number of groups + 1
} until (($TitleCol[$iRange] -ne "") -or ($GroupCol[$iRange] -eq ""))


#*** Assign the User to the Group, if not already in the group ***

$NewUser.DN = (Get-ADUser -Identity $NewUser.username).DistinguishedName
$groups = $NewUser.groups

foreach ($group in $groups) {
    $GroupMast = (Get-ADGroup -Filter "Name -eq '$group'" -Properties *)
    $GroupDN = ($GroupMast | select DistinguishedName).DistinguishedName
    $GroupName = ($GroupMast | select Name).Name
    write-output ""
    write-output "group = $group"
    Write-output "GroupDN = $GroupDN"
    Write-output "GroupName = $GroupName"

    if ($group = $GroupName) {
        write-output "Found $group group"

        #Skip the 'Domain Users' group - since it is already assigned to the new user
        if ($group -ne "Domain Users") {
        #assign user to group
            Add-ADGroupMember -Identity $GroupDN -Members $NewUser.DN -Server $domain
            Write-Output "group = $group finished assignment to user"
        } else {
            write-output "Skipping $group group"
            continue
        }
    } else {
        write-output "$group group not found. Found $GroupName, $GroupDN."
    }
} #foreach
write-output ""
Write-Output ""
Stop-Transcript
by (20 points)

1 Answer

by (216k points)
0 votes

Hello Jack,

The issue is caused by the $iRange variable that is equal $NULL instead of digits and used in an array at lines 96 and 100 of your script. As a possible solution here, you can add the following code to display an error in the Execution log and exit from the script in case of $iRange variable being $NULL:

if ($NULL -eq $iRange)
{
    $Context.LogMessage("Target  JobTitle wasn’t found.", "Error")
    return
}

If that is not what you need, for us to suggest a solution, please, provide an accurate description of all variables used in the script and how exactly the script should work. Live examples of the desired behaviour will be much appreciated.

Related questions

So I need to export a list of all user's Line URI's to a CSV file. Running Adaxes 2021 Version 3.14.18804.0 (64 bit) and Teams Powershell 4.1.0 ... a Microsoft 365 account } finally { # Close the connection and release resources Disconnect-MicrosoftTeams }

asked Aug 4, 2022 by TheLexicon (200 points)
0 votes
1 answer

Update group membership based on one property values. I am trying to find a script that resembles "Update group membership based on two property value" but just for one value.

asked Apr 7, 2022 by lee_thomas (20 points)
0 votes
1 answer

I have found the script to force membership updates for all rule based groups, but is there a script to force update a specific rule based group? I am looking to add a ... I would like to trigger a rule based group that adds members of the manual group. Thanks

asked Jul 9 by msheppard (840 points)
0 votes
1 answer

I'm trying to setup a quick automations to drop a notification into a Micrsoft Teams feed using their Webhook integration. I've managed to make Webhooks work ... -body $body -ContentType 'application/json' Any assistance with this would be gratefully received

asked Jan 20, 2020 by richarddewis (260 points)
0 votes
0 answers

I have an account that I am using as the "run as" account to run PowerShell scripts for several different custom commands. I would like to be able to update the ... a script that updates the credentials used to run a script in an existing custom command?

asked Oct 18, 2021 by KelseaIT (320 points)
0 votes
1 answer