【问题标题】:Monitor Permission changes on Shared Folder using PowerShell使用 PowerShell 监控共享文件夹的权限更改
【发布时间】:2021-07-23 22:25:15
【问题描述】:

我正在尝试编写一个脚本来监控共享文件夹权限的更改,但找不到任何东西。根据所附图片,如果有人尝试添加/删除任何组或用户或在此处更改权限,则应通知我用户详细信息和时间。 欢迎您提出任何建议或参考。

Shared folder which is used by others for accessing files

【问题讨论】:

  • 您找不到什么?怎么写,或者符合你描述的代码?你能告诉我们你的尝试吗?
  • @AbrahamZinala 我使用下面 Rich Moss 提供的相同模板“powershell.one/tricks/filesystem/filesystemwatcher”来监控文件和文件夹,但无法弄清楚如何监控会发生的更改安全权限,如组或用户被添加到那里。

标签: powershell automation acl servicenow fileserver


【解决方案1】:

您可能正在寻找FilesystemWatcher,但您需要进行这些代码更改以监控更改的安全性:

# specify the file or folder properties you want to monitor:
$AttributeFilter = [System.IO.NotifyFilters]::Security 

# specify the type of changes you want to monitor:
$ChangeTypes = [System.IO.WatcherChangeTypes]::Changed

请注意,此脚本必须始终运行以监控更改。可能无法监控远程共享。

编辑:这是从上面的链接中提取的一个最小示例,用于监视安全性或文件内容的更改。按照建议,我从异步版本开始捕获所有事件,而不仅仅是第一个:

try {
  $watcher = New-Object IO.FileSystemWatcher -Property @{
    Path = [Environment]::GetFolderPath('Desktop')
    Filter = '*'
    IncludeSubdirectories = $true
    NotifyFilter = @([IO.NotifyFilters]::Security, [IO.NotifyFilters]::LastWrite) #add any other notify filters to this array
    EnableRaisingEvents = $true
  }
  $handlers = .{#add any other events to listen for
    Register-ObjectEvent -InputObject $watcher -EventName 'Changed' -Action {Write-Host "`nChanged: $($event | ConvertTo-Json -Depth 5)"}
    Register-ObjectEvent -InputObject $watcher -EventName 'Deleted' -Action {Write-Host "`nDeleted: $($event | ConvertTo-Json -Depth 5)"}
  }
  Write-Warning "FileSystemWatcher is monitoring $($watcher.NotifyFilter) events for $($watcher.Path)"
  do{
    Wait-Event -Timeout 1
    Write-Host "." -NoNewline     # write a dot to indicate we are still monitoring:
  } while ($true)# the loop runs forever until you hit CTRL+C    
}finally{#release the watcher and free its memory
  $handlers | %{Unregister-Event -SourceIdentifier $_.Name }
  $handlers | Remove-Job
  $watcher.Dispose() 
  Write-Warning 'FileSystemWatcher removed.'
}

【讨论】:

  • 我使用相同的模板进行监控,但无法弄清楚如何监控安全权限上的更改(例如添加或删除组或用户)。我将尝试应用 $AttributeFiIlter 和 $ChangeTypes 并检查是否可以成功使用它。很快就会回复您。
  • 您可以将它们与应该存在的内容进行比较。请提供代码
  • @AbrahamZinala 我不擅长 PowerShell,所以我刚刚使用了 Rich 强调使用和厌倦的模板.. 对其进行的改动不多,但我想获得用户详细信息将对共享路径的安全权限进行任何更改。 Rich Moss 脚本也是一样的。你可以参考一下。我刚刚添加了要监控的文件名和目录名。
  • @RichMoss 谢谢。我已经尝试了您的建议和提供的脚本,但它对用户详细信息没有帮助。例如:[2]:i.stack.imgur.com/t7HoJ.png - 参考图片...在共享路径上,我正在尝试添加用户“Demo.user”并授予他写入权限。所以我希望脚本提供我今天在路径上添加 Demo.user 并具有写入权限的详细信息。这是我在捕捉细节方面滞后的地方。希望我能解释一下。
  • 当当前用户的桌面和子文件夹的权限发生变化时,我提供的脚本会报告所有$event 属性。您可以更改-Action 参数以将今天的日期/时间与路径一起写入,但该事件不包含有关更改权限的所有详细信息。您可以在启动watcher之前更改代码写入当前权限,然后更改-Action参数以写出更改的权限。
猜你喜欢
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2016-10-18
  • 2015-05-26
  • 1970-01-01
  • 2022-11-22
  • 2010-10-24
  • 1970-01-01
相关资源
最近更新 更多