OK - this is what I did in the end:
$SPGroups
= Get-ADGroup
-Filter *
-SearchBase "OU=SHAREPOINT,OU=GROUPS,OU= etc"
-Properties Members
if (!($SPGroups))
{
Log " - No SharePoint Groups could be found"
exit 1
}
ForEach ($Group
in $SPGroups )
{
Log "$($Group.Name)"
Foreach ($dn
in $Group.Members)
{
if ( $dn.startswith("CN=S-1-5") )
{
$SIDText = ($dN.Split(","))[0].SubString(3)
$SID =
New-Object System.Security.Principal.SecurityIdentifier
$SIDText
$strSPUser = $($SID.Translate([System.Security.Principal.NTAccount]).Value)
}
else
{
$ADUser =
get-aduser $dn
-properties samaccountname,EmailAddress
$strSPUser =
"domain\$($ADUser.SamAccountName)"
}
Log " - $strSPUser"
$BillingRecord
= New-Object
System.Object
$BillingRecord
| Add-Member -type
NoteProperty -name
"SAMAccountName"
-value $strSPUser
$BillingRecord
| Add-Member -type
NoteProperty -name
"SMTPAddress"
-value "SMTPADDRESSHERE"
$BillingRecord
| Add-Member -type
NoteProperty -name
"SPGroup" -value
$Group.Name
$BillingOutput
+= $BillingRecord
}
# End loop of DN's in a group
}
# End loop of groups
$BillingOutput
| ft
Log
" - Adding SP Users to CSV File: "
$BillingOutput
| export-csv
-Path $SPCSVData
-Force -ErrorAction
SilentlyContinue -NoTypeInformation