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

dcaz
  • 847
  • 6
  • 15
  • The first snippet only creates 1 object, with 2 _properties_. It looks like you're trying to align `$ADResult` and `$GResult` - what is `$GResult`? – Mathias R. Jessen Sep 07 '21 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](https://stackoverflow.com/a/60708579/1701026) – iRon Sep 07 '21 at 14:53
  • The 2nd object is pretty much the same as the first @IRon. I use GAM to get a persons google groups – dcaz Sep 07 '21 at 14:55
  • @dcaz And you're absolutely sure that the number of AD groups and the number of Google Groups is exactly the same? Otherwise, it doesn't make sense to loop over them together like this – Mathias R. Jessen Sep 07 '21 at 14:56
  • @MathiasR.Jessen they are not. That is why I do the `[int]$max = $ADResult.count if ([int]$GResult.count -gt $max) { [int]$max = $GResult.count }` – dcaz Sep 07 '21 at 14:59
  • I want to combine the results from both commands and then report. The issue I have is when both command results that build the objects are `$null` – dcaz Sep 07 '21 at 15:00
  • 1
    In that case, change `""` to `@()` when you create the "empty" object properties – Mathias R. Jessen Sep 07 '21 at 15:01
  • @MathiasR.Jessen change that to the answer and I can accept it because that works. – dcaz Sep 07 '21 at 15:06
  • 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***. – iRon Sep 07 '21 at 15:06
  • @iRon can you come chat about += ? [+=chat](https://chat.stackoverflow.com/rooms/236856/building-an-object-with-out) – dcaz Sep 07 '21 at 15:29

1 Answers1

1

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

$ADResult = [PSCustomObject]@{ 
    ADGroups                  = @()
    ADGroupsdistinguishedName = @()
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206