1

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.

1 Answer 1

1

The value of the Expression entry in a calculated property must be a ScriptBlock. Change:

@{Name="Domain Controller";Expression=$DomainController.name}

to

@{Name="Domain Controller";Expression={$DomainController.Name}}

Putting the list of properties in an array may also help you read and maintain your script better:

$Properties = @(
    'SamAccountName', 
    'DisplayName', 
    'LastLogonDate', 
    'LogonCount', 
    'Enabled', 
    'PasswordExpired', 
    'PasswordLastSet', 
    'PasswordNeverExpires', 
    'PasswordNotRequired', 
    'CannotChangePassword', 
    'AccountExpirationDate', 
    'AccountExpires', 
    'WhenCreated', 
    'canonicalName' 
)
$DCName = $DomainController.Name

$Select = $Properties + @{Name="DomainController";Expression={$DCName}}
Get-ADUser -Filter * -Properties $Properties -Server $DCName| Select $Select | sort DomainController,SamAccountName,LastLogonDate
Sign up to request clarification or add additional context in comments.

2 Comments

Was your college major Being Awesome, or were you just born that way? Thanks!
@NateR np. And no, I dropped out of high school to study the subtle art of solving real-world problems, worked out pretty well :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.