Functions/Private/GetImageType.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
function GetImageType {

    <#
    .SYNOPSIS
    Retrieves the type of the image.
 
    .PARAMETER Path
    The path to the image file that will be inspected.
 
    .OUTPUTS
    This command emits a value from the [ImageType] enumeration
    #>

    [CmdletBinding()]
    param (
        [ValidateScript({
            if (Test-Path -Path $PSItem) { $true }
            else { throw 'File does not exist or permission denied.' }
        })]
        [string] $Path
    )

    $Path = Resolve-Path -Path $Path

    try {
        Write-Verbose -Message ('Reading image file: {0}' -f $Path)
        $Image = Get-WindowsImage -ImagePath $Path -ErrorAction Stop
        if ($Image) {
            Write-Verbose -Message 'Image file appears to be a valid WIM or VHDX file.'
        }
        Write-Verbose -Message ('Image file {0} contains {1} images' -f $Path, $Image.Count)

        ### If the file name ends with 'wim', or it has more than one image, then it's a WIM
        if ($Image.Count -gt 1 -or ($Path -match 'wim$' -and ($Image.ImageName.Length -ge 1))) {
            Write-Verbose -Message 'This image appears to be a valid Windows Image Format (WIM) file.'
            return [ImageType]::WIM
        }

        ### If the file only has a single image, and doesn't have an image name, it might be a VHDX
        if ($Image.Count -eq 1 -and $Image.ImageName -eq '') {
            Write-Verbose -Message 'This image appears to be a valid Virtual Hard Drive (VHDX) file.'
            return [ImageType]::VHDX
        }
        
        return [ImageType]::Unknown
    }
    catch {
        Write-Error -Message ('Error occurred while attempting to inspect the image file. {0}' -f $PSItem.Exception.Message)
        throw $PSItem
    }

}