禁用某个特定GPO的所有链接
我需要使用PowerShell为特定的GPO禁用所有链接。
我知道一个简单的命令是
Set-GPLink -Name $gpoName -Target $targetPath -LinkEnabled No
如果我想一次对一个OU来做,这个方法就能工作。我想一次性为特定的GPO禁用所有链接。为此,我可以使用下面类似的脚本:
$gpoName = 'Test-Do-Not-Use'
$gpo = Get-GPO -Name $gpoName
[xml]$gpoReport = Get-GPOReport -Name $gpoName -ReportType Xml
$links = $gpoReport.GPO.LinksTo
foreach ($link in $links) {
$targetPath = $link.SOMPath
Set-GPLink -Name $gpoName -Target $targetPath -LinkEnabled No
Write-Host "Disabled link for '$gpoName' at: $targetPath"
}
问题在于 -Target 正在查找LDAP Distinguished Name (ou=MyOU,dc=contoso,dc=com),而 SomPath 把它列为 contoso.com/MyOU。
是否有办法使用 Set-GPlink 命令删除特定GPO的所有链接,或者有没有办法把 SOMPath 转换为LDAP Distinguished Name的格式?
先行感谢你提供的任何帮助和建议
解决方案
在 .SOMPath 中得到的实际上是一个 canonical name,确实不能把它作为 Set-GPLink 的目标。可以用来把canonical name转换为区分名格式(DN)的,是 IADsNameTranslate interface…… 这可能有点复杂,但这比你能找到的任何用于从canonical name翻译成区分名格式的正则表达式都要安全得多(有很多)。
我在这里添加注释,方便你跟随我的思路:
# Ref: https://learn.microsoft.com/en-us/windows/win32/api/iads/ne-iads-ads_name_type_enum
$ADS_NAME_TYPE_1779 = 1 # Format of DistinguishedName
$ADS_NAME_TYPE_CANONICAL = 2 # Format of CanonicalName
# Ref: https://learn.microsoft.com/en-us/windows/win32/api/iads/ne-iads-ads_name_inittype_enum
$ADS_NAME_INITTYPE_GC = 3 # Query to Global Catalog
$nt = New-Object -ComObject NameTranslate
$nt.Init($ADS_NAME_INITTYPE_GC, $null)
$map = @{} # <- we avoid translating more than once...
$gpoName = 'Test-Do-Not-Use'
[xml] $gpoReport = Get-GPOReport -Name $gpoName -ReportType Xml
foreach ($link in $gpoReport.GPO.LinksTo) {
$targetPath = $link.SOMPath
# if we don't know yet the DN for this CanonicalName
if (-not $map.ContainsKey($targetPath)) {
# set the format to CanonicalName
$null = $nt.Set($ADS_NAME_TYPE_CANONICAL, $targetPath)
# get it in the format of DistinguishedName
$dn = $nt.Get($ADS_NAME_TYPE_1779)
if ([string]::IsNullOrEmpty($dn)) {
Write-Warning "Could not translate '$targetPath'."
}
# add it to the hash table, even if null,
# the key should be there to avoid querying more than once
$map[$targetPath] = $dn
}
# get the translated DistinguishedName
$dn = $map[$targetPath]
# and, if it's not null
if (-not [string]::IsNullOrEmpty($dn)) {
Set-GPLink -Name $gpoName -Target $dn -LinkEnabled No
Write-Host "Disabled link for '$gpoName' at: $targetPath"
}
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。