来自csv文件的get-aduser lastlogon passwordexpire

时间:2017-07-12 20:29:08

标签: powershell csv active-directory passwords

下面的脚本为我提供了一个帐户的正确输入,当添加多个帐户时,它不起作用。如何让它适用于csv文件中列出的所有用户帐户?

$csv = import-csv "c:\users.csv"

foreach($user in $csv){
$Displayname = $user.Displayname
Get-aduser -filter {displayname -eq $displayname}`
-Properties displayName,employeeID,mail, "msDS-UserPasswordExpiryTimeComputed","lastLogonTimestamp" |`
 select "Displayname","Enabled",@{Name="PasswordExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}},@{N='LastLogonTime'; E={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},SamAccountName,employeeID,mail |`
Export-Csv "c:\temp\users1.csv"
}

1 个答案:

答案 0 :(得分:1)

您需要将-append开关添加到export-csv,否则每次循环时文件都会被覆盖,只留下列表中最后一个用户的详细信息

Export-Csv "c:\temp\users1.csv" -append