[英]Powershell Active Directory Scripting - Bulk disable with change of display name
I am looking for assistance in creating/completing a Powershell script that grabs a user's samAccountName from a .csv file, disables that user in a specific domain, eg "foo.bar", and then prepends their AD display name with a single character.我正在寻求帮助来创建/完成一个 Powershell 脚本,该脚本从 .csv 文件中获取用户的 samAccountName,在特定域中禁用该用户,例如“foo.bar”,然后在他们的 AD 显示名称前加上一个字符。 This is a bulk disable script, and it has to add that single character to the front/beginning of their display name.
这是一个批量禁用脚本,它必须将该单个字符添加到其显示名称的前面/开头。
What I have so far is:到目前为止我所拥有的是:
Import-Module ActiveDirectory
$Server = read-host "Enter Domain to query/domain controller"
Import-Csv "C:\Temp\samAccountNames.csv" | ForEach-Object {
$samAccountName = $_."samAccountName"
Get-ADUser -Server $Server -Identity $samAccountName | Disable-ADAccount
}
Now, what I need to do is to prepend the display name with the '#' character.现在,我需要做的是在显示名称前加上“#”字符。
(eg "Doe, John" becomes "#Doe, John") (例如“Doe,John”变成“#Doe,John”)
You need to check if the user can be found at all first, then update the displayname and disable the account您需要首先检查是否可以找到该用户,然后更新显示名称并禁用该帐户
Import-Module ActiveDirectory
$characterToPrepend = '#' # the character you want to prepend the DisplayName with
$Server = Read-Host "Enter Domain to query/domain controller"
Import-Csv "C:\Temp\samAccountNames.csv" | ForEach-Object {
$ADUser = Get-ADUser -Server $Server -Filter "SamAccountName -eq '$($_.samAccountName)'" -Properties DisplayName -ErrorAction SilentlyContinue
if ($ADUser) {
# test if the user is not already disabled
if (!$ADUser.Enabled) {
Write-Host "User '$($_.samAccountName)' is already disabled"
}
else {
$newDisplayName = $characterToPrepend + $ADUser.DisplayName
# set the new displayname and disable the user
$ADUser | Set-ADUser -DisplayName $newDisplayName -Enabled $false
}
}
else {
Write-Warning "User '$($_.samAccountName)' does not exist"
}
}
I'm using -Filter
to get the user rather than the -Identity
parameter because the latter will throw an exception when a user with that SamAccountName could not be found我使用
-Filter
来获取用户而不是-Identity
参数,因为当找不到具有该 SamAccountName 的用户时,后者将引发异常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.