# AD模組
Import-Module ActiveDirectory
# 指定多個 OU
$ouPaths = @(
"OU=AEG,DC=test,DC=local",
"OU=TW,DC=test,DC=local",
"OU=TJ,DC=test,DC=local",
"OU=LY,DC=test,DC=local"
)
# 结果輸出 CSV 文件
$exportPath = "D:\ADAccountList.csv"
# 初始化
$allUsers = @()
# 迴圈處理每個 OU
foreach ($ouPath in $ouPaths) {
$users = Get-ADUser -Filter * -Properties "sn", "givenName", "displayName", "telephoneNumber", "sAMAccountName", "mail", "PasswordNeverExpires", "Enabled", "AccountExpirationDate" -SearchBase $ouPath
$allUsers += $users
}
# 结果輸出 CSV 文件
$allUsers | Select-Object sn, givenName, displayName, telephoneNumber, sAMAccountName, mail, PasswordNeverExpires,
@{
Name = "Status"
Expression = {
# 依照 Enabled 屬性顯示在職或離職
if ($_.Enabled) {
"在職"
} else {
"離職"
}
}
},
@{
Name = "AccountExpirationDate"
Expression = {
# 格式化 AccountExpirationDate 為 yyyy/mm/dd
if ($_.AccountExpirationDate) {
$_.AccountExpirationDate.ToString("yyyy/MM/dd")
} else {
"未設置"
}
}
} | Export-Csv -Path $exportPath -NoTypeInformation -Encoding UTF8
Write-Host "文件路径: $exportPath"
沒有留言:
張貼留言