|
|
|
@ -78,13 +78,13 @@ $env:TMP = $original_tmp
|
|
|
|
Function Assert-Age($info) {
|
|
|
|
Function Assert-Age($info) {
|
|
|
|
$valid_match = $true
|
|
|
|
$valid_match = $true
|
|
|
|
|
|
|
|
|
|
|
|
if ($age -ne $null) {
|
|
|
|
if ($null -ne $age) {
|
|
|
|
$seconds_per_unit = @{'s'=1; 'm'=60; 'h'=3600; 'd'=86400; 'w'=604800}
|
|
|
|
$seconds_per_unit = @{'s'=1; 'm'=60; 'h'=3600; 'd'=86400; 'w'=604800}
|
|
|
|
$seconds_pattern = '^(-?\d+)(s|m|h|d|w)?$'
|
|
|
|
$seconds_pattern = '^(-?\d+)(s|m|h|d|w)?$'
|
|
|
|
$match = $age -match $seconds_pattern
|
|
|
|
$match = $age -match $seconds_pattern
|
|
|
|
if ($match) {
|
|
|
|
if ($match) {
|
|
|
|
[int]$specified_seconds = $matches[1]
|
|
|
|
[int]$specified_seconds = $matches[1]
|
|
|
|
if ($matches[2] -eq $null) {
|
|
|
|
if ($null -eq $matches[2]) {
|
|
|
|
$chosen_unit = 's'
|
|
|
|
$chosen_unit = 's'
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$chosen_unit = $matches[2]
|
|
|
|
$chosen_unit = $matches[2]
|
|
|
|
@ -148,7 +148,7 @@ Function Assert-Hidden($info) {
|
|
|
|
Function Assert-Pattern($info) {
|
|
|
|
Function Assert-Pattern($info) {
|
|
|
|
$valid_match = $false
|
|
|
|
$valid_match = $false
|
|
|
|
|
|
|
|
|
|
|
|
if ($patterns -ne $null) {
|
|
|
|
if ($null -ne $patterns) {
|
|
|
|
foreach ($pattern in $patterns) {
|
|
|
|
foreach ($pattern in $patterns) {
|
|
|
|
if ($use_regex -eq $true) {
|
|
|
|
if ($use_regex -eq $true) {
|
|
|
|
# Use -match for regex matching
|
|
|
|
# Use -match for regex matching
|
|
|
|
@ -172,13 +172,13 @@ Function Assert-Pattern($info) {
|
|
|
|
Function Assert-Size($info) {
|
|
|
|
Function Assert-Size($info) {
|
|
|
|
$valid_match = $true
|
|
|
|
$valid_match = $true
|
|
|
|
|
|
|
|
|
|
|
|
if ($size -ne $null) {
|
|
|
|
if ($null -ne $size) {
|
|
|
|
$bytes_per_unit = @{'b'=1; 'k'=1024; 'm'=1024*1024; 'g'=1024*1024*1024; 't'=1024*1024*1024*1024}
|
|
|
|
$bytes_per_unit = @{'b'=1; 'k'=1024; 'm'=1024*1024; 'g'=1024*1024*1024; 't'=1024*1024*1024*1024}
|
|
|
|
$size_pattern = '^(-?\d+)(b|k|m|g|t)?$'
|
|
|
|
$size_pattern = '^(-?\d+)(b|k|m|g|t)?$'
|
|
|
|
$match = $size -match $size_pattern
|
|
|
|
$match = $size -match $size_pattern
|
|
|
|
if ($match) {
|
|
|
|
if ($match) {
|
|
|
|
[int]$specified_size = $matches[1]
|
|
|
|
[int]$specified_size = $matches[1]
|
|
|
|
if ($matches[2] -eq $null) {
|
|
|
|
if ($null -eq $matches[2]) {
|
|
|
|
$chosen_byte = 'b'
|
|
|
|
$chosen_byte = 'b'
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$chosen_byte = $matches[2]
|
|
|
|
$chosen_byte = $matches[2]
|
|
|
|
@ -255,19 +255,24 @@ Function Get-FileStat($file) {
|
|
|
|
$isdir = $true
|
|
|
|
$isdir = $true
|
|
|
|
|
|
|
|
|
|
|
|
$share_info = Get-WmiObject -Class Win32_Share -Filter "Path='$($file.Fullname -replace '\\', '\\')'"
|
|
|
|
$share_info = Get-WmiObject -Class Win32_Share -Filter "Path='$($file.Fullname -replace '\\', '\\')'"
|
|
|
|
if ($share_info -ne $null) {
|
|
|
|
if ($null -ne $share_info) {
|
|
|
|
$isshared = $true
|
|
|
|
$isshared = $true
|
|
|
|
$file_stat.sharename = $share_info.Name
|
|
|
|
$file_stat.sharename = $share_info.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# only get the size of a directory if there are files (not directories) inside the folder
|
|
|
|
# only get the size of a directory if there are files (not directories) inside the folder
|
|
|
|
$dir_files_sum = Get-ChildItem $file.FullName -Recurse | Where-Object { -not $_.PSIsContainer }
|
|
|
|
# Get-ChildItem -LiteralPath does not work properly on older OS', use .NET instead
|
|
|
|
|
|
|
|
$dir_files = @()
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$dir_files = $file.EnumerateFiles("*", [System.IO.SearchOption]::AllDirectories)
|
|
|
|
|
|
|
|
} catch [System.IO.DirectoryNotFoundException] { # Broken ReparsePoint/Symlink, cannot enumerate
|
|
|
|
|
|
|
|
} catch [System.UnauthorizedAccessException] {} # No ListDirectory permissions, Get-ChildItem ignored this
|
|
|
|
|
|
|
|
|
|
|
|
if ($dir_files_sum -eq $null -or ($dir_files_sum.PSObject.Properties.name -contains 'length' -eq $false)) {
|
|
|
|
$size = 0
|
|
|
|
$file_stat.size = 0
|
|
|
|
foreach ($dir_file in $dir_files) {
|
|
|
|
} else {
|
|
|
|
$size += $dir_file.Length
|
|
|
|
$file_stat.size = ($dir_files_sum | Measure-Object -property length -sum).Sum
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_stat.size = $size
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$file_stat.size = $file.length
|
|
|
|
$file_stat.size = $file.length
|
|
|
|
$file_stat.extension = $file.Extension
|
|
|
|
$file_stat.extension = $file.Extension
|
|
|
|
@ -291,8 +296,17 @@ Function Get-FileStat($file) {
|
|
|
|
|
|
|
|
|
|
|
|
Function Get-FilesInFolder($path) {
|
|
|
|
Function Get-FilesInFolder($path) {
|
|
|
|
$items = @()
|
|
|
|
$items = @()
|
|
|
|
foreach ($item in (Get-ChildItem -Force -Path $path -ErrorAction SilentlyContinue)) {
|
|
|
|
|
|
|
|
if ($item.PSIsContainer -and $recurse) {
|
|
|
|
# Get-ChildItem -LiteralPath can bomb out on older OS', use .NET instead
|
|
|
|
|
|
|
|
$dir = New-Object -TypeName System.IO.DirectoryInfo -ArgumentList $path
|
|
|
|
|
|
|
|
$dir_files = @()
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$dir_files = $dir.EnumerateFileSystemInfos("*", [System.IO.SearchOption]::TopDirectoryOnly)
|
|
|
|
|
|
|
|
} catch [System.IO.DirectoryNotFoundException] { # Broken ReparsePoint/Symlink, cannot enumerate
|
|
|
|
|
|
|
|
} catch [System.UnauthorizedAccessException] {} # No ListDirectory permissions, Get-ChildItem ignored this
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($item in $dir_files) {
|
|
|
|
|
|
|
|
if ($item -is [System.IO.DirectoryInfo] -and $recurse) {
|
|
|
|
if (($item.Attributes -like '*ReparsePoint*' -and $follow) -or ($item.Attributes -notlike '*ReparsePoint*')) {
|
|
|
|
if (($item.Attributes -like '*ReparsePoint*' -and $follow) -or ($item.Attributes -notlike '*ReparsePoint*')) {
|
|
|
|
# File is a link and we want to follow a link OR file is not a link
|
|
|
|
# File is a link and we want to follow a link OR file is not a link
|
|
|
|
$items += $item.FullName
|
|
|
|
$items += $item.FullName
|
|
|
|
@ -311,8 +325,8 @@ Function Get-FilesInFolder($path) {
|
|
|
|
|
|
|
|
|
|
|
|
$paths_to_check = @()
|
|
|
|
$paths_to_check = @()
|
|
|
|
foreach ($path in $paths) {
|
|
|
|
foreach ($path in $paths) {
|
|
|
|
if (Test-Path $path) {
|
|
|
|
if (Test-Path -LiteralPath $path) {
|
|
|
|
if ((Get-Item -Force $path).PSIsContainer) {
|
|
|
|
if ((Get-Item -LiteralPath $path -Force).PSIsContainer) {
|
|
|
|
$paths_to_check += Get-FilesInFolder -path $path
|
|
|
|
$paths_to_check += Get-FilesInFolder -path $path
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Fail-Json $result "Argument path $path is a file not a directory"
|
|
|
|
Fail-Json $result "Argument path $path is a file not a directory"
|
|
|
|
@ -321,11 +335,11 @@ foreach ($path in $paths) {
|
|
|
|
Fail-Json $result "Argument path $path does not exist cannot get information on"
|
|
|
|
Fail-Json $result "Argument path $path does not exist cannot get information on"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$paths_to_check = $paths_to_check | Select-Object -Unique
|
|
|
|
$paths_to_check = $paths_to_check | Select-Object -Unique | Sort-Object
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($path in $paths_to_check) {
|
|
|
|
foreach ($path in $paths_to_check) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
$file = Get-Item -Force -Path $path
|
|
|
|
$file = Get-Item -LiteralPath $path -Force
|
|
|
|
$info = Get-FileStat -file $file
|
|
|
|
$info = Get-FileStat -file $file
|
|
|
|
} catch {
|
|
|
|
} catch {
|
|
|
|
Add-Warning -obj $result -message "win_find failed to check some files, these files were ignored and will not be part of the result output"
|
|
|
|
Add-Warning -obj $result -message "win_find failed to check some files, these files were ignored and will not be part of the result output"
|
|
|
|
|