Hi Guys
I need to extract all AD users and all their groups from the memberof, including the primary group, the "Memberof" attribute does not get the primary group, so I developed the script below to get the primary group as well.
The script works well in environments with few users, however in environments with many users the script crashes and does not complete the extraction.
Does anyone know how I should adjust the script so that I can extract the information this way?
Thank you in advance
Function CriarObjeto($users){
$properties = [ordered] @{
'valor fixo1' = "USR";
'valor fixo2' = ":";
'ENVIRONMENT NAME' = $textBoxEnvironmentName.text;
'valor fixo3' = ":";
'DEVICE' = $textBoxDeviceDomain.text;
'valor fixo4' = ":";
'LOGIN'=$users.SamAccountName;
'valor fixo5' = ":";
'valor fixo6' = "0";
'valor fixo7' = ":";
'valor fixo8' = "0";
'valor fixo9' = ":";
'FULL NAME'=$users.Name;
'valor fixo10' = ":";
'LABEL' = $users.Description;
'valor fixo11' = ":";
'valor fixo12' = "0";
'valor fixo13' = ":";
'LAST LOGON' =[dateTime]::FromFileTime($users.LastLogonTimestamp).ToString("MM/dd/yyyy");
'valor fixo14' = ":";
'PRIVILEGE'= (((($(Get-ADPrincipalGroupMembership $users.SamAccountName).distinguishedName) -replace "\,.*") -replace "CN=","" ) -join ",");
'valor fixo15' = ":";
'ACCOUNT STATUS' = if(($users.Enabled -eq $true)) {'1'} Else {'2'};
'valor fixo16' = ":";
}
$object = New-Object -TypeName psobject -Property $properties
return $object
}
$users = @()
$users = Get-ADUser -ldapfilter "(objectclass=user)" -properties *
$resutls = @()
foreach($user in $users){
$resutls += CriarObjeto($user)
}
$resutls | Export-Csv -Path $CsvReportFile -NoTypeInformation -Encoding Utf8