使用 powershell 將審核規則新增至 Active Directory 組織單位

使用 powershell 將審核規則新增至 Active Directory 組織單位

在下面的 powershell 腳本中,我會收集特定 OU 的 Active Directory 審核規則,檢查是否有針對失敗的審核規則,如果不存在則新增失敗審核規則。一切正常,直到新增失敗規則的最後一步。沒有錯誤訊息,但新規則未出現在 Active Directory 中。

#find the needed OU
Import-Module activedirectory
Set-Location ad:
dir

#get an acl object based for the OU
$acl = Get-Acl -Path "DC=something,DC=somethingElse" -Audit
$auditRules = $acl.GetAuditRules(1,1,[System.Security.Principal.SecurityIdentifier])
$foundFailRule = $false
foreach ($rule in $auditRules) {
    if ($rule.AuditFlags -ieq "Failure") {
        $foundFailRule = $true
        break
    }
}
if ($foundFailRule -eq $true) {
    Write-Host "Found fail rule.  No action needed."
}
else {
    #find the IdentityReference from the first audit rule entry in the OU
    $identityReference = $auditRules[0].IdentityReference
    $activeDirectoryRights = "GenericAll"
    $auditFlags = "Failure"
    $failAuditRule = New-Object System.DirectoryServices.ActiveDirectoryAuditRule $identityReference, $activeDirectoryRights, $auditFlags
    $acl.AddAuditRule($failAuditRule)
}

不確定這是否重要,但我注意到在 OU 的預設審核規則中,有時 ObjectType 和 InheritedObjectType 是全零的字串,而其他時候它包含一些非零資料。使用上面的建構函數方法,這些屬性預設為全零。

知道為什麼 AddAuditRule 方法沒有在 OU 上產生新規則嗎?既然沒有錯誤,也許它正在添加一條規則,但它正在 Active Directory 中的意外位置填充?

相關內容