将域用户加入拨入属性
该拨入属性,结合AD的【路由和访问】角色,以使用VPN功能。
<#By GuoRuilongDate: 2020-12-3Description:Enable the dial-in property permission#>Import-Module ActiveDirectoryWrite-Host "---此工具用于开通AD域账号的VPN权限---" -ForegroundColor DarkRedwhile(1){$USER = Read-Host -Prompt "请输入AD域账号"# 查询该账户是否存在$NUM = (dsquery user -samid $USER.Trim()).countif($NUM -eq 1){# 检测msNPAllowDialin(拨入属性)的布尔值$vpnPriv = (Get-ADUser $USER.Trim() -Properties *)['msNPAllowDialin']if(!$vpnPriv){Write-Host "正在启用账号,请稍后......" -ForegroundColor DarkCyan# 设置属性msNPAllowDialin为TrueSet-ADUser $USER.Trim() -Replace @{msNPAllowDialin=$true}# 延迟5秒再查询权限sleep 5Write-Host "提示:已为该用户添加VPN权限!" -ForegroundColor DarkYellow(Get-ADUser $USER.Trim() -Properties * |fl @{name='VPN权限'; Expression={$_.msNPAllowDialin}} | out-String).trim()Write-Host "----------------------------------------"}else{Write-Warning "该用户已拥有VPN权限,无需重复开通!"Write-Host "----------------------------------------"}}else{Write-Warning "AD中没有该账号 - $USER"}Write-Host "请按组合键Ctrl+C退出该程序!`n" -ForegroundColor DarkRed}
说明:
- 通过
-ForegroundColor来指定提示内容的文字颜色; - 通过
Set-ADUser设置属性:Set-ADUser -Identity GlenJohn -Replace @{title="director";mail="glenjohn@fabrikam.com"}- 设置用户的title为”director”,mail为”glenjohn@fabrikam.com”
查询所有加入拨入属性的账号
```powershell Get-ADUser -SearchBase “DC=contoso,DC=com” -Filter ‘msNPAllowDialin -eq $true -and Enabled -eq $true’ -Properties * | Sort-Object SamAccountName | Format-Table SamAccountName, Enabled, @{name=”VPN权限”;expression={$_.msNPAllowDialin}} > d:\all_VPN_users.txt
dsquery查看
dsquery -Filter “(&(objectCategory=person)(objectClass=user)(msNPAllowDialin=TRUE))” -limit 1000 ``` *说明:
- Filter参数必须使用单引号,否则会报错;
- 若要获取msNPAllowDialin的值,需指定
-Properties *或-Properties msNPAllowDialin - 创建一个哈希表
@{key=value},用来指定属性别名。- 第一个键可以是Name、N、Label或L,值为自定义的属性名称;
- 第二个键可以是expression或E,值为属性名,通过$_可以读取管道对象的属性。
