Force Office C2R Versions to Update
When you receive an email stating that Microsoft released a critical patch for Outlook, you might wonder how other MSPs roll out fixes like this.
If you'd like an automated way to do this that works and adds value for your customers, below is a script you can use to force Office Click-to-Run (C2R) versions to update.
Note: The forceappshutdown=true option will close any open Office apps. Either run it after-hours or change it to false.
Import-Module $env:SyncroModule
# Check if running as System
$sid = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value
if ($sid -ne "S-1-5-18") {
Write-Host "This script needs to be run as System."
Exit 1
}
$c2rPaths =@(
"$($env:ProgramFiles)\Common Files\Microsoft Shared\ClickToRun\OfficeC2RClient.exe"
"${env:ProgramFiles(x86)}\Common Files\Microsoft Shared\ClickToRun\OfficeC2RClient.exe"
)
# Add a flag to indicate whether the update was performed or not
$updatePerformed = $false
# Check each path to see which is correct
foreach ($c2rPath in $c2rPaths) {
$pathExists = Test-Path -Path $c2rPath -PathType Leaf
if ($pathExists) {
Write-Host "Updating Office using C2R located at '$($c2rPath)'."
try {
$result = Start-Process -FilePath $c2rPath -ArgumentList "/update user displaylevel=false updatepromptuser=false forceappshutdown=true" -PassThru -Wait
} catch {
Write-Host "An error occurred updating Office."
Exit 1
}
if ($result.ExitCode -eq 0) {
Write-Host "Successfully called the Office C2R updater."
$updatePerformed = $true # Set the flag to true when the update is successful
} else {
Write-Host "The Office C2R updater was not called successfully. Please investigate."
Rmm-Alert -Category "Windows | Application Management" -Body "The Office C2R updater was not called successfully. Please investigate."
Exit 1
}
}
}
# Output a message if the OfficeC2RClient.exe file wasn't found and exit with error code 2
if (-not $updatePerformed) {
Write-Host "OfficeC2RClient.exe was not found. Unable to update Office."
Exit 2
}