如何提示用户输入正确的部门名称并根据 Active Directory 对其进行验证
How to prompt users to enter the correct Department name and to verify it against Active Directory
我正在使用以下脚本从 AD 中的特定部门获取用户。脚本本身运行良好,但我想完成两件事:
- 为确保用户不能将部门名称留空,
- 验证部门名称是否存在于 Active Directory 中,如果不存在,则提示用户验证名称并输入正确的名称。
Clear-Host
Import-Module ActiveDirectory
Write-Host "Processing"
$Dept = Read-Host "Enter the desired department"
$Department = Read-Host -Prompt "Input the desired department name"
if ($Department -eq $null){
Write-Host "Department cannot be blank, please re-enter the correct department"
$strFilter = "(&(objectCategory=User)(Department=*$Dept*))"
$colResults = Get-ADUser -LDAPFilter $strFilter |
Select-Object -Expand DistinguishedName
您可以像这样从 AD 中提取部门列表:
$departments = Get-ADUser -Filter * -Property Department |
Where-Object { $_.Department } |
Select -Expand Department -Unique
使用该列表,您可以像这样验证您的用户输入:
do {
$Dept = Read-Host "Enter the desired department"
} until ($departments -contains $Dept)
我正在使用以下脚本从 AD 中的特定部门获取用户。脚本本身运行良好,但我想完成两件事:
- 为确保用户不能将部门名称留空,
- 验证部门名称是否存在于 Active Directory 中,如果不存在,则提示用户验证名称并输入正确的名称。
Clear-Host
Import-Module ActiveDirectory
Write-Host "Processing"
$Dept = Read-Host "Enter the desired department"
$Department = Read-Host -Prompt "Input the desired department name"
if ($Department -eq $null){
Write-Host "Department cannot be blank, please re-enter the correct department"
$strFilter = "(&(objectCategory=User)(Department=*$Dept*))"
$colResults = Get-ADUser -LDAPFilter $strFilter |
Select-Object -Expand DistinguishedName
您可以像这样从 AD 中提取部门列表:
$departments = Get-ADUser -Filter * -Property Department |
Where-Object { $_.Department } |
Select -Expand Department -Unique
使用该列表,您可以像这样验证您的用户输入:
do {
$Dept = Read-Host "Enter the desired department"
} until ($departments -contains $Dept)