I have a small error in my CSV. I'm just trying to get my CSV to kick out the properties listed for a user and a last column stating the specific DC from which it pulled that data:
$Path = Get-ScriptDirectory
$Date = (Get-Date).ToString('yyyy-MM-dd')
$Domain = Get-ADDomain | select -ExpandProperty ParentDomain
$DomainName = Get-ADDomain | select -ExpandProperty NetBIOSName
$Filename = "$Path\$DomainName" + "_Users_By_Last_DC_" + $Date + ".csv"
$DClist = Get-ADDomainController -Filter * | select name
$Statement = ForEach ($DomainController in $DClist){
Get-ADUser -Filter * -Properties SamAccountName, DisplayName, LastLogonDate, LogonCount, Enabled, PasswordExpired, PasswordLastSet, PasswordNeverExpires, PasswordNotRequired, CannotChangePassword, AccountExpirationDate, AccountExpires, WhenCreated, canonicalName -Server $DomainController.Name | select SamAccountName, DisplayName, LastLogonDate, LogonCount, Enabled, PasswordExpired, PasswordLastSet, PasswordNeverExpires, PasswordNotRequired, CannotChangePassword, AccountExpirationDate, AccountExpires, WhenCreated, canonicalName, @{Name="Domain Controller";Expression=$DomainController.name} | sort $DomainController.name, samAccountName, LastLogonDate}
$Statement | Export-Csv $FileName -NoTypeInformation
I'm getting for 'Domain Controller' the following: Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
I feel like adding an -expandproperty name would be appropriate, but I can't seem to figure out where it would go.
Any help would be appreciated.