powershell - 用于禁用 GPO 的计算机或用户部分的 Powershell 脚本
问题描述
我正在寻找一个 powershell 代码片段来禁用 Active Directory GPO 的计算机或用户部分。
我已经提取了相关 GPO 的 Guid,只需要找到一种方法来禁用计算机或用户部分,而不是整个 GPO。
对象是如果为空则禁用 GPO 的计算机部分,反之亦然。
谢谢。
解决方案
像这样(您只需要更改DC=mydom,DC=adds
和mydom.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 加载状态,并且当它们以这种方式更改时,它会检测到不一致,但事实并非如此),您可以在之后运行它来验证:)
欲了解更多信息
- flags = 0 : 全部启用
- 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 Name
为Get-Content C:\GPOsDatas\List.txt
(替换为正确的文件路径)
推荐阅读
- python - 如何修复:模块“tensorflow”没有属性“会话”
- php - 魔术函数如何与 PHP 对象注入中的 unserialize() 一起工作?
- php - Mysqli fetch() 没有获取数据
- python - 如何在python中的空白行和单独的行之间只保留一个空白行?
- google-sheets - VLOOKUP 用于引用不同工作表的多个条件
- c++ - 在 boost 单元测试中使用 boost 文件系统全局夹具错误
- julia - 如何获得Julia中数据框所有列的算术平均值、最小值和最大值?
- php - 无法使用 AJAX Codeigniter 上传图片
- java - 一段时间后 Apache HttpClient 读取超时
- python - 读取 Numpy 数组中不同类型的列