0 votes

Receive "Index operation failed; the array index evaluated to null. Stack trace: at <ScriptBlock>, <No file>: line 104>" and "Index operation failed; the array index evaluated to null. Stack trace: at <ScriptBlock>, <No file>: 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 (

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"

$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 ***"
#Import-Csv 'C:\scripts\Job Title and Permissions.csv' |`
Import-Csv $FileCSV |`
    ForEach-Object {
        $TitleCol += $_."Job Title"
        $GroupCol += $_."AD: Member Of"
Import-Csv 'C:\scripts\Branches.csv' |`
    ForEach-Object {
        $BranchCol += $_.branch

$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
        } #if

# ** 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"
    } else {
        write-output "$group group not found. Found $GroupName, $GroupDN."
} #foreach
write-output ""
Write-Output ""
by (20 points)

1 Answer

0 votes
by (215k points)

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")

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

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 by lee_thomas (20 points)
0 votes
0 answers

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 (240 points)
0 votes
1 answer

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

We are using the following the script and would like to have it updated to use a CSV file instead of the list option. Thank you in advance for your assistance. ... error occurred when updating user. Error: " + $_.Exception.Message, "Warning") }

asked Jun 5, 2017 by willy-wally (3.2k points)
0 votes
1 answer

I've got the following script as part of a larger piece where param-members is an AD Object picker list seperated by a ' ; ' currently: New-DistributionGroup -Name ... or convert that to username but I'm struggling to achieve that with multiple Users selected

asked Jan 27, 2020 by richarddewis (240 points)
2,737 questions
2,471 answers
1,344,989 users