首页 > 解决方案 > 用于禁用 GPO 的计算机或用户部分的 Powershell 脚本

问题描述

我正在寻找一个 powershell 代码片段来禁用 Active Directory GPO 的计算机或用户部分。

我已经提取了相关 GPO 的 Guid,只需要找到一种方法来禁用计算机或用户部分,而不是整个 GPO。

对象是如果为空则禁用 GPO 的计算机部分,反之亦然。

谢谢。

标签: powershellactive-directorygpo

解决方案


像这样(您只需要更改DC=mydom,DC=addsmydom.adds字符串以匹配您的域):

Get-ChildItem "AD:\CN=Policies,CN=System,DC=mydom,DC=adds" | Select -ExpandProperty Name | ForEach-Object {
    if (-not (Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\MACHINE))
    {
        # No machine configuration, disable machine parameters
        Set-ADObject "CN=$_,CN=Policies,CN=System,DC=mydom,DC=adds" -Replace @{flags=2}
    }
    elseif (-not (Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\USER))
    {
        # No user configuration, disable user parameters
        Set-ADObject "CN=$_,CN=Policies,CN=System,DC=mydom,DC=adds" -Replace @{flags=1}
    }
}

您应该在运行之前关闭 gpmc.msc 以避免错误(gpmc 为 GPO 加载状态,并且当它们以这种方式更改时,它会检测到不一致,但事实并非如此),您可以在之后运行它来验证:)

欲了解更多信息

  1. flags = 0 : 全部启用
  2. flags = 3 : 全部禁用

或者最好将它们全部统治:

Get-ChildItem "AD:\CN=Policies,CN=System,DC=mydom,DC=adds" | Select -ExpandProperty Name | ForEach-Object {
    $flags = 0
    if (-not (Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\MACHINE))
    {
        $flags = 2
    }
    if (-not (Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\USER))
    {
        # Disable all if both are empty, and user only otherwise
        $flags += 1
    }
    Set-ADObject "CN=$_,CN=Policies,CN=System,DC=mydom,DC=adds" -Replace @{flags=$flags}
}

这需要有“干净”的 GPO。我的意思是,如果你有一个没有机器配置的 GPO,那么在组策略编辑器中,你转到机器策略的 Windows 设置部分,什么都不做,编辑器会将脚本文件夹结构添加到 GPO,所以Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\MACHINE)不会返回$null$flags也不会相应地设置为 2。使用更多代码,很容易清理那些空结构;):

Get-ChildItem "AD:\CN=Policies,CN=System,DC=mydom,DC=adds" | Select -ExpandProperty Name | ForEach-Object {
   $flags = 0
   $machineItems = Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\MACHINE
   $machineScripts = Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\MACHINE\Scripts -Recurse
   $userItems = Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\USER
   $userDocs = Get-ChildItem "\\mydom.adds\SYSVOL\mydom.adds\Policies\$_\USER\Documents & Settings" -Recurse
   $userScripts = Get-ChildItem \\mydom.adds\SYSVOL\mydom.adds\Policies\$_\USER\Scripts -Recurse
   if (-not ($machineItems) -or ($machineItems.Count -eq 1 -and $machineItems[0].Name -eq "Scripts" -and $machineScripts.Count -eq 2 -and $machineScripts[0].Name -eq "Shutdown" -and $machineScripts[1].Name -eq "Startup"))
   {
       # No machine configuration (or empty Scripts folders)
       # Optionally, you can delete extra Scripts folder here
       $flags = 2
   }
   if (-not ($userItems) -or ($userItems.Count -eq 2 -and $userItems[0].Name -eq "Documents & Settings" -and $userItems[1].Name -eq "Scripts" -and $userScripts.Count -eq 2 -and $userScripts[0].Name -eq "Logoff" -and $userScripts[1].Name -eq "Logon"))
   {
       # No user configuration (or empty 'Documents & settings' and Scripts folders)
       # Disable all if both are empty, and user only otherwise
       # Optionally, you can delete extra 'Documents & settings' and Scripts folders here
       $flags += 1
   }
   Set-ADObject "CN=$_,CN=Policies,CN=System,DC=mydom,DC=adds" -Replace @{flags=$flags}
}

如果您只想要一个特殊列表,请将其保存在以下格式的文本文件中:

{GUID1}
{GUID2}
...

并替换{Get-ChildItem "AD:\CN=Policies,CN=System,DC=mydom,DC=adds" | Select -ExpandProperty NameGet-Content C:\GPOsDatas\List.txt(替换为正确的文件路径)


推荐阅读