根据“由...管理”的 Active Directory 属性对计算机进行动态分组

根据“由...管理”的 Active Directory 属性对计算机进行动态分组

公司政策要求每台服务器都指定一名管理员。此信息保存在计算机对象的“管理者”字段中。

在 SCOM 中创建一个通过此 AD 字段填充的动态组非常有用。而这正是我的问题所在。

我已经尝试过的步骤。

1.为 Windows-Computer 目标创建一个“Managed by”属性。此属性由 WMI 查询使用SELECT * FROM DS_computer属性DS_managedBy

2. 创建一个具有动态过滤器的组。(Object is Windows-Computer_Extended AND (Managed by Equals usr) AND True)

现在这在理论上似乎是合理的,但显然根本不起作用。

我试图创造的东西真的有可能实现吗?我该如何解决这个问题?

答案1

经过一番研究,我发现有人已经解决了这个问题。我修改了脚本。删除了最后几行,并添加了下面我需要的逻辑。它很乱,但效果很好。您需要更改的只是管理服务器和包含正则表达式(我们有多个域,但只希望对一个域进行这种自动化)。

<Functions from original script go here>

$ManagementServer = "<ManagementServerGoesHere>"

$ManagementGroup = GetSCOMManagementGroup -ms $ManagementServer
$Groups = Get-SCOMGroup -DisplayName "Managed-By *"
$Groups |% {
    $Group = $_
    $Group.DisplayName -match "Managed-By (\w{3})" | Out-Null
    $sAMAccountName = $Matches[1]
    $User = Get-ADUser -Filter {sAMAccountName -eq $sAMAccountName}
    $UserDN = $User.DistinguishedName
    $ADManagedByComputers = Get-ADComputer -Filter {ManagedBy -eq $UserDN}
    $SCOMManagedByComputer = Get-SCOMGroup $Group.id | Get-SCOMClassInstance
    [string]$InstancesToAdd = ""
    [string]$InstancesToRemove = ""

    $ADManagedByComputers | % {
        if($SCOMManagedByComputer.DisplayName -notcontains $_.DNSHostName) {
            $Agent = Get-SCOMAgent -DNSHostName $_.DNSHostName
            if($Agent) {
                Write-Host ($_.DNSHostName + " not in SCOM Group " + $Group.DisplayName) -ForegroundColor Yellow
                $InstancesToAdd = $InstancesToAdd + "," + $Agent.Id
             } else {
                Write-Host ($_.DNSHostName + " has no Agent installed!") -ForegroundColor Gray
             }
        } else {
            Write-Host ($_.DNSHostname + " already in SCOM Group " + $Group.DisplayName) -ForegroundColor Green
        }
    }

    $SCOMManagedByComputer | % {
        if($_.DisplayName -match "<InclusionRegex>") {
            if($ADManagedByComputers.DNSHostName -notcontains $_.DisplayName) {
                Write-Host ($_.DisplayName + " should not be in SCOM Group " + $Group.DisplayName) -ForegroundColor DarkYellow
                $Agent = Get-SCOMAgent -DNSHostName $_.DisplayName
                if($Agent) {
                    $InstancesToRemove = $InstancesToRemove + "," + $Agent.Id
                }
            }  
        } else {
            Write-Host ($_.DisplayName + " is not in the domain scope. Skipping.") -ForegroundColor Cyan
        }
    }

    $InstancesToAdd = $InstancesToAdd.Trim(",")
    $InstancesToRemove = $InstancesToRemove.Trim(",")


        $ManagementPackName = ($Group | Get-SCOMClass).ManagementPackName
        $ManagementPackID = (Get-SCManagementPack -Name $ManagementPackName)
        $MP = ValidateGroup -mg $ManagementGroup -mp $MP -groupID $Group.FullName
    If($InstancesToAdd -ne "" -and $InstancesToRemove -ne "") {
        $MP = UpdateGroup -mg $ManagementGroup -mp $ManagementPackID -groupID $Group.FullName -instancesToAdd $InstancesToAdd -instancesToRemove $InstancesToRemove
    }

    if($InstancesToAdd -ne "" -and $InstancesToRemove -eq "") {
        $MP = UpdateGroup -mg $ManagementGroup -mp $MP -groupID $Group.FullName -instancesToAdd $InstancesToAdd
    }

    if($InstancesToAdd -eq "" -and $InstancesToRemove -ne "") {
        $MP = UpdateGroup -mg $ManagementGroup -mp $MP -groupID $Group.FullName -instancesToRemove $InstancesToRemove
    }
}

相关内容