2014-11-14 2 views
0

감사 설정을 적용 할 파일과 폴더가 포함 된 텍스트 파일의 내용을 읽고 있습니다. 폴더에는 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가 작동되지 않습니다. 스크립트에서 확장명이있는 모든 항목을 확인하고 $ 파일에 넣은 다음 확장명이없는 작업을 수행하고 $ 디렉토리로 이동하여 작업을 수행합니다. 나는 아직도 배우기 때문에 내 논리가 작동하지 않을 수 있습니다. 난 노력 했어 "." - 일치,하지만 아무것도하지 않는 것 같습니다.

모듈은 여기에서 찾을 수 있습니다 : https://gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85#content

어떤 도움 주셔서 감사합니다!

답변

0

내가 거기에 아무것도 디버깅 할 수 없습니다 그래서 그 특정 모듈 함께 일하지했지만 로직이 내가 사용하는 거라고 무엇 :

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: $_"; 
    } 
} 
  1. 가져 오기 모듈.
  2. 이 (일명, 파일) 잎 객체의 경우 .DLL, 그것은 .EXE를 가지고 있는지 확인 ...
  3. 을 파일 내용의 각 라인에 대한
  4. 의 콘텐츠를, 또는의 .msc 신장. 그렇다면 파일에 NTFSSecurity 명령을 적용하십시오.
  5. 컨테이너 (일명, 디렉토리) 인 경우 디렉토리에 NTFSSecurity 명령을 적용하십시오.
  6. 리프 나 컨테이너가 아닌 경우 올바른 경로가 아니기 때문입니다.

내 구문은 다소 의미가 있지만 개선 될 수 있지만 시작해야합니다. 나는 당신이 성취하려는 것을 확신하지 못하며 가장 간단한 버전입니다.

+0

대단히 감사합니다. 불행히도, -in식이 PowerShell 3.0에서 도입 된 것 같습니다. 나는 단단히 통제 된 네트워크에 있고 업그레이드는 불가능합니다. ( – watcherseye

+0

@watcherseye'((Get-Item $ _) .Extension -eq '.exe'와 같이) 따로 분리 할 수 ​​있습니다. -or (Get-Item $ _) .Extension -eq '.dll') - 또는 (Get-Item $ _) .Extension -eq '.msc'))'. 그것은 단지 읽기 쉽지 않습니다. –