Added stage mechanism

Script can continue from different stages now
This commit is contained in:
Aaron Viehl 2022-02-06 11:31:49 +01:00
parent 4959f74e88
commit 10b44b561d
2 changed files with 108 additions and 27 deletions

View File

@ -1,5 +1,5 @@
#requires -version 2 #requires -version 2
#Requires -RunAsAdministrator #//Requires -RunAsAdministrator
<# <#
.SYNOPSIS .SYNOPSIS
@ -15,6 +15,10 @@
Will use the setup method to remove current Office installations instead of SaRA. Will use the setup method to remove current Office installations instead of SaRA.
.PARAMETER Force .PARAMETER Force
Skip user-input. Skip user-input.
.PARAMETER RunAgain
Skip Stage validation and runs the whole script again.
.PARAMETER SecondsToReboot
Seconds before the computer will reboot.
.INPUTS .INPUTS
None None
.OUTPUTS .OUTPUTS
@ -33,9 +37,10 @@ Param (
[switch]$InstallOffice365 = $False, [switch]$InstallOffice365 = $False,
[switch]$SuppressReboot = $False, [switch]$SuppressReboot = $False,
[switch]$UseSetupRemoval = $False, [switch]$UseSetupRemoval = $False,
[Switch]$Force = $False [Switch]$Force = $False,
[switch]$RunAgain = $False,
[int]$SecondsToReboot = 60
) )
#----------------------------------------------------------[Declarations]---------------------------------------------------------- #----------------------------------------------------------[Declarations]----------------------------------------------------------
$SaRA_URL = "https://aka.ms/SaRA_CommandLineVersionFiles" $SaRA_URL = "https://aka.ms/SaRA_CommandLineVersionFiles"
$SaRA_ZIP = "$env:TEMP\SaRA.zip" $SaRA_ZIP = "$env:TEMP\SaRA.zip"
@ -45,7 +50,7 @@ $Office365Setup_URL = "https://github.com/Admonstrator/msoffice-removal-tool/raw
#-----------------------------------------------------------[Functions]------------------------------------------------------------ #-----------------------------------------------------------[Functions]------------------------------------------------------------
Function Invoke-OfficeUninstall { Function Invoke-OfficeUninstall {
if (-Not (Test-Path "$SaRA_DIR")) { if (-Not (Test-Path "$SaRA_DIR")) {
New-Item "$SaRA_DIR" -ItemType Directory New-Item "$SaRA_DIR" -ItemType Directory | Out-Null
} }
if ($UseSetupRemoval) { if ($UseSetupRemoval) {
Write-Host "Invoking default setup method ..." Write-Host "Invoking default setup method ..."
@ -108,21 +113,25 @@ Function Invoke-SaRA {
switch ($SaRAProcess.ExitCode) { switch ($SaRAProcess.ExitCode) {
0 { 0 {
Write-Host "Uninstall successful!" Write-Host "Uninstall successful!"
Set-CurrentStage 2
Break Break
} }
7 { 7 {
Write-Host "No office installations found." Write-Host "No office installations found."
Set-CurrentStage 2
Break Break
} }
8 { 8 {
Write-Error "Multiple office installations found. Program need to be run in GUI mode." Write-Error "Multiple office installations found. Program need to be run in GUI mode."
Set-CurrentStage 4
Exit 2 Exit 2
} }
9 { 9 {
Write-Error "Uninstall failed! Program need to be run in GUI mode." Write-Error "Uninstall failed! Program need to be run in GUI mode."
Set-CurrentStage 4
Exit 3 Exit 3
} }
} }
@ -138,23 +147,33 @@ Function Invoke-SetupOffice365($Office365ConfigFile) {
switch ($OfficeSetup.ExitCode) { switch ($OfficeSetup.ExitCode) {
0 { 0 {
Write-Host "Install successful!" Write-Host "Install successful!"
Set-CurrentStage 3
Break Break
} }
1 { 1 {
Write-Error "Install failed!" Write-Error "Install failed!"
Set-CurrentStage 3
Break Break
} }
} }
} }
} }
Function Invoke-Reboot { Function Invoke-RebootInSeconds($Seconds) {
if (-not $SuppressReboot) { if (-not $SuppressReboot) {
Start-Process -FilePath "$env:SystemRoot\system32\shutdown.exe" -ArgumentList "/r /c `"Reboot needed. System will reboot in 60 seconds.`" /t 60 /f /d p:4:1" Start-Process -FilePath "$env:SystemRoot\system32\shutdown.exe" -ArgumentList "/r /c `"Reboot needed. System will reboot in $Seconds seconds.`" /t $Seconds /f /d p:4:1"
} }
} }
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Function Set-CurrentStage($StageValue) {
if (-not (Test-Path "HKLM:\Software\OEM\101\M365\Install")) {
New-Item -Path "HKLM:\Software\OEM\101\M365\Install" -Force | Out-Null
}
New-ItemProperty -Path "HKLM:\Software\OEM\101\M365\Install" -Name "CurrentStage" -Value $StageValue -PropertyType String -Force | Out-Null
}
Function Invoke-Intro {
Write-Host " ___ ___ ___ _____ _____ _____ " Write-Host " ___ ___ ___ _____ _____ _____ "
Write-Host "|_ | | |_ | | __| __| |" Write-Host "|_ | | |_ | | __| __| |"
Write-Host " _| |_| | |_| |_ | __| __| | | |" Write-Host " _| |_| | |_| |_ | __| __| | | |"
@ -164,7 +183,10 @@ Write-Host "Microsoft Office Removal Tool"
Write-Host "by Aaron Viehl (101 Frankfurt)" Write-Host "by Aaron Viehl (101 Frankfurt)"
Write-Host "einsnulleins.de" Write-Host "einsnulleins.de"
Write-Host "" Write-Host ""
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
# Check if -Force is set
if (-Not $Force) { if (-Not $Force) {
do { do {
$YesOrNo = Read-Host "Are you sure you want to remove Office from this PC? (y/n)" $YesOrNo = Read-Host "Are you sure you want to remove Office from this PC? (y/n)"
@ -175,7 +197,58 @@ if (-Not $Force) {
} }
} }
# Check if there is a stage to resume
if (-not ($RunAgain)) {
if (Test-Path "HKLM:\Software\OEM\101\M365\Install") {
$CurrentStageValue = (Get-ItemProperty "HKLM:\Software\OEM\101\M365\Install").CurrentStage
Switch ($CurrentStageValue) {
1 {
Write-Host "Resuming Stage 1: Uninstalling Office ..."
Invoke-OfficeUninstall
Invoke-SetupOffice365 "$Office365Setup_URL/upgrade.xml"
Remove-SaRA
Invoke-RebootInSeconds $SecondsToReboot
}
2 {
Write-Host "Resuming Stage 2: Installing Office 365 ..."
Invoke-SetupOffice365 "$Office365Setup_URL/upgrade.xml"
Remove-SaRA
Invoke-RebootInSeconds $SecondsToReboot
}
3 {
Write-Host "Resuming Stage 3: Cleaning up ..."
Remove-SaRA
}
4 {
# Final stage: All is done, script will not run.
exit 0
}
default {
Write-Host "Resuming Stage 1: Uninstalling Office ..."
Invoke-OfficeUninstall
Invoke-SetupOffice365 "$Office365Setup_URL/upgrade.xml"
Remove-SaRA
Invoke-RebootInSeconds $SecondsToReboot
}
}
}
else {
Invoke-Intro
Stop-OfficeProcess Stop-OfficeProcess
Invoke-OfficeUninstall Invoke-OfficeUninstall
Invoke-SetupOffice365 "$Office365Setup_URL/upgrade.xml" Invoke-SetupOffice365 "$Office365Setup_URL/upgrade.xml"
Invoke-Reboot Invoke-RebootInSeconds $SecondsToReboot
}
}
else {
Invoke-Intro
Stop-OfficeProcess
Invoke-OfficeUninstall
Invoke-SetupOffice365 "$Office365Setup_URL/upgrade.xml"
Invoke-RebootInSeconds $SecondsToReboot
}
exit

View File

@ -24,12 +24,20 @@ By using `-UseSetupRemoval` the Office365 setup method will be used.
## Parameter ## Parameter
| Parameter | Usage | | Parameter | Usage |
|-------------------|-------------------------------------------------------------------------| |------------------------|-------------------------------------------------------------------------|
| -InstallOffice365 | The script will try to install the newest Office365 build after removal | | -InstallOffice365 | The script will try to install the newest Office365 build after removal |
| -SuppressReboot | No reboot will be executed after script is done | | -SuppressReboot | No reboot will be executed after script is done |
| -UseSetupRemoval | Will use the official Office365 setup instead of SaRA | | -UseSetupRemoval | Will use the official Office365 setup instead of SaRA |
| -RunAgain | Will skip the stage mechanism - whole script will run again |
| -Force | Non-interactive - No user interaction required | | -Force | Non-interactive - No user interaction required |
| -SecondsToReboot [int] | Seconds until the computer will reboot |
### Example ### Example
``.\msoffice-removal-tool.ps1 -InstallOffice365 -SuppressReboot -Force`` ``.\msoffice-removal-tool.ps1 -InstallOffice365 -SuppressReboot -Force``
### Stage mechanism
To make sure that the program will only do the necessary parts a _stage mechanism_ is builtin. After every stage a registry value will be written to ``HKLM:\Software\OEM\101\M365\Install\CurrentStage``
To overwrite all stages and restart the whole script use ``-RunAgain``