我有一个PS脚本,该脚本循环AD获取lastlogon属性以及其他属性。 我想获取displayName,SamAccountName,WhenCreated,WhenChanged,专有名称和上次登录时间。 现在一切正常,除了输出CSV缺少displayName,whenCreated和whenChanged属性外。
我很困惑它适用于某些属性,而不适用于其他属性。
这是我的代码:
Import-Module ActiveDirectory
function Get-ADUsersLastLogon() {
$dcs = Get-ADDomainController -Filter {Name -like "*"}
$users = Get-ADUser -Filter * -SearchBase "ou=Other Users,ou=Users,ou=Items,dc=mydc,dc=,dc=com"
$time = 0
$exportFilePath = "c:\tmp\lastLogon $(get-date -f dd-MM-yyyy).csv"
$columns = "name;username;whencreated;whenchanged;DNname;datetime"
Out-File -FilePath $exportFilePath -Force -InputObject $columns
foreach ($user in $users) {
foreach ($dc in $dcs) {
$hostname = $dc.HostName
$currentUser = Get-ADUser $user.SamAccountName |
Get-ADObject -Server $hostname -Properties lastLogon
if ($currentUser.LastLogon -gt $time) {
$time = $currentUser.LastLogon
}
}
$dt = [DateTime]::FromFileTime($time)
$row = $user.displayName + ";" + $user.SamAccountName + ";" +
$user.whenCreated + ";" + $user.whenChanged + ";" +
$user.distinguishedName + ";" + $dt
Out-File -FilePath $exportFilePath -Append -NoClobber -InputObject $row
$time = 0
}
}
Get-ADUsersLastLogon