安装 ActiveDirectory PowerShell 模块
对于 DC 而言, 在安装 AD DS 角色的时候会自动帮我们添加安装相关的 PowerShell 模块, 如果在其他 Windows Server 系统中可以通过下面 PowerShell 命令进行单独安装:
Add-WindowsFeature -Name RSAT-AD-PowerShell
查询和筛选 AD 对象
以下是常用的查询 cmdlet, 支持 -Filter
参数进行条件筛选, 需要注意的是 -Filter
参数后面需要传入的是一个字符串, 除了直接传入 *
作为通配符不用加引号, 其他内容都需要用单引号或双引号包裹起来. 另外过滤条件中的内容也是不区分大小写的(这就很 Windows style)
# 查找域中所有用户账户
Get-ADUser -Filter *
# 查找 lp 开头的用户账户
Get-ADUser -Filter 'Name -like "lp*"'
# 根据准确的用户名直接查找单个用户, 两种方式:
Get-ADUser -Filter "samAccountName -eq 'lpwm'"
Get-ADUser -Identity lpwm
# 查找 90 天内没有用过账户的用户
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 -UsersOnly
# 查看所有用户最近修改密码的时间
Get-ADUser -Filter * -Properties passwordlastset | Select name,passwordlastset | Sort-Object -Property passwordlastset -Descending
# 查找 30 天内没有修改密码的用户
$days = (Get-Date).AddDays(-30)
Get-ADUser -Filter "Enabled -eq 'True' -and passwordlastset -lt '$days'" | Select name
创建 AD 用户
# 在域 alian.com 的 Sales OU 中创建用户
New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -UserPrincipalName "jdoe@alian.com" -AccountPassword (ConvertTo-SecureString "P@ssw0rd!" -AsPlainText -Force) -Enabled $true -Path "OU=Sales,DC=alian,DC=com"
修改 AD 用户
Get-ADUser -Identity "jdoe" | Set-ADUser -City "Beijing"
查看 AD 用户
# 查看所有属性
Get-ADUser -Identity "jdoe" -Properties *
# 查看特定属性
Get-ADUser -Identity "jdoe" -Properties City | Select-Object City
删除 AD 用户
Remove-ADUser -Identity "jdoe" -Confirm:$false
查看 AD 默认密码策略
PS C:\Users\Administrator> Get-ADDefaultDomainPasswordPolicy
ComplexityEnabled : True
DistinguishedName : DC=alian,DC=com
LockoutDuration : 00:30:00
LockoutObservationWindow : 00:30:00
LockoutThreshold : 0
MaxPasswordAge : 42.00:00:00
MinPasswordAge : 1.00:00:00
MinPasswordLength : 7
objectClass : {domainDNS}
objectGuid : 093009b5-6229-4314-bb8b-5a7abb6af1ac
PasswordHistoryCount : 24
ReversibleEncryptionEnabled : False
其他操作
由于 AD 坑太大了, 其他操作直接摇 GPT 帮忙给写个代码示例再调试就行了 🤣