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 (
[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