Powershell筛选内容以查找文件或目录

Powershell筛选内容以查找文件或目录,powershell,Powershell,我正在读取一个文本文件的内容,其中包括我正在应用审核设置的文件和文件夹。这些文件夹在SACL上有一些不同的设置,所以我想筛选所有文件并执行一个操作,然后只对目录执行一个不同的操作。我很难找到一种方法来过滤/区分这两者。我正在使用PowerShell v2.0,可能无法升级 以下是我的代码,我知道它不起作用,但给出了我的想法: import-module NTFSSecurity $Targetfiles = Get-Content c:\temp\test.txt if($Targetfil

我正在读取一个文本文件的内容,其中包括我正在应用审核设置的文件和文件夹。这些文件夹在SACL上有一些不同的设置,所以我想筛选所有文件并执行一个操作,然后只对目录执行一个不同的操作。我很难找到一种方法来过滤/区分这两者。我正在使用PowerShell v2.0,可能无法升级

以下是我的代码,我知道它不起作用,但给出了我的想法:

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不起作用。我希望脚本检查所有具有扩展名的项,将它们放入$文件中,然后执行该工作以及任何没有扩展名的操作,进入$目录并执行该工作。我还在学习,所以我的逻辑可能行不通。我试过了。使用-match,但它似乎没有任何作用

可在此处找到该模块:


谢谢你的帮助

我没有使用过该特定模块,因此无法在那里调试任何东西,但对于您的逻辑,我将使用以下内容:

Import-Module NTFSSecurity;
Get-Content C:\Temp\test.txt | ForEach-Object {
    If (Test-Path $_ -PathType Leaf) {
        If ((Get-Item $_).Extension -in ('.exe','.dll','.msc')) {
            #Apply executable file rule
        }
    } ElseIf (Test-Path $_ -PathType Container) {
        #Apply folder rule
    } Else {
        Write-Output "Path not found: $_";
    }
}
导入模块。 获取文件的内容 对于内容的每一行。。。 如果它是一个叶对象,也就是一个文件,请检查它是否具有.exe、.dll或.msc扩展名。如果是,请对文件应用NTFSSecurity命令。 如果它是一个容器aka,一个目录,请为目录应用NTFSSecurity命令。 如果它不是叶子或容器,那是因为它根本不是有效的路径。
我的语法有点迂腐,可以改进,但这应该可以让你开始。我不确定您想要实现什么,这是最简单的版本。

非常感谢您,不幸的是,我相信-in表达式是在PowerShell 3.0中引入的。我在一个严格控制的网络上,升级是不可能的:@watcherseye你可以像Get Item$\扩展名-eq.exe'-或Get Item$\扩展名-eq.dll'-或Get Item$\扩展名-eq.msc'那样将其拆分。只是可读性较差。