我正在阅读包含我正在将审核设置应用到的文件和文件夹的文本文件的内容。这些文件夹在SACL上有几个不同的设置,所以我想过滤以查找所有文件并执行操作,然后仅对目录执行不同的操作。我无法找到过滤/区分两者的方法。我正在使用PowerShell v2.0,升级可能不是一种可能性。Powershell筛选内容以查找文件或目录
这里是我的代码,我知道这是行不通的,但给我的思考一个想法:
import-module NTFSSecurity
$Targetfiles = Get-Content c:\temp\test.txt
if($Targetfiles -match ".exe or .dll or .msc"){
$files = $Targetfiles -match ".exe or .dll or .msc"
foreach ($File in $files){
Get-NTFSAudit -Path $files | Remove-NTFSAudit -PassThru
Add-NTFSAudit -Path $files -Account 'NT Authority\Everyone' -AccessRights FullControl -Type Failure -AppliesTo ThisFolderOnly
Add-NTFSAudit -Path $files -Account 'NT Authority\Everyone' -AccessRights ExecuteFile, AppendData, Delete, ChangePermissions, TakeOwnership -Type Success -AppliesTo ThisFolderOnly
}
}
else{
$directories = $Targetfiles -notmatch ".exe or .dll or .msc"
foreach ($Directory in $directories){
Get-NTFSAudit -Path $directories | Remove-NTFSAudit -PassThru
Add-NTFSAudit -Path $directories -Account 'NT Authority\Everyone' -AccessRights FullControl - Type Failure -AppliesTo ThisFolderOnly
Add-NTFSAudit -Path $directories -Account 'NT Authority\Everyone' -AccessRights ExecuteFile, AppendData, ReadData, CreateFiles, Delete, ChangePermissions, TakeOwnership -Type Success -AppliesTo ThisFolderOnly
}
}
显然-match/-notmatch不工作。我希望脚本检查所有带有扩展名的项目,将它们放到$ files中,然后执行任何没有扩展名的工作,进入$目录并执行此操作。我还在学习,所以我的逻辑可能无法正常工作。我试过了 ”。”与 - 匹配,但它似乎没有做任何事情。
该模块可以在这里找到:https://gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85#content
感谢您的帮助!
非常感谢,不幸的是,我相信-in表达式是在PowerShell 3.0中引入的。我在一个严格控制的网络上升级是不可能的:( – watcherseye 2014-11-14 16:35:08
@watcherseye你可以把它分开像'((Get-Item $ _)。Extension -eq'.exe') - 或者(Get-Item $ _)。Extension -eq'.dll') - 或(Get-Item $ _)。Extension -eq'.msc'))'。它的可读性较差。 – 2014-11-14 16:37:34