-
Notifications
You must be signed in to change notification settings - Fork 4
/
Test-Image.ps1
64 lines (55 loc) · 1.85 KB
/
Test-Image.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<#
.Synopsis
Compares the first 8 bytes of a given file to a set of known image headers.
.Example
PS> dir .\1.tiff | Test-Image
True
PS> Test-Image -Path test.ps1
False
#>
function Test-Image {
[CmdletBinding()]
[OutputType([System.Boolean])]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[Alias('PSPath')]
[string] $Path
)
PROCESS {
$knownImageHeaders = @{
jpg = @( "FF", "D8" );
bmp = @( "42", "4D" );
gif = @( "47", "49", "46" );
tif = @( "49", "49", "2A" );
png = @( "89", "50", "4E", "47", "0D", "0A", "1A", "0A" );
pdf = @( "25", "50", "44", "46" );
}
$bytes = Get-Content -LiteralPath $path -Encoding Byte -ReadCount 1 -TotalCount 8 -ErrorAction Ignore
$retval = $false
foreach($key in $knownImageHeaders.Keys) {
# transform into array of the same length and format as the reference array
$byteArray = $bytes | Select-Object -First $knownImageHeaders[$key].Length | ForEach-Object { $_.ToString("X2") }
if($byteArray.Length -eq 0) {
continue
}
# compare the two arrays
$diff = Compare-Object -ReferenceObject $knownImageHeaders[$key] -DifferenceObject $byteArray
if(($diff | Measure-Object).Count -eq 0) {
$retval = $true
}
}
return $retval
}
}
function Get-ChildImage {
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param(
[parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[Alias('PSPath')]
[string] $Path
)
Get-ChildItem $Path -File | Where-Object { Test-Image $_.FullName }
}