"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 }

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.

If you deploy an app via SCCM without a repair command defined, the only way users can re-run the install is to uninstall first, which is an extra step. If the app is deployed as ‘required’ then they can’t even do this. Therefore I always fill out the repair section unless I’m working in an Intune-only environment which doesn’t (yet!) support repairs.

1 Like

So after starting this thread, and reading all the responses, I’ve come to the conclusion that for “Required” SCCM deployments, if the uninstall is not too complicated I’ll include removal in the [PRE-INSTALL] section. Then in SCCM I’ll fill in the “Repair Program:” in deployment-type Programs tab as:

Deploy-Application.exe -DeploymentType ‘Install’

which effectively removes and reinstalls the program. But either way you’re copying your uninstall routine in two places in the script.

Idea for repairing “Required” SCCM deployments:
If the app needs to have the machine reboot for a reinstall to “stick”, place a reboot at the end of the Repair section and let SCCM deal with the reinstall.