forked from alexinslc/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Posh-MissyWix.ps1
266 lines (237 loc) · 12.1 KB
/
Posh-MissyWix.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# This script / function will help you build an msi package using the WiX Toolset.
# It contains functions for the following:
# 1. Create folder structure of source code for XML file using heat.exe.
# 2. Create basic XML template file for building the msi.
# 3. Programatically Run Light and Candle and output the msi.
# Function "wrapper" for heat.
function Build-HeatXML() {
param (
[Parameter(mandatory=$true)][string]$HarvestInput, # This should be the actual Harvest Type object, aka your source. ( Directory | File | Project | Website | Perf | Reg )
[Parameter(mandatory=$true)][ValidateSet("dir","file","project","website","perf","reg")][string]$HarvestType, # This is the type of harvest you will perform.
[Parameter(mandatory=$true)][string]$HeatOpts, # This should be a string of your heat options. Ex: "-cg -template fragment -sfrag -gg"
[Parameter(mandatory=$false)][string]$OutFileName # You can set this to whatever file name you would like. It will append .wsx to the end of the file name.
)
try {
$CurrentDir = pwd
# Run the heat command on the directory with the command arguments.
switch ($HarvestType) {
"dir" {
# Harvest a Directory
if ($OutFileName) { heat dir $HarvestInput $HeatOpts -out $CurrentDir$OutFileName.wxs }
else { heat dir $HarvestInput $HeatOpts -out directory.wxs }
}
"file" {
# Harvest a File
if ($OutFileName) { heat file $HarvestInput $HeatOpts -out $OutFileName.wxs }
else { heat file $HarvestInput $HeatOpts -out file.wxs }
}
"project" {
# Harvest a Visual Studio Project
if ($OutFileName) { heat project $HarvestInput $HeatOpts -out $OutFileName.wxs }
else { heat project $HarvestInput $HeatOpts -out project.wxs }
}
"website" {
# Harvest a Website
if ($OutFileName) { heat website $HarvestInput $HeatOpts -out $OutFileName.wxs }
else { heat website $HarvestInput $HeatOpts -out website.wxs }
}
"perf" {
# Harvest Performance Counters
if ($OutFileName) { heat perf $HarvestInput $HeatOpts -out $OutFileName.wxs }
else { heat perf $HarvestInput $HeatOpts -out perf.wxs }
}
"reg" {
# Harvest a Registry File
if ($OutFileName) { heat reg registry.reg $HeatOpts -out $OutFileName.wxs }
else { heat reg registry.reg $HeatOpts -out reg.wxs }
}
}
}
catch {
Write-Warning $_
}
}
# Function for building the WiX XML file.
function Build-TemplateXML() {
param (
[Parameter(mandatory=$true)][string]$Path,
[Parameter(mandatory=$true)][string]$AppName,
[Parameter(mandatory=$true)][string]$AppVersion,
[Parameter(mandatory=$true)][string]$AppManufacturer,
[Parameter(mandatory=$false)][string]$InstallScope = 'perMachine',
[Parameter(mandatory=$true)][string]$HeatFile
)
try {
# Get the information from the heat file.
$SourceFiles = Get-Content($HeatFile)
# Create a new XMLTextWriter object to start building the XML
$XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)
# choose a pretty formatting:
$XmlWriter.Formatting = 'Indented'
# write the header
$XmlWriter.WriteStartDocument()
# $XmlWriter.WriteAttributeString('encoding', 'UTF-8')
# Create "Wix" Element and its xmlns
$XmlWriter.WriteStartElement('Wix')
$XmlWriter.WriteAttributeString('xmlns', 'http://schemas.microsoft.com/wix/2006.wi')
# Create "Product" Element and Setup Attributes
$UpgradeCode = [System.GUID]::NewGuid().ToString() # Generate GUID for $UpgradeCode
$XmlWriter.WriteStartElement('Product')
$XmlWriter.WriteAttributeString('Id', '*')
$XmlWriter.WriteAttributeString('Name', $AppName)
$XmlWriter.WriteAttributeString('Language', '1033')
$XmlWriter.WriteAttributeString('Version', $AppVersion)
$XmlWriter.WriteAttributeString('Manufacturer', $AppManufacturer)
$XmlWriter.WriteAttributeString('UpgradeCode', $UpgradeCode)
# Create "Package" Element and Setup Attributes.
$XmlWriter.WriteStartElement('Package')
$XmlWriter.WriteAttributeString('InstallerVersion', '200')
$XmlWriter.WriteAttributeString('Compressed', 'yes')
$XmlWriter.WriteAttributeString('InstallScope', $InstallScope)
$XmlWriter.WriteEndElement() # Close Package
# Create "MajorUpgrade" Element and Set Attribute.
$XmlWriter.WriteStartElement('MajorUpgrade')
$XmlWriter.WriteAttributeString('DowngradeErrorMessage', ('A newer version of ' + $AppName + ' is already installed.'))
$XmlWriter.WriteEndElement() # Close MajorUpgrade
# Create "MediaTemplate" Element
$XmlWriter.WriteStartElement('MediaTemplate')
$XmlWriter.WriteEndElement() # Close MediaTemplate
# Create "Feature" Element and Setup Attributes
$XmlWriter.WriteStartElement('Feature')
$XmlWriter.WriteAttributeString('Id', 'ProductFeature')
$XmlWriter.WriteAttributeString('Title', $AppName)
$XmlWriter.WriteAttributeString('Level', '1')
# Create "ComponentGroupRef" Element and Setup Attributes
$XmlWriter.WriteStartElement('ComponentGroupRef')
$XmlWriter.WriteAttributeString('Id', 'ProductComponents')
$XmlWriter.WriteEndElement() # Close ComponentRefGroup
$XmlWriter.WriteEndElement() # Close Feature
$XmlWriter.WriteEndElement() # Close Product
# Create "Fragment" Element for "Directory" and Setup Attributes.
$XmlWriter.WriteStartElement('Fragment')
$XmlWriter.WriteStartElement('Directory')
$XmlWriter.WriteAttributeString('Id', 'TARGETDIR')
$XmlWriter.WriteAttributeString('Name', 'SourceDir')
$XmlWriter.WriteStartElement('Directory')
$XmlWriter.WriteAttributeString('Id', 'ProgramFilesFolder')
$XmlWriter.WriteStartElement('Directory')
$XmlWriter.WriteAttributeString('Id', 'INSTALLFOLDER')
$XmlWriter.WriteAttributeString('Name', $AppName)
$XmlWriter.WriteEndElement() # Close INSTALLFOLDER Directory
$XmlWriter.WriteEndElement() # Close ProgramFilesFolder Directory
$XmlWriter.WriteEndElement() # Close TARGETDIR Directory
$XmlWriter.WriteEndElement() # Close Fragment
# Create "Fragment" Element for "ComponentGroup" with Components / Files and Setup Attributes.
$XmlWriter.WriteStartElement('Fragment')
$XmlWriter.WriteStartElement('ComponentGroup')
$XmlWriter.WriteAttributeString('Id', 'ProductComponents')
$XmlWriter.WriteAttributeString('Directory', 'INSTALLFOLDER')
$XmlWriter.WriteStartElement('Component')
$XmlWriter.WriteAttributeString('Id', 'ProductComponent')
$XmlWriter.WriteComment("This is where your file list from heat will go.")
# This is where your files will go!
$XmlWriter.WriteEndElement() # Close Component
$XmlWriter.WriteEndElement() # Close ComponentGroup
$XmlWriter.WriteEndElement() # Close Fragment
$XmlWriter.WriteEndElement() # Close Wix
# finalize the document:
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
#notepad $Path # Uncomment this if you want to automatically open the newly created xml in notepad
}
catch {
Write-Warning $_
}
}
# Function to run both Candle.exe and Light.exe
function Build-MSI() {
param (
[Parameter(mandatory=$true)][string]$FileName # This should be the file name of your .wxs file.
)
try {
# First Run Candle.exe (This is the WiX compiler.)
candle $FileName.wsx
# Then Run Light.exe (This is the WiX Linker which generates the final installer.)
light $FileName.wixobj
}
catch {
Write-Warning $_
}
}
# Function to put it all together!
function Posh-MSI() {
try {
# Verify WiX ToolSet is installed!
$WixReg = "HKCU:\SOFTWARE\WiX"
$WixToolSetInstaller = "wix39.exe"
if (!(Get-Item $WixReg -ErrorAction SilentlyContinue)) {
Write-Warning "WiX ToolSet is not installed, attempting download."
# Install WiX!
$Source = "https://wix.codeplex.com/downloads/get/925661"
$Destination = Get-Location
$Destination = ($Destination.Path + "\" + $WixToolSetInstaller)
Invoke-WebRequest $Source -OutFile $Destination
# Sadly (for now) you'll have to actually click Install
# TODO: Figure out how to automagically install this!
Write-Host -ForegroundColor Green "=============================================================="
Write-Host -ForegroundColor Green "Sadly, at this time, you'll need to actually click install"
Write-Host -ForegroundColor Green "within the Installer program. Please click INSTALL!"
Write-Host -ForegroundColor Green "Opening the WiX ToolSet installer..."
Write-Host -ForegroundColor Green "=============================================================="
# Give user time to read the message.
Start-Sleep 7
# Open the WiX ToolSet Installer.
.\wix39.exe
# Allow user to wait until install is complete.
Read-Host "Please press the <ENTER KEY> once the installation is complete" > $null
}
else {
Write-Host "WiX ToolSet seems to be installed."
}
# Add the WiX tools to the System PATH env.
$Current = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\').Path
$New = ($Current + ';C:\Program Files (x86)\WiX Toolset v3.9\bin')
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\' -Name Path -Value $New
Write-Host "WiX ToolSet added to System PATH env."
# Start le Questions
Write-Host -ForegroundColor Green "====================="
Write-Host -ForegroundColor Green "PowerShell Missy WiX"
Write-Host -ForegroundColor Green "====================="
Write-Host " "
# Ask for information regarding the heat generated file.
Write-Host "We need to create a heat.exe generated file to add to the Template XML file..."
$HarvestType = Read-Host "What type of harvest will you perform? Typically, people choose dir. ( dir | file | project | website | perf | reg )"
Write-Host " "
$HarvestInput = Read-Host "Please enter the full path of your source file(s). Something like C:\projects\myapp"
Write-Host " "
$HeatOpts = Read-Host "What options do you need your heat command to use? (Ex: -gg -cg ComponentGroup1 -template fragement)"
Write-Host " "
$OutFileName = Read-Host "Please enter the file name you would like to use for your heat-generated .wsx file. If nothing is specified, it will default to <HarvestType.wsx>"
Write-Host " "
Write-Host ("Executing heat.exe " + $HarvestType + " " + $HarvestInput + " " + $HeatOpts + " " + "-out " + $OutFileName + ".wxs!")
# Execute the command!
Build-HeatXML -HarvestType $HarvestType -HarvestInput $HarvestInput -HeatOpts $HeatOpts -OutFileName $OutFileName
# Ask for information for the basic template xml file.
$HeatFile = Read-Host "Please enter the full path of your generated Heat File..."
Write-Host " "
$AppName = Read-Host "What is the name of your application"?
Write-Host " "
$Path = Read-Host "Where do you want to save the template XML?"
Write-Host " "
$AppVersion = Read-Host "What is the version of your application?"
Write-Host " "
$AppManufacturer = Read-Host "Who is the application manufacturer?"
# Ask for information to build the msi.
Write-Host " "
$FileName = Read-Host "What is the name of your .wsx file? (Do NOT include file extension!)"
# Build the msi using candle and light.
Write-Host ("Building the " + $FileName + ".msi.")
Build-MSI -FileName $FileName
$CurrentDir = pwd
Write-Host ("You should find your final " + $FileName + ".msi in the " + $CurrentDir + " directory.")
}
catch {
Write-Warning $_
}
}