"Invoke-Repair" Function feature request

I need a simple one-liner function to use in the ‘Repair’ phase for non-MSI installers that will automatically run through the ‘Uninstall’ then the ‘Install’ phases of the script.

It’s so messy to duplicate everything into the Repair phase, and is more error-prone when editing to make sure both sections are identical.

Maybe needs a " -Delay [seconds]" parameter to pause between the two phases if required.

I have yet to use the REPAIR section.

I do uninstall of previous versions in the PreInstall section.

To avoid repetition of code, you could define scriptblocks for install and uninstall, then invoke those in the install/uninstall/repair sections.

Exactly what I do.

For example:

#   ________________
#┌─┤ INSTALLATION ► ├────────────────────────────────────┐
#   ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
[ScriptBlock]$InstallScriptBlock = {
	$installPhase = "Pre-Installation"

	# < Commands >


	$installPhase = "Installation"

	# < Commands >


	$installPhase = "Post-Installation"

	# < Commands >
#   __________________
#┌─┤ UNINSTALLATION ► ├──────────────────────────────────┐
#   ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
[ScriptBlock]$UninstallScriptBlock = {
	$installPhase = "Pre-Uninstallation"

	# < Commands >


	$installPhase = "Uninstallation"

	# < Commands >


	$installPhase = "Post-Uninstallation"

	# < Commands >
#region REPAIR
#   __________
#┌─┤ REPAIR ► ├──────────────────────────────────────────┐
#   ‾‾‾‾‾‾‾‾‾‾
[ScriptBlock]$RepairScriptBlock = {
	$installPhase = "Pre-Repair"

	# < Commands >


	$installPhase = "Repair"

	#▌Invokes the uninstallation commands
	Invoke-Command -ScriptBlock $UninstallScriptBlock -NoNewScope

	#▌Invokes the installation commands
	Invoke-Command -ScriptBlock $InstallScriptBlock -NoNewScope

	$installPhase = "Post-Repair"

	# < Comandos >
# ▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲

if ($deploymentType -ine "Uninstall" -and $deploymentType -ine "Repair") { Invoke-Command -ScriptBlock $InstallScriptBlock -NoNewScope }
elseif ($deploymentType -ieq "Uninstall") { Invoke-Command -ScriptBlock $UninstallScriptBlock -NoNewScope }
elseif ($deploymentType -ieq "Repair") { Invoke-Command -ScriptBlock $RepairScriptBlock -NoNewScope }
1 Like

This is probably the way to go I suppose, but it’s a bit of a re-write of the template. I’ll have to play with this a bit and see.

If DanGough and LFM8787 are both doing this, and I’m looking for similar functionality, it might be worth creating a function for. There’s probably DOZENS of us! DOZENS! :stuck_out_tongue_winking_eye:

So for SCCM “required” deployments, if you want to give the option for clients to run a repair, you just run the install again?

You do not need to give a [Repair] button to each Application.
For most situations, installing over top a broken installation will work.
If the re-installation over top fails, an uninstall (with reboot or without reboot) and re-install usually works.

The only time a [Repair] button is useful is:
1- You need the user to be the one who decides and initiate the repair
2a- the vendor package supports the Repair capability directly
2b- You have intimate knowledge that a install-over-top-bad will have negative consequences and you know how to avoid it using scripting.