Background
Now that we have downloaded and installed Raimund Andrée’s NTFSSecurity in one of the standard PowerShell Module’s folder, we are ready to write a little test code and see how well it works.
Code
Script
getNTFSPermissions.ps1
param ( [string]$file , [string]$folder , [string]$fileExt ) Set-StrictMode -Version 2.0 #Import NTFSSecurity Import-Module NTFSSecurity # Declare variables [string] $CONST_FILEMODE_DIRECTORY = "d-----"; [boolean] $fileCheck = $false; if ([string]::IsNullOrEmpty($file)) { $fileCheck = $false; } else { $fileCheck = $true; } if ($fileCheck -eq $false) { if ([string]::IsNullOrEmpty($folder)) { $folder = Get-Location } } function getNTFSFile([string] $_fileLocal) { Get-NTFSAccess -Path $_fileLocal } #getNTFSFile() function getNTFSFolder([string] $folderLocal, [string] $fileExtLocal) { #Declare Local variables [string]$_file = $null; [string]$_fileFullName = $null; [string]$_fileExt = $null; [string]$_fileMode = $null; [boolean]$_fileExtMatch = $true; # Get files Get-ChildItem $folderLocal | foreach { $_file = $_ $_fileFullName = $_.FullName $_fileExt = $_.extension $_fileMode = $_.mode <# #$_file #$_fileFullName #$_fileMode #> $_fileExtMatch = $true; <# If we are matching on file extensions let us see whether it matches #> if ([string]::IsNullOrEmpty($fileExt )) { $_fileExtMatch = $true; } else { if ( $_fileExt -eq $fileExtLocal ) { $_fileExtMatch = $true; } else { $_fileExtMatch = $false; #"file extension $_fileExt does not match $fileExt " } } if (` ($_fileMode -ne $CONST_FILEMODE_DIRECTORY )` -and ( $_fileExtMatch)` ) { Get-NTFSAccess -Path $_fileFullName -ExcludeInherited } } } #getNTFSFolder() if ($fileCheck -eq $true) { getNTFSFile $file } elseif ($fileCheck -eq $false) { getNTFSFolder $folder $fileExt }
Sample
Get Permissions for Excel files
Code
powershell .\getNTFSPermissions.ps1 -folder C:\temp -fileExt .xlsx
Output
Get Permissions for Scheduled Tasks ( Local to machine)
Code
powershell .\getNTFSPermissions.ps1 -folder C:\Windows\System32\Tasks
[…] Raimund Andrée – NTFSSecurity – Usage Scenario – Day 1 Link […]