My code builds two PSCustomObjects. Both objects can be $null
, either Object can be $null
. I test for that like this
$ADResult = @()
if ([string]::IsNullOrWhiteSpace($ADGroups)) {
Write-Warning "No AD Groups"
$ADResult = [PSCustomObject]@{
ADGroups = ""
ADGroupsdistinguishedName = ""
}
}
Else {
foreach ($group in $ADGroups) { do stuff }
The problem is when both objects are $null
. When I put the objects together for a report. I get the error "Cannot index into a null array."
[int]$max = $ADResult.count
if ([int]$GResult.count -gt $max) { [int]$max = $GResult.count }
$Result = @()
for ( $i = 0; $i -lt $max; $i++) {
$Result += [PSCustomObject]@{
PrimaryEmail = $email
Title = $UserInfo.title
Department = $UserInfo.Department
Manager = $Manager
EmailBackup = $ENV:Backup
AccountDisabled = $ENV:ADDisabled
GoogleRemoved = $ENV:RemoveGoogle
ADGroupName = $ADResult.ADGroups[$i]
ADGroupNameDistinguishedName = $ADResult.ADGroupsdistinguishedName[$i]
GoogleGroup = $GResult.GoogleGroups[$i]
Role = $GResult.role[$i]
DateOfSeparation = (Get-Date).ToString("yyyy_MM_dd")
UnixID = $unix
UserDistinguishedName = $UserInfo.distinguishedName
UserOU = $UserInfo.Ou
PrimaryGroup = $UserInfo.primaryGroup.Split('=').Split(',')[1]
}
}
How can I overcome this better?
I want the other information like ou and related if both objects are $null