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 の予期しない場所にルールが取り込まれている可能性があります。

関連情報