Class CustomAdInfo {
$UserName
$Group
$Email
$Manager
}
[char] 'y'..[char] 'z' | ForEach-Object {
$getADUserSplat = @{
Filter = "Name -like '$_*'"
Properties = 'SamAccountName', 'memberOf', 'EmailAddress', 'Manager'
ResultPageSize = 256
SearchBase = 'CN=Users,DC=ab,DC=cd'
}
foreach ($user in Get-ADUser @getADUserSplat) {
foreach ($group in $user.MemberOf | Get-ADGroup) {
[CustomAdInfo]@{
UserName = $user.SamAccountName
Group = $group.Name # <= You need to choose an attribute here
Email = $user.EmailAddress
Manager = $user.Manager
}
}
}
} | Export-Csv 'C:\Users\Downloads\Members_and_Groups.csv'
Above code returns Field Manager value as CN=manager-id,CN=Users,DC=ab,DC=cd in csv. Manager field is bound by CN user type and Domain details. Expectation is to replace these, extract only manager-id from the string and assign it to class property Manager. Am a beginner, couldn't find anything helpful on the internet. Anyone could help me here?
Add a constructor to your class definition where you can extract the common name from the manager's distinguished name. Details on the regex pattern used in the class .ctor
can be found here: https://regex101.com/r/tAx4jM/1.
class CustomAdInfo {
$UserName
$Group
$Email
$Manager
CustomAdInfo($user, $group) {
$this.UserName = $user.SamAccountName
$this.Group = $group.Name
$this.Email = $user.EmailAddress
$this.Manager = $user.Manager -replace '^CN=|(?<!\\),.+'
}
}
[char[]] ([char] 'a'..[char] 'z') | ForEach-Object {
$getADUserSplat = @{
Filter = "Name -like '$_*'"
Properties = 'SamAccountName', 'memberOf', 'EmailAddress', 'Manager'
ResultPageSize = 256
SearchBase = 'CN=Users,DC=ab,DC=cd'
}
foreach ($user in Get-ADUser @getADUserSplat) {
foreach ($group in $user.MemberOf | Get-ADGroup) {
[CustomAdInfo]::new($user, $group)
}
}
} | Export-Csv 'C:\Users\Downloads\Members_and_Groups.csv'