Hi Community when i am running the below script i am getting error :===================== SETTINGS =====================
param([string]$TargetOwner = “user@domain.com”)
$ErrorActionPreference = ‘Stop’$ProgressPreference = ‘SilentlyContinue’
$SendOwnerEmails = $true$SendITEmail = $true
$DateSuffixTight = (Get-Date).ToString(‘dd-MM-yyyy_hh-mm_tt’)$DateSuffixDL = (Get-Date).ToString(‘dd-MM-yyyy hh-mm tt’).Replace(‘:’,‘-’)
$BaseOwner_Members = ‘C:\psscr\UnifiedGroups’$BaseUnified_IT = ‘C:\psscr\UnifiedGroups\ReportforIT’
$allFolders = @($BaseOwner_Members,$BaseUnified_IT)foreach($f in $allFolders){ if(-not (Test-Path $f)){ New-Item -ItemType Directory -Path $f -Force | Out-Null } }
$ITRecipients = @(‘test@test.in’)
========== MODULES & AUTH ========
Import-Module ImportExcelImport-Module ExchangeOnlineManagementConnect-ExchangeOnlineConnect-AzureAD
function New-OwnerSheetData {param([string]$SheetName,[string]$Members)foreach($m in $Members){ [PSCustomObject]@{ $SheetName = $m } }}
function Build-ColumnMatrix {param([hashtable]$MapDisplayToMembers)$headers = $MapDisplayToMembers.Keys$maxRows = ($MapDisplayToMembers.Values | ForEach-Object { $_.Count }) | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum$rows = @()for($i=0;$i -lt $maxRows;$i++){$row = [PSCustomObject]@{}foreach($h in $headers){$members = $MapDisplayToMembers[$h]$value = if($members.Count -eq 1 -and $i -eq 0){ $members } elseif($i -lt $members.Count){ $members[$i] } else { ‘’ }$row | Add-Member -MemberType NoteProperty -Name $h -Value $value -Force}$rows += $row}return $rows}
======== DATA COLLECTION ========
$TargetOwner = $TargetOwner.ToLower() # normalize once$OwnerData = @{}$M365_ITRows = @(); $SG_ITRows = @(); $DL_ITRows = @()
— M365 Groups —
$M365_allGroups = Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$.ResourceProvisioningOptions -contains ‘Team’ -or $.AccessType -in @(‘Private’,‘Public’) -or ($_.ResourceProvisioningOptions.Count -eq 0)}
$M365_MapForIT = @{}foreach($g in $M365_allGroups){$members = Get-UnifiedGroupLinks -Identity $g.Identity -LinkType Members -ResultSize Unlimited | Select-Object -ExpandProperty PrimarySmtpAddress$owners = Get-UnifiedGroupLinks -Identity $g.Identity -LinkType Owners -ResultSize Unlimited | Select-Object -ExpandProperty PrimarySmtpAddress$owners = $owners | ForEach-Object { $_.ToLower() }
if($TargetOwner -and ($owners -notcontains $TargetOwner)){ continue }
$M365_MapForIT["$($g.DisplayName) ($($g.PrimarySmtpAddress))"] = $members
foreach($o in $owners){
if(-not $OwnerData.ContainsKey($o)){ $OwnerData[$o] = @() }
$OwnerData[$o] += @{ GroupName = $g.DisplayName; Members = $members }
}
}$M365_ITRows = Build-ColumnMatrix -MapDisplayToMembers $M365_MapForIT
— Security Groups —
$mailEnabledSGs = Get-DistributionGroup -ResultSize Unlimited | Where-Object { $.RecipientTypeDetails -eq ‘MailUniversalSecurityGroup’ }$nonMailSGs = Get-AzureADGroup | Where-Object { $.SecurityEnabled -eq $true -and -not $.MailEnabled -and ($.GroupTypes -notcontains ‘Unified’) -and ($_.GroupTypes -notcontains ‘DynamicMembership’) }
$SG_MapForIT = @{}foreach($sg in $mailEnabledSGs){$members = Get-DistributionGroupMember -Identity $sg.Identity -ResultSize Unlimited | Select-Object -ExpandProperty PrimarySmtpAddress$owners = (Get-DistributionGroup -Identity $sg.Identity).ManagedBy | ForEach-Object { (Get-Recipient -Identity $_ -ErrorAction SilentlyContinue).PrimarySmtpAddress }$owners = $owners | Where-Object { $_ } | ForEach-Object { $_.ToLower() }
if($TargetOwner -and ($owners -notcontains $TargetOwner)){ continue }
$SG_MapForIT[$sg.DisplayName] = $members
foreach($o in $owners){
if(-not $OwnerData.ContainsKey($o)){ $OwnerData[$o] = @() }
$OwnerData[$o] += @{ GroupName = $sg.DisplayName; Members = $members }
}
}foreach($sg in $nonMailSGs){$members = Get-AzureADGroupMember -ObjectId $sg.ObjectId | Select-Object -ExpandProperty UserPrincipalName$owners = Get-AzureADGroupOwner -ObjectId $sg.ObjectId | Select-Object -ExpandProperty UserPrincipalName$owners = $owners | Where-Object { $_ } | ForEach-Object { $_.ToLower() }
if($TargetOwner -and ($owners -notcontains $TargetOwner)){ continue }
$SG_MapForIT[$sg.DisplayName] = $members
foreach($o in $owners){
if(-not $OwnerData.ContainsKey($o)){ $OwnerData[$o] = @() }
$OwnerData[$o] += @{ GroupName = $sg.DisplayName; Members = $members }
}
}$SG_ITRows = Build-ColumnMatrix -MapDisplayToMembers $SG_MapForIT
— Distribution Lists —
$DLs = Get-DistributionGroup -ResultSize Unlimited | Where-Object { $_.RecipientTypeDetails -eq ‘MailUniversalDistributionGroup’ }
$DL_MapForIT = @{}foreach($dl in $DLs){$members = Get-DistributionGroupMember -Identity $dl.Identity -ResultSize Unlimited | Select-Object -ExpandProperty PrimarySmtpAddress$owners = (Get-DistributionGroup -Identity $dl.Identity).ManagedBy | ForEach-Object { (Get-Recipient -Identity $_ -ErrorAction SilentlyContinue).PrimarySmtpAddress }$owners = $owners | Where-Object { $_ } | ForEach-Object { $_.ToLower() }
if($TargetOwner -and ($owners -notcontains $TargetOwner)){ continue }
$DL_MapForIT[$dl.DisplayName] = $members
foreach($o in $owners){
if(-not $OwnerData.ContainsKey($o)){ $OwnerData[$o] = @() }
$OwnerData[$o] += @{ GroupName = $dl.DisplayName; Members = $members }
}
}$DL_ITRows = Build-ColumnMatrix -MapDisplayToMembers $DL_MapForIT
====== PER OWNER FILE & EMAIL ======
foreach($owner in $OwnerData.Keys){if($TargetOwner -and $owner -ne $TargetOwner){ continue }
$filePath = Join-Path $BaseOwner_Members ("${owner}_Groups_${DateSuffixTight}.xlsx")
$first = $true
foreach($grp in $OwnerData[$owner]){
$sheet = $grp.GroupName -replace '[\\/:*?"<>|]','_'
(New-OwnerSheetData -SheetName $grp.GroupName -Members $grp.Members) | Export-Excel -Path $filePath -WorksheetName $sheet -AutoSize -Append:(!$first)
$first = $false
}
}
====== MASTER REPORT FOR IT =====
if($TargetOwner){$UnifiedPath = Join-Path $BaseUnified_IT (“Unified_Groups_Report_${DateSuffixTight}.xlsx”)if(@($M365_ITRows).Length -gt 0){$M365_ITRows | Export-Excel -Path $UnifiedPath -WorksheetName ‘M365Groups’ -AutoSize}if(@($SG_ITRows).Length -gt 0){$SG_ITRows | Export-Excel -Path $UnifiedPath -WorksheetName ‘SecurityGroups’ -AutoSize -Append}if(@($DL_ITRows).Length -gt 0){$DL_ITRows | Export-Excel -Path $UnifiedPath -WorksheetName ‘DistributionLists’ -AutoSize -Append}<#if($M365_ITRows.Count -gt 0){$M365_ITRows | Export-Excel -Path $UnifiedPath -WorksheetName ‘M365Groups’ -AutoSize}if($SG_ITRows.Count -gt 0){$SG_ITRows | Export-Excel -Path $UnifiedPath -WorksheetName ‘SecurityGroups’ -AutoSize -Append}if($DL_ITRows.Count -gt 0){$DL_ITRows | Export-Excel -Path $UnifiedPath -WorksheetName ‘DistributionLists’ -AutoSize -Append}#>}
Disconnect-ExchangeOnline -Confirm:$falseWrite-Host ‘Process complete.’ -ForegroundColor Green
The above script throws error as shown in below screenshot and this script is fetching data from O365 cloud groups but gives error of Active Directory as both are synced using ADConnect feature.
Error in Text Format:Active Directory rejected paged search cookie because a cookie handle was discarded by a Domain Controller or a different LDAP connection was used on subsequent page retrieval.Additional information: The parameter is incorrect.