0

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

10
  • The first snippet only creates 1 object, with 2 properties. It looks like you're trying to align $ADResult and $GResult - what is $GResult? Commented Sep 7, 2021 at 14:49
  • 1
    You start with $ADResult and then you call it $GPResult. Anyways, try to avoid using the increase assignment operator (+=) to create a collection Commented Sep 7, 2021 at 14:53
  • The 2nd object is pretty much the same as the first @IRon. I use GAM to get a persons google groups Commented Sep 7, 2021 at 14:55
  • 1
    In that case, change "" to @() when you create the "empty" object properties Commented Sep 7, 2021 at 15:01
  • 1
    If $Max represents the maximum number of $ADResult objects, it should be $ADResult[$i].ADGroups (not $ADResult.ADGroups[$i]) as it might be that $ADResult exists but has simply no ADGroups. Anyways, it would help if you copy the exact error message (which the line that fails) into the question. Commented Sep 7, 2021 at 15:06

1 Answer 1

1

Change the value of the properties in your "empty" placeholder object from an empty string to a empty array:

$ADResult = [PSCustomObject]@{ 
    ADGroups                  = @()
    ADGroupsdistinguishedName = @()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.