-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-MsiExec.ps1
222 lines (176 loc) · 6.15 KB
/
Invoke-MsiExec.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
function Invoke-MsiExec {
<#
.SYNOPSIS
Invokes msiexec.exe to install, uninstall, or perform administrative installations of MSI files with various options.
.DESCRIPTION
This function allows you to invoke msiexec.exe to install, uninstall, or perform administrative installations of MSI files.
.PARAMETER File
The path to the MSI file or GUID of the installed program to be processed.
.PARAMETER Action
Specifies the action to perform: Install, Uninstall, or Administrative.
.PARAMETER Quiet
Runs msiexec.exe with quiet mode. Use additional switches like NoUI, NoUIPlus, Basic, BasicPlus, Reduced, or Full to control the level of UI.
.PARAMETER NoUI
Runs msiexec.exe with no user interface (UI).
.PARAMETER NoUIPlus
Runs msiexec.exe with no UI, except for the final dialog box at the end.
.PARAMETER Basic
Runs msiexec.exe with a basic UI.
.PARAMETER BasicPlus
Runs msiexec.exe with a basic UI, including the final dialog box at the end.
.PARAMETER Reduced
Runs msiexec.exe with reduced UI.
.PARAMETER Full
Runs msiexec.exe with a full UI.
.PARAMETER Restart
Specifies the restart behavior: NoRestart, Prompt, or Force.
.PARAMETER LogVerbose
Enables verbose logging with the log file saved at the specified path.
.EXAMPLE
Invoke-MsiExec -File "C:\temp\installer.msi" -Action Install -Quiet NoUI -LogVerbose "C:\Logs\installer.log"
Installs the MSI file with no UI and logs the installation details to "C:\Logs\installer.log".
.EXAMPLE
Invoke-MsiExec -File "{3C28BFD4-90C7-3138-87EF-418DC16E9598}" -Action Uninstall -Quiet NoUIPlus -LogVerbose "C:\Logs\uninstaller.log"
Uninstalls the program with the specified GUID with no UI, except for the final dialog box at the end, and logs the details to "C:\Logs\uninstaller.log".
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ParameterSetName='File')]
[string]$File,
[Parameter(Mandatory=$true, ParameterSetName='GUID')]
[string]$GUID,
[Parameter(Mandatory=$true)]
[ValidateSet('Install', 'Uninstall', 'Administrative')]
[string]$Action,
[Switch]$Quiet,
[Switch]$NoUI,
[Switch]$NoUIPlus,
[Switch]$Basic,
[Switch]$BasicPlus,
[Switch]$Reduced,
[Switch]$Full,
[ValidateSet('NoRestart', 'Prompt', 'Force')]
[string]$Restart = 'NoRestart',
[Parameter(Mandatory=$true)]
[string]$LogVerbose
)
function Get-MsiExecPath {
$msiExecPath = Join-Path $env:WINDIR 'System32\msiexec.exe'
if (Test-Path $msiExecPath) {
return $msiExecPath
}
$env:PATH.Split(';') | ForEach-Object {
$path = Join-Path $_ 'msiexec.exe'
if (Test-Path $path) {
return $path
}
}
Write-Warning "msiexec.exe not found in PATH or default location."
exit
}
function Validate-FilePath {
param (
[string]$Path
)
if (-Not $Path) {
$Path = Read-Host "Enter the path to the MSI file or GUID"
}
if (-Not $Path) {
Write-Warning "No path or GUID provided. Exiting."
exit
}
if ($Path -match '^{[A-F0-9-]+}$') {
# Assume it's a GUID
$GUID = $Path
}
else {
# Assume it's a file path
if ($Path -notlike '"*"' -or $Path -notlike "'*'") {
$Path = '"' + $Path + '"'
}
$File = $Path
}
}
function Validate-MsiFile {
param (
[string]$File
)
if (-Not $File) {
$File = Read-Host "Enter the path to the MSI file"
}
if (-Not $File) {
Write-Warning "No path to MSI file provided. Exiting."
exit
}
if (-Not (Test-Path $File -PathType Leaf)) {
Write-Warning "MSI file not found at $File. Exiting."
exit
}
}
function Get-LogFileName {
param (
[string]$File
)
$timestamp = Get-Date -Format 'yyyyMMdd_HHmmss'
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($File)
$logFileName = "${fileName}-${timestamp}.log"
return $logFileName
}
$msiExecPath = Get-MsiExecPath
if ($Action -eq 'Install') {
$arguments = "/i $File"
}
elseif ($Action -eq 'Uninstall') {
$arguments = "/x $File"
}
elseif ($Action -eq 'Administrative') {
$arguments = "/a $File"
}
if ($Quiet) {
if ($NoUI) {
$arguments += ' /qn'
}
elseif ($NoUIPlus) {
$arguments += ' /qn+'
}
elseif ($Basic) {
$arguments += ' /qb'
}
elseif ($BasicPlus) {
$arguments += ' /qb+'
}
elseif ($Reduced) {
$arguments += ' /qr'
}
elseif ($Full) {
$arguments += ' /qf'
}
else {
$arguments += ' /q'
}
}
if ($Restart -ne 'NoRestart') {
$arguments += " /forcerestart"
}
if ($LogVerbose) {
$logPath = [System.IO.Path]::GetDirectoryName($LogVerbose)
if (-Not $logPath) {
$logPath = Read-Host "Enter the path to store the log file"
if (-Not $logPath) {
Write-Warning "No path for the log file provided. Exiting."
exit
}
if ($logPath -notlike '"*"' -or $logPath -notlike "'*'") {
$logPath = '"' + $logPath + '"'
}
$LogVerbose = Join-Path $logPath (Get-LogFileName -File $File)
}
$arguments += " /l*v `"$LogVerbose`""
}
$msiExecCommand = "$msiExecPath $arguments"
Write-Host "Running: $msiExecCommand"
Invoke-Expression $msiExecCommand
}
# Usage examples:
# Invoke-MsiExec -File "C:\temp\installer.msi" -Action Install -Quiet NoUI -LogVerbose "C:\Logs"
# Invoke-MsiExec -File "{3C28BFD4-90C7-3138-87EF-418DC16E9598}" -Action Uninstall -Quiet NoUIPlus -LogVerbose "C:\Logs"