将域用户加入拨入属性

该拨入属性,结合AD的【路由和访问】角色,以使用VPN功能。

  1. <#
  2. By GuoRuilong
  3. Date: 2020-12-3
  4. Description:Enable the dial-in property permission
  5. #>
  6. Import-Module ActiveDirectory
  7. Write-Host "---此工具用于开通AD域账号的VPN权限---" -ForegroundColor DarkRed
  8. while(1){
  9. $USER = Read-Host -Prompt "请输入AD域账号"
  10. # 查询该账户是否存在
  11. $NUM = (dsquery user -samid $USER.Trim()).count
  12. if($NUM -eq 1){
  13. # 检测msNPAllowDialin(拨入属性)的布尔值
  14. $vpnPriv = (Get-ADUser $USER.Trim() -Properties *)['msNPAllowDialin']
  15. if(!$vpnPriv){
  16. Write-Host "正在启用账号,请稍后......" -ForegroundColor DarkCyan
  17. # 设置属性msNPAllowDialin为True
  18. Set-ADUser $USER.Trim() -Replace @{msNPAllowDialin=$true}
  19. # 延迟5秒再查询权限
  20. sleep 5
  21. Write-Host "提示:已为该用户添加VPN权限!" -ForegroundColor DarkYellow
  22. (Get-ADUser $USER.Trim() -Properties * |fl @{name='VPN权限'; Expression={$_.msNPAllowDialin}} | out-String).trim()
  23. Write-Host "----------------------------------------"
  24. }else{
  25. Write-Warning "该用户已拥有VPN权限,无需重复开通!"
  26. Write-Host "----------------------------------------"
  27. }
  28. }else{
  29. Write-Warning "AD中没有该账号 - $USER"
  30. }
  31. Write-Host "请按组合键Ctrl+C退出该程序!`n" -ForegroundColor DarkRed
  32. }

说明:

  • 通过-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 ``` *说明:

  1. Filter参数必须使用单引号,否则会报错;
  2. 若要获取msNPAllowDialin的值,需指定-Properties *-Properties msNPAllowDialin
  3. 创建一个哈希表@{key=value},用来指定属性别名。
    1. 第一个键可以是Name、N、Label或L,值为自定义的属性名称;
    2. 第二个键可以是expression或E,值为属性名,通过$_可以读取管道对象的属性。