-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSIX_App_Container_Creator_1.0.ps1
192 lines (139 loc) · 5.84 KB
/
MSIX_App_Container_Creator_1.0.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
param($packageName, $vhdSize, $overrideVolumeName)
#Editable Parametters
#Folders locations parameter
#Source folder of MSIX application packet.
$msixPath = "MSIX application Packet Folder"
#Destination folder where MSIX App Attach VHDX file will be created.
$vhdSrcFolder = "VHDX Destination Folder"
#Folder where is MSIXMGR tool.
$msixmgrPath = "MSIXMGR tool Folder"
#Destination folder where tool creating powershell Sripts for Attach and Detach App Attach container.
$scriptLocation = "Attach/Detach scipts destination folder"
#Optionally parameters
#Parent Folder on VHDX
$parentFolder = "MSIX"
#Attach and Detach script MSIX Junction. This affect only attach/detach MSIX App Attach scipts
$msixJunction = "C:\temp\AppAttach\"
#Static parameters
$parts = $packageName.split("_")
$appName = $parts[0] + "_" + $parts[1]
$vhdSrc = $vhdSrcFolder + "\" + $appName + ".vhdx"
$parentFolder = "\" + $parentFolder + "\"
$volumeName = "MSIX-" + $parts[0]
$packagePath = $msixPath + "\" + $packageName + ".msix"
#Generate a VHD or VHDX package for MSIX
New-VHD -SizeBytes $vhdSize -Path $vhdSrc -Dynamic -confirm:$false
$vhdObject = Mount-VHD $vhdSrc -PassThru
$disk = Initialize-Disk -PassThru -Number $vhdObject.Number
$partition = New-Partition -AssignDriveLetter -UseMaximumSize -DiskNumber $disk.Number
Format-Volume -FileSystem NTFS -confirm:$false -DriveLetter $partition.DriveLetter -Force
$Path = $partition.DriveLetter + ":" + $parentFolder
#Create a folder with Package Parent Folder
New-Item -Path $Path -ItemType Directory
#Partition and Volume Name
if (-not ($overrideVolumeName -eq $null)) {
$volumeName = "MSIX-" + $overrideVolumeName
}
else {
if ($volumeName.length -gt 32) {
Write-Host "Volume name is too long. Give new volume name. MAX length 27 letters"
$volumeName = Read-Host -Prompt 'New Volume Name'
}
}
Set-Volume -DriveLetter $partition.DriveLetter -NewFileSystemLabel $volumeName
#Unpack MSIX on VHD/VHDX
Set-Location $msixmgrPath
Write-Host ("Unpacking MSIX to VHD/VHDX Disk") -BackgroundColor Green
.\msixmgr.exe -Unpack -packagePath $packagePath -Destination $Path -applyacls
#Get VHD/VHDX GUID ID
$volumeDeviceID = ((Get-WmiObject win32_volume | Where-Object { $_.DriveLetter -eq $partition.DriveLetter + ":" }).DeviceId)
$volumeGuid = ($volumeDeviceID.split("{")).split("}")[1]
Dismount-VHD -Path $vhdSrc
#Creating passing parametters for Scripts
$vhdSrcPass = '$vhdSrc = "'
$vhdSrcPass += $vhdSrc
$vhdSrcPass += '"'
$packageNamePass = '$packageName = "'
$packageNamePass += $packageName
$packageNamePass += '"'
$parentFolderPass = '$parentFolder = "'
$parentFolderPass += $parentFolder
$parentFolderPass += '"'
$volumeGuidPass = '$volumeGuid = "'
$volumeGuidPass += $volumeGuid
$volumeGuidPass += '"'
$parentFolderPass = '$parentFolder = "'
$parentFolderPass += $parentFolder
$parentFolderPass += '"'
$msixJunctionPass = '$msixJunction = "'
$msixJunctionPass += $msixJunction
$msixJunctionPass += '"'
#Creating Attach Script
#Attach Passing parametter on script
$attachAppScript = "
$vhdSrcPass
$packageNamePass
$parentFolderPass
$volumeGuidPass
$msixJunctionPass
"
$attachAppScript += @'
#Mountvhd
try
{
Mount-Diskimage -ImagePath $vhdSrc -NoDriveLetter -Access ReadOnly
Write-Host ("Mounting of " + $vhdSrc + " was completed!") -BackgroundColor Green
}
catch
{
Write-Host ("Mounting of " + $vhdSrc + " has failed!") -BackgroundColor Red
}
#Makelink
$msixDest = "\\?\Volume{" + $volumeGuid + "}\"
if (!(Test-Path $msixJunction))
{
md $msixJunction
}
$msixJunction = $msixJunction + $packageName
cmd.exe /c mklink /j $msixJunction $msixDest
#region stage
[Windows.Management.Deployment.PackageManager,Windows.Management.Deployment,ContentType=WindowsRuntime] | Out-Null
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where { $_.ToString() -eq 'System.Threading.Tasks.Task`1[TResult] AsTask[TResult,TProgress](Windows.Foundation.IAsyncOperationWithProgress`2[TResult,TProgress])'})[0]
$asTaskAsyncOperation = $asTask.MakeGenericMethod([Windows.Management.Deployment.DeploymentResult], [Windows.Management.Deployment.DeploymentProgress])
$packageManager = [Windows.Management.Deployment.PackageManager]::new()
$path = $msixJunction + $parentFolder + $packageName # needed if we do the pbisigned.vhd
$path = ([System.Uri]$path).AbsoluteUri
$asyncOperation = $packageManager.StagePackageAsync($path, $null, "StageInPlace")
$task = $asTaskAsyncOperation.Invoke($null, @($asyncOperation))
$task
#endregion
#Registers the application of the mounted container in the user context
$path = "C:\Program Files\WindowsApps\" + $packageName + "\AppxManifest.xml"
Add-AppxPackage -Path $path -DisableDevelopmentMode -Register
'@
#Creating Attach Script END
#Export Attach Script File
$attachExportParam = $scriptLocation + "\" + "Attach-" + $appName + ".ps1"
$attachAppScript | Out-File $attachExportParam
#Creating Detach Script
#Attach Passing parametter on script
$detachAppScript = "
$vhdSrcPass
$packageNamePass
$msixJunctionPass
"
$detachAppScript += @'
#Deregisters the application of the mounted container in the user context
Remove-AppxPackage -PreserveRoamableApplicationData $packageName
#Derregister
Remove-AppxPackage -AllUsers -Package $packageName
cd $msixJunction
rmdir $packageName -Recurse -Force -Confirm:$false
#Dismount VHD
disMount-Diskimage -ImagePath $vhdSrc
'@
#Creating Detach Script END
#Export Detach Script File
$detachExportParam = $scriptLocation + "\" + "Detach-" + $appName + ".ps1"
$detachAppScript | Out-File $detachExportParam