Powershell将域控制器名称添加到CSV

时间:2015-10-13 21:43:25

标签: powershell

我的CSV中有一个小错误。我只想尝试让我的CSV为用户列出属性,并在最后一栏中说明从中提取数据的特定DC:

$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

我正在寻找域控制器'下列: Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

我觉得添加一个-expandproperty name是合适的,但我似乎无法弄清楚它会去哪里。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

计算属性中Expression条目的值必须为ScriptBlock。变化:

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

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

将属性列表放在数组中也可以帮助您更好地阅读和维护脚本:

$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