Skip to content

Commit 5e63bb1

Browse files
Logging enabled + error handling
Added logging module, Added error handling Added rename MSIX download and rename to original file name.
1 parent cf28d46 commit 5e63bb1

1 file changed

Lines changed: 130 additions & 36 deletions

File tree

Lines changed: 130 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#cls
22
#Needs:
33
#try/catch in main functions
4-
#change write-host to proper logging
5-
#Test Param Block
4+
#complete #change update-log -Data to proper logging
5+
#complete #Test Param Block
66
#test with store disabled from policy
77
#test on Windows 10
88
#test on LTSC greater than 1809
@@ -17,125 +17,218 @@ Param(
1717
[int]$DisableAutoUpdate = 0,
1818
[parameter(mandatory = $false, HelpMessage = "Value to set auto update reg key")]
1919
[ValidateSet($true,$false)]
20-
[string]$UninstallMSRDC = $true
20+
[string]$UninstallMSRDC = $true,
21+
[parameter(mandatory = $false, HelpMessage = "Log path and file name")]
22+
[string]$logpath = "$env:windir\temp\MultiTool.log"
2123
)
2224

23-
24-
#$source = "Store" #Store,WinGet,MSIX,All
25-
#$UninstallMSRDC = $true #$true,$false
26-
2725
#$DisableAutoUpdate = 0
2826
#0: Enables updates (default value)
2927
#1: Disable updates from all locations
3028
#2: Disable updates from the Microsoft Store
3129
#3: Disable updates from the CDN location
3230

31+
#logging function
32+
function update-log {
33+
Param(
34+
[Parameter(
35+
Mandatory = $true,
36+
ValueFromPipeline = $true,
37+
ValueFromPipelineByPropertyName = $true,
38+
Position = 0
39+
)]
40+
[string]$Data,
41+
[validateset('Information', 'Warning', 'Error', 'Comment')]
42+
[string]$Class = "Information",
43+
[validateset('Console', 'File', 'Both')]
44+
[string]$Output
45+
)
46+
47+
$date = get-date -UFormat "%m/%d/%y %r"
48+
$String = $Class + " " + $date + " " + $data
49+
if ($Output -eq "Console") { Write-Output $string | out-host }
50+
if ($Output -eq "file") { Write-Output $String | out-file -FilePath $logpath -Append }
51+
if ($Output -eq "Both") {
52+
Write-Output $string | out-host
53+
Write-Output $String | out-file -FilePath $logpath -Append
54+
}
55+
}
56+
3357
#function to uninstall MSRDC by pulling MSIEXEC.EXE GUID from the registy - primary method
3458
function uninstall-MSRDCreg{
3559

3660
$MSRCDreg = Get-ItemProperty hklm:\software\microsoft\windows\currentversion\uninstall\* | Where-Object {$_.Displayname -like "*Remote Desktop*"} | Select-Object DisplayName,DisplayVersion,UninstallString,QuietUninstallString
3761
if ($MSRCDreg.DisplayName -eq "Remote Desktop"){
38-
write-host "Remote Desktop Installation Found"
39-
#write-host "Version " $MSRCDreg.DisplayVersion
62+
update-log -Data "Remote Desktop Installation Found" -Class Information -Output Both
4063
$uninstall = $MSRCDreg.uninstallstring -replace "MsiExec.exe /X",""
41-
write-host "Uninstalling Remote Desktop"
42-
Start-Process -FilePath "msiexec.exe" -ArgumentList "/x $($uninstall) /q /norestart"
64+
update-log -Data "Uninstalling Remote Desktop" -Class Information -Output Both
65+
66+
try{
67+
Start-Process -FilePath "msiexec.exe" -ArgumentList "/x $($uninstall) /q /norestart" -ErrorAction Stop
68+
}
69+
catch
70+
{
71+
update-log -Data "Something went wrong uninstalling Remote Desktop" -Class Error -Output Both
72+
Update-Log -data $_.Exception.Message -Class Error -Output Both
73+
}
74+
4375
}
4476
else
4577
{
46-
write-host "Remote Desktop not detected via registry. Trying as package"
78+
update-log -Data "Remote Desktop not detected via registry. Trying as package" -Class Information -Output Both
4779
uninstall-MSRDC
4880
}
4981
}
5082

5183
#Function to uninstall MSRDC via uninstall-package as a secondary method
5284
function uninstall-MSRDC{
5385
try{
54-
write-host "Looking to see if Remote Desktop is an installed package"
55-
$MSRDC = Get-Package -Name "Remote Desktop" -ErrorAction Stop
86+
update-log -Data "Looking to see if Remote Desktop is an installed package" -Class Information -Output Both
87+
try{
88+
$MSRDC = Get-Package -Name "Remote Desktop" -ErrorAction Stop
89+
}
90+
catch
91+
{
92+
Update-Log -data $_.Exception.Message -Class Error -Output Both
93+
}
5694

5795
if ($MSRDC.name -eq "Remote Desktop"){
58-
write-host "Remote Desktop Install Found"
59-
#write-host "Version: " $MSRDC.Version
60-
write-host "Uninstalling Remote Desktop"
61-
Uninstall-Package -Name "Remote Desktop" -force | Out-Null
62-
write-host "Remote Desktop uninstalled"
96+
update-log -Data "Remote Desktop Install Found" -Class Information -Output Both
97+
#update-log -Data "Version: " $MSRDC.Version
98+
update-log -Data "Uninstalling Remote Desktop" -Class Information -Output Both
99+
try{
100+
Uninstall-Package -Name "Remote Desktop" -force -ErrorAction Stop| Out-Null
101+
}
102+
catch
103+
{
104+
Update-Log -data $_.Exception.Message -Class Error -Output Both
105+
}
106+
107+
update-log -Data "Remote Desktop uninstalled" -Class Information -Output Both
63108
}
64109
}
65110
catch
66111
{
67-
Write-Host "Remote Desktop not found as package."
112+
update-log -Data "Remote Desktop not found as package." -Class Information -Output Both
68113
}
69114
}
70115

71116
#function to install Windows App from MS Store - write install process log to $env:windir\temp\WindowsAppStoreInstall.log
72117
function install-windowsappstore{
73-
invoke-command -ScriptBlock { winget install 9N1F85V9T8BN --accept-package-agreements --accept-source-agreements} | Out-File -FilePath c:\windows\temp\WindowsAppStoreInstall.log -Append #MS Store Install
118+
update-log -Data "Writing install process log to $env:windir\temp\WindowsAppStoreInstall.log" -Class Information -Output Both
119+
try{
120+
invoke-command -ScriptBlock { winget install 9N1F85V9T8BN --accept-package-agreements --accept-source-agreements} | Out-File -FilePath $env:windir\temp\WindowsAppStoreInstall.log -Append #MS Store Install
121+
}
122+
catch
123+
{
124+
Update-Log -data $_.Exception.Message -Class Error -Output Both
125+
Exit 1
126+
}
127+
74128
}
75129

76130
#Function to install Windows App from Winget CDN - write install process log to $env:windir\temp\WindowsAppWinGetInstall.log
77131
function install-windowsappwinget{
78-
invoke-command -ScriptBlock {winget install Microsoft.WindowsApp --accept-package-agreements --accept-source-agreements} | Out-File -FilePath c:\windows\temp\WindowsAppWinGetInstall.log -Append #Winget Install
132+
update-log -Data "Writing install process log to $env:windir\temp\WindowsAppWinGetInstall.log" -Class Information -Output Both
133+
try{
134+
invoke-command -ScriptBlock {winget install Microsoft.WindowsApp --accept-package-agreements --accept-source-agreements} | Out-File -FilePath $env:windir\temp\WindowsAppWinGetInstall.log -Append #Winget Install
135+
}
136+
catch
137+
{
138+
Update-Log -data $_.Exception.Message -Class Error -Output Both
139+
Exit 1
140+
}
141+
79142
}
80143

81144
#Function to install Windows App from MSIX direct download
82145
function install-windowsappMSIX{
83-
Invoke-WebRequest -uri "https://go.microsoft.com/fwlink/?linkid=2262633" -outfile "c:\windows\temp\WindowsApp.MSIX" -UseBasicParsing -PassThru #windowsapp download
84-
Add-AppxPackage -Path C:\windows\temp\WindowsApp.MSIX
146+
147+
try{
148+
if ((test-path -Path $env:windir\Temp\WindowsApp.msix) -eq $true){Remove-Item -Path $env:windir\Temp\WindowsApp.msix -Force -ErrorAction Stop}
149+
150+
$Payload = Invoke-WebRequest -uri "https://go.microsoft.com/fwlink/?linkid=2262633" -UseBasicParsing -OutFile $env:windir\Temp\WindowsApp.msix -PassThru -ErrorAction Stop
151+
$filename = ($Payload.BaseResponse.ResponseUri.AbsolutePath -replace ".*/")
152+
153+
if ((test-path -Path $env:windir\Temp\$filename) -eq $true){Remove-Item -Path $env:windir\Temp\$filename -Force -ErrorAction Stop}
154+
155+
Rename-Item -Path $env:windir\Temp\WindowsApp.msix -NewName $filename -Force -ErrorAction Stop
156+
update-log -Data "Downloaded $filename to $env:windir\temp" -Class Information -Output Both
157+
}
158+
catch{
159+
Update-Log -data $_.Exception.Message -Class Error -Output Both
160+
}
161+
162+
try{
163+
Add-AppxPackage -Path $env:windir\temp\$filename -ErrorAction Stop
164+
}
165+
catch{
166+
Update-Log -data $_.Exception.Message -Class Error -Output Both
167+
}
168+
169+
85170
}
86171

87172
#Function to check if Windows App is installed
88173
function invoke-WAInstallCheck{
89174
if ((($testWA = get-appxpackage -name MicrosoftCorporationII.Windows365).name) -eq "MicrosoftCorporationII.Windows365" ){
90-
Write-Host "Windows App Installation found."
175+
update-log -Data "Windows App Installation found." -Class Information -Output Both
91176
Return 0
92177
}
93178
else
94179
{
95-
write-host "Windows App installation not found."
180+
update-log -Data "Windows App installation not found." -Class Information -Output Both
96181
Return 1
97182
}
98183
}
99184

100185
#function to set the registry key to control auto updates
101186
function invoke-disableautoupdate($num){
102-
write-host "Setting disableautoupdate reg key"
187+
update-log -Data "Setting disableautoupdate reg key" -Class Information -Output Both
103188
$path = "HKLM:\SOFTWARE\Microsoft\WindowsApp"
104189
If (!(Test-Path $path)) {
105190
New-Item -Path $path -Force
106191
}
107-
New-ItemProperty -Path $path -Name DisableAutomaticUpdates -PropertyType DWORD -Value $num -Force
192+
try{
193+
New-ItemProperty -Path $path -Name DisableAutomaticUpdates -PropertyType DWORD -Value $num -Force -ErrorAction Stop| Out-Null
194+
}
195+
catch{
196+
Update-Log -data $_.Exception.Message -Class Error -Output Both
197+
}
198+
199+
108200
}
109201

110202
#check if Windows App is installed. If so, skip installation. Else, install
111203
if ((invoke-WAInstallCheck) -eq 0){
112-
write-host "Skipping Windows App Installation"
204+
update-log -Data "Skipping Windows App Installation" -Class Information -Output Both
113205
}
114206
else
115207
{
116208
if ($source -eq "Store"){
117-
write-host "Starting Windows App installation from Microsoft Store"
209+
update-log -Data "Starting Windows App installation from Microsoft Store" -Class Information -Output Both
118210
install-windowsappstore
119211
}
120212
if ($source -eq "WinGet"){
121-
write-host "Starting Windows App installation from WinGet"
213+
update-log -Data "Starting Windows App installation from WinGet" -Class Information -Output Both
122214
install-windowsappwinget
123215
}
124216
if ($source -eq "MSIX"){
125-
write-host "Starting Windows App installation from MSIX download"
217+
update-log -Data "Starting Windows App installation from MSIX download" -Class Information -Output Both
126218
install-windowsappMSIX
127219
}
128220
}
129221

130222
#verify if Windows App has now been installed. If so, move to uninstalling MSRDC. Else, fail.
131223
if ((invoke-WAInstallCheck) -eq 0){
132-
write-host "Validated Windows App Installed"
224+
update-log -Data "Validated Windows App Installed" -Class Information -Output Both
133225
if ($UninstallMSRDC -eq $true){uninstall-MSRDCreg}
134-
#write-host "Installation Complete"
226+
#update-log -Data "Installation Complete"
135227
}
136228
else
137229
{
138-
write-host "Windows App does not appear to be installed. Something went wrong"
230+
update-log -Data "Windows App does not appear to be installed. Something went wrong" -Class Error -Output Both
231+
exit 1
139232
}
140233

141234
#Apply auto update registry key if option selected
@@ -145,4 +238,5 @@ if ($DisableAutoUpdate -ne 0){
145238
if ($DisableAutoUpdate -eq 3){invoke-disableautoupdate -num 3}
146239

147240
}
148-
write-host "Installation Complete"
241+
update-log -Data "Installation Complete" -Class Information -Output Both
242+
update-log -data "************" -Class Information -Output File

0 commit comments

Comments
 (0)