Den10102020
Den10102020

Reputation: 87

Error with huge list of AD groups. Not giving output

I am trying to automate a powerbi report using AD data. I was doing a test on this code and it will give me a good output that I need for a couple of groups. But I was trying to run this with 250K list of groups and I am getting this error. I tried couple of times and it takes a lot of time and it was not giving me any output.

Error Exception calling "GetSteppablePipeline" with "1" argument(s): "The parameter is incorrect. "

Note: All attributes are generic and no custom attributes

$groupUsersList = Get-Content $Path
$tableStorage = @()

$groupUsersList |  ForEach-Object { 
    $GroupProperties = get-adgroup $_ -prop *
   
        $table = new-object psobject
        $table | Add-Member -NotePropertyName Name -NotePropertyValue  $GroupProperties.Name
        $table | Add-Member -NotePropertyName GroupCategory -NotePropertyValue $GroupProperties.GroupCategory
        $table | Add-Member -NotePropertyName Owner -NotePropertyValue $GroupProperties.extensionAttribute12.Split("=")[1].split(",").split("|")[0] 
        $table | Add-Member -NotePropertyName OwnerinfoID -NotePropertyValue  $GroupProperties.info.Split("=")[1].split(",").split("|")[0]}
        $table | Add-Member -NotePropertyName InCloud -NotePropertyValue  $GroupProperties.InCloud
        $table | Add-Member -NotePropertyName Created -NotePropertyValue  $GroupProperties.Created
        $table | Add-Member -NotePropertyName Owner2 -NotePropertyValue $GroupProperties.BusinessOwner.Split("=")[1].Split(",")[0]
        $table | Add-Member -NotePropertyName Description -NotePropertyValue  $GroupProperties.Description
        $table | Add-Member -NotePropertyName Owner3 -NotePropertyValue  $GroupProperties.info   
        $table | Add-Member -NotePropertyName atttrib12 -NotePropertyValue  $GroupProperties.extensionAttribute12
        $tableStorage += $table
}

$tableStorage |  Export-Csv -Path $output'.csv' -NoTypeInformation -Force 

output: Exception calling "GetSteppablePipeline" with "1" argument(s): "The parameter is incorrect. "

Upvotes: 0

Views: 331

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 61293

Try this code instead, it should be faster than what you're doing. In addition, it should help you understand the errors you're getting.

As you said, you were getting the You cannot call a method on a null-valued expression exception. One possible way you're getting this exception is for example, let's assume that the value for $adGroup.info is this:

$string = 'This is the group information without the equal sign'

Now if we try to split the string as you're doing:

$string.Split("=")[1].split(",").split("|")[0]

You would get that exact same error:

You cannot call a method on a null-valued expression.
At line:1 char:1
+ $string.Split("=")[1].split(",").split("|")[0]

Code

$ErrorActionPreference = 'Stop'
$groupUsersList = Get-Content $Path
$destinationPath = "$HOME\Documents\output.csv"
$tableStorage = [system.collections.generic.list[pscustomobject]]::new()

foreach($group in $groupUsersList)
{
    if([string]::IsNullOrWhiteSpace($group))
    {
        continue
    }

    try
    {
        $hash = @{
            Identity = $group
            Properties = @(
                'inCloud','BusinessOwner'
                'extensionAttribute12','Info'
                'Created','Description'
            )
        }

        $adGroup = Get-ADGroup @hash
    }
    catch
    {
        Write-Warning $_
        continue
    }

    try
    {
        $businessOwner = $adGroup.BusinessOwner.Split("=")[1].Split(",")[0]
    }
    catch
    {
        'Can't split this: {0}' -f $adGroup.BusinessOwner
        $businessOwner = $adGroup.BusinessOwner
    }
    
    try
    {
        $info = $adGroup.info.Split("=")[1].split(",").split("|")[0]
    }
    catch
    {
        'Can't split this: {0}' -f $adGroup.info 
        $info = $adGroup.info
    }

    try
    {
        $owner = $adGroup.extensionAttribute12.Split("=")[1].split(",").split("|")[0]
    }
    catch
    {
        'Can't split this: {0}' -f $adGroup.extensionAttribute12
        $owner = $adGroup.extensionAttribute12
    }

    $tableStorage.Add(
        [pscustomobject]@{
            Name = $adGroup.Name
            GroupCategory = $adGroup.GroupCategory
            Owner = $owner
            OwnerinfoID = $info 
            InCloud = $adGroup.InCloud
            Created = $adGroup.Created
            Owner2 = $businessOwner
            Description = $adGroup.Description
            Owner3 = $adGroup.info   
            atttrib12 = $adGroup.extensionAttribute12
    })
}

if($tableStorage)
{
    $tableStorage | Export-Csv -Path $destinationPath -NoTypeInformation -Force
    "CSV Exported successfully to $destinationPath"
}

A few points to consider

  • Adding items to System.Array @() is a lot slower than adding items to a collections.generic.list.

  • Creating objects with new psobject + Add-Member is a lot slower than casting objects with pscustomobject.

  • Calling ALL properties on Get-ADGroup -Properties * is a lot slower than calling only the properties you need -Properties prop1,prop2,prop3.

Upvotes: 3

Related Questions