-
Notifications
You must be signed in to change notification settings - Fork 21
/
Appx-Backup.ps1
144 lines (131 loc) · 4.53 KB
/
Appx-Backup.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
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string] $WSAppPath,
[Parameter(Mandatory=$True)]
[string] $WSAppOutputPath,
[Parameter(Mandatory=$True)]
[string] $WSTools
)
function Run-Process {
Param ($p, $a)
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $p
$pinfo.Arguments = $a
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$output = $p.StandardOutput.ReadToEnd()
$output += $p.StandardError.ReadToEnd()
$p.WaitForExit()
return $output
}
# find tools
$FileExists = Test-Path "$WSTools\MakeAppx.exe"
if ($FileExists -eq $False) {
Write-Output "ERROR: MakeAppx.exe not found in WSTools path."
Exit
}
$FileExists = Test-Path "$WSTools\MakeCert.exe"
if ($FileExists -eq $False) {
Write-Output "ERROR: MakeCert.exe not found in WSTools path."
Exit
}
$FileExists = Test-Path "$WSTools\Pvk2Pfx.exe"
if ($FileExists -eq $False) {
Write-Output "ERROR: Pvk2Pfx.exe not found in WSTools path."
Exit
}
$FileExists = Test-Path "$WSTools\SignTool.exe"
if ($FileExists -eq $False) {
Write-Output "ERROR: SignTool.exe not found in WSTools path."
Exit
}
$WSAppXmlFile="AppxManifest.xml"
# read manifest
Write-Output "Reading ""$WSAppPath\$WSAppXmlFile"""
$FileExists = Test-Path "$WSAppPath\$WSAppXmlFile"
if ($FileExists -eq $False) {
Write-Output "ERROR: Windows Store manifest not found."
Exit
}
[xml]$manifest = Get-Content "$WSAppPath\$WSAppXmlFile"
$WSAppName = $manifest.Package.Identity.Name
$WSAppPublisher = $manifest.Package.Identity.Publisher
Write-Output " App Name : $WSAppName"
Write-Output " Publisher: $WSAppPublisher"
# prepare
$WSAppFileName = gi $WSAppPath | select basename
$WSAppFileName = $WSAppFileName.BaseName
Write-Output "Creating ""$WSAppOutputPath\$WSAppFileName.appx""."
if (Test-Path "$WSAppOutputPath\$WSAppFileName.appx") {
Remove-Item "$WSAppOutputPath\$WSAppFileName.appx"
}
$proc = "$WSTools\MakeAppx.exe"
$args = "pack /d ""$WSAppPath"" /p ""$WSAppOutputPath\$WSAppFileName.appx"" /l"
$output = Run-Process $proc $args
if ($output -inotlike "*succeeded*") {
Write-Output " ERROR: Appx creation failed!"
Write-Output " proc = $proc"
Write-Output " args = $args"
Write-Output (" " + $output)
Exit
}
Write-Output " Done."
Write-Output "Creating self-signed certificates."
Write-Output " Click NONE in the 'Create Private Key Passsword' pop-up."
if (Test-Path "$WSAppOutputPath\$WSAppFileName.pvk") {
Remove-Item "$WSAppOutputPath\$WSAppFileName.pvk"
}
if (Test-Path "$WSAppOutputPath\$WSAppFileName.cer") {
Remove-Item "$WSAppOutputPath\$WSAppFileName.cer"
}
$proc = "$WSTools\MakeCert.exe"
$args = "-n ""$WSAppPublisher"" -r -a sha256 -len 2048 -cy end -h 0 -eku 1.3.6.1.5.5.7.3.3 -b 01/01/2000 -sv ""$WSAppOutputPath\$WSAppFileName.pvk"" ""$WSAppOutputPath\$WSAppFileName.cer"""
$output = Run-Process $proc $args
if ($output -inotlike "*succeeded*") {
Write-Output "ERROR: Certificate creation failed!"
Write-Output "proc = $proc"
Write-Output "args = $args"
Write-Output (" " + $output)
Exit
}
Write-Output " Done."
Write-Output "Converting certificate to pfx."
if (Test-Path "$WSAppOutputPath\$WSAppFileName.pfx") {
Remove-Item "$WSAppOutputPath\$WSAppFileName.pfx"
}
$proc = "$WSTools\Pvk2Pfx.exe"
$args = "-pvk ""$WSAppOutputPath\$WSAppFileName.pvk"" -spc ""$WSAppOutputPath\$WSAppFileName.cer"" -pfx ""$WSAppOutputPath\$WSAppFileName.pfx"""
$output = Run-Process $proc $args
if ($output.Length -gt 0) {
Write-Output " ERROR: Certificate conversion to pfx failed!"
Write-Output " proc = $proc"
Write-Output " args = $args"
Write-Output (" " + $output)
Exit
}
Write-Output " Done."
Write-Output "Signing the package."
$proc = "$WSTools\SignTool.exe"
$args = "sign -fd SHA256 -a -f ""$WSAppOutputPath\$WSAppFileName.pfx"" ""$WSAppOutputPath\$WSAppFileName.appx"""
$output = Run-Process $proc $args
if ($output -inotlike "*successfully signed*") {
Write-Output "ERROR: Package signing failed!"
Write-Output $output.Length
Write-Output "proc = $proc"
Write-Output "args = $args"
Write-Output (" " + $output)
Exit
}
Write-Output " Done."
Remove-Item "$WSAppOutputPath\$WSAppFileName.pvk"
Remove-Item "$WSAppOutputPath\$WSAppFileName.pfx"
Write-Output "Success!"
Write-Output " App Package: ""$WSAppOutputPath\$WSAppFileName.appx"""
Write-Output " Certificate: ""$WSAppOutputPath\$WSAppFileName.cer"""
Write-Output "Install the '.cer' file to [Local Computer\Trusted Root Certification Authorities] before you install the App Package."
Exit