审计成功和失败事件到文件夹

Auditing Success and Failure event to folder

我正在使用 PowerShell 添加用户以审核 Windows10 中的文件夹。 我正在使用此代码为审核设置 "EVERYONE"。 但是我需要为失败制定特殊规则,为成功和失败制定特殊规则,所以我需要将它保存在两行不同的行中。 - 喜欢这张照片:

这是我正在使用的代码:

$Folders = "C:\windows\system32\config"

Foreach ($Folder in $Folders) {
    Write-Host "" # Empty line
    Write-Host "Applying Auditing for folder", $Folder
    Write-Host "" # Empty line

    $ACL = Get-Acl $Folder

    # Set Auditing for Success event for above Folders for EVeryone group
    $PermAudited = "CreateFiles"
    $AccessRule = New-Object System.Security.AccessControl.FileSystemAuditRule("Everyone", $PermAudited, "Failure")
    $ACL.SetAuditRule($AccessRule)

    # Set Auditing for Success event for Top folder
    Write-Host $Folder, "for auditing Success event"
    $ACL | Set-Acl $Folder
}

您可以使用 System.Security.AccessControl.FileSystemRights 枚举指定这些规则,检查可用规则如下:

[enum]::GetNames([System.Security.AccessControl.FileSystemRights])

基本上,您需要查看 FileSystemAuditRule 的构造函数之一,以了解您需要如何设置它,根据您的需要,我认为这是正确的:

FileSystemAuditRule(
string identity,
FileSystemRights fileSystemRights,
AuditFlags flags
)

因此,您需要设置 RightsAuditFlags,根据您的示例,它应该是这样的:

$Rights = "ReadAndExecute","Modify"
$Flags = "Failure"

$AccessRights = [System.Security.AccessControl.FileSystemRights]$Rights 
$AuditFlags = [System.Security.AccessControl.AuditFlags]$Flags 

然后像这样设置 ACL:

$ACL = Get-Acl $Folder
$AccessRule = New-Object System.Security.AccessControl.FileSystemAuditRule("Everyone",$AccessRights, $AuditFlags)
$ACL.SetAuditRule($AccessRule)
Set-Acl -Path $Folder -AclObject $ACL