简体   繁体   English

Powershell Active Directory 脚本 - 通过更改显示名称批量禁用

[英]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.

相关问题 如何使用Powershell更改Active Directory中的显示名称? - How can I change the display name in Active Directory with powershell? 使用Powershell列出活动目录用户显示名称,OU和上次密码更改日期 - list active directory User Display Name, OU and last password change date using powershell 使用 Powershell 中的脚本在 Active Directory 中创建/删除批量用户 - Creating/Deleteing Bulk Users to Active Directory Using a Script in Powershell 如何从powershell显示活动目录浏览器 - How to display active directory browser from powershell Powershell 从没有 AD 权限的 Active Directory 获取 LDAP 显示名称 - Powershell get LDAP display name from Active Directory without AD privileges 使用PowerShell的Active Directory更改网页字段 - Active Directory Change Webpage field using PowerShell 使用PowerShell在目录和子文件夹中重命名具有给定名称的批量文件 - Rename bulk files with given name, in directory and subfolders using PowerShell 在 PowerShell 脚本中读取目录中的文件 - read the files in Directory in PowerShell scripting 使用来自csv的powershell删除活动目录samaccount名称中的空白区域 - remove white space in active directory samaccount name with powershell from csv 如何在 PowerShell 中从 csv 查找 Active Directory 组名称? - How to look for Active Directory group name from csv in PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM