Public/DISM/Mount-MyWindowsImage.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<#
.SYNOPSIS
Mounts a WIM file
 
.DESCRIPTION
Mounts a WIM file automatically selecting the Path and the Index
 
.PARAMETER ImagePath
Specifies the full path to the Windows Image
 
.PARAMETER Index
Index of the Windows Image
 
.PARAMETER ReadOnly
Mount the Windows Image as Read Only
 
.PARAMETER Explorer
Opens Windows Explorer to the Mount Directory
 
.LINK
https://osd.osdeploy.com/module/functions/mywindowsimage
 
.NOTES
#>

function Mount-MyWindowsImage {
    [CmdletBinding()]
    param (
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipelineByPropertyName
        )]
        [string[]]$ImagePath,

        [Parameter(
            ValueFromPipelineByPropertyName
        )]
        [UInt32]$Index = 1,

        [Parameter(
            ValueFromPipelineByPropertyName
        )]
        [switch]$ReadOnly,

        [Parameter(
            ValueFromPipelineByPropertyName
        )]
        [switch]$Explorer
    )

    begin {
        #=======================================================================
        # Require Admin Rights
        #=======================================================================
        if ((Get-OSDGather -Property IsAdmin) -eq $false) {
            Write-Warning "$($MyInvocation.MyCommand) requires Admin Rights ELEVATED"
            Break
        }
        #=======================================================================
        # Get Registry Information
        #=======================================================================
        $GetRegCurrentVersion = Get-RegCurrentVersion
        #=======================================================================
        # Require OSMajorVersion 10
        #=======================================================================
        if ($GetRegCurrentVersion.CurrentMajorVersionNumber -ne 10) {
            Write-Warning "$($MyInvocation.MyCommand) requires OS MajorVersion 10"
            Break
        }
        #=======================================================================
    }
    process {
        foreach ($Input in $ImagePath) {
            Write-Verbose "$Input"
            #=======================================================================
            # Get-Item
            #=======================================================================
            if (Get-Item $Input -ErrorAction SilentlyContinue) {
                $GetItemInput = Get-Item -Path $Input
            } else {
                Write-Warning "Unable to locate WindowsImage at $Input"
                Break
            }
            #=======================================================================
            # Directory
            #=======================================================================
            if ($GetItemInput.PSIsContainer) {
                Write-Verbose "Directory was not expected"

                if (Test-WindowsImageMountPath -Path $GetItemInput.FullName) {
                    Write-Verbose "Windows Image is already mounted in this Directory"
                    Write-Verbose "Returning Mount-WindowsImage Object"
                    Get-WindowsImage -Mounted | Where-Object {($_.Path -eq $GetItemInput.FullName) -and ($_.ImageIndex -eq $Index)}
                    Continue
                } else {
                    Write-Warning "There isn't really anything that I can do with this directory. Goodbye!"
                    Continue
                }
            }
            #=======================================================================
            # Read Only
            #=======================================================================
            if ($GetItemInput.IsReadOnly) {
                Write-Warning "Cannot Mount this Read Only Image. Goodbye!"
                Continue
            }
            #=======================================================================
            # Already Mounted
            #=======================================================================
            if (Test-WindowsImageMounted -ImagePath $GetItemInput.FullName -Index $Index) {
                Write-Verbose "Windows Image is already mounted"
                Write-Verbose "Returning Mount-WindowsImage Object"
                Get-WindowsImage -Mounted | Where-Object {($_.ImagePath -eq $GetItemInput.FullName) -and ($_.ImageIndex -eq $Index)}
                Continue
            }
            #=======================================================================
            # Not a Windows Image
            #=======================================================================
            if (-Not (Test-WindowsImage -ImagePath $GetItemInput.FullName)) {
                Write-Warning "Does not appear to be a Windows Image. Goodbye!"
                Continue
            }
            #=======================================================================
            # Set Mount Path
            #=======================================================================
            $MyWindowsImageMountPath = $env:Temp + '\Mount' + (Get-Random)
            if (-NOT (Test-Path $MyWindowsImageMountPath)) {
                New-Item $MyWindowsImageMountPath -ItemType Directory -Force -ErrorAction Stop | Out-Null
            }
            $Path = (Get-Item $MyWindowsImageMountPath).FullName
            #=======================================================================
            # Mount-WindowsImage
            #=======================================================================
            $Params = @{
                Path        = $Path
                ImagePath   = $GetItemInput.FullName
                Index       = $Index
                ReadOnly    = $ReadOnly
            }
            Mount-WindowsImage @Params | Out-Null
            #=======================================================================
            # Explorer
            #=======================================================================
            if ($Explorer.IsPresent) {explorer $Path}
            #=======================================================================
            # Return for PassThru
            #=======================================================================
            Get-WindowsImage -Mounted | Where-Object {$_.Path -eq $Path}
        }
    }
    end {}
}