Automated Office Updates v1.0

My Automated Office Updates PowerShell script can also be downloaded from the Microsoft TechNet Gallery the PowerShell Gallery and GitHub.

-Mike

 

Features and Requirements

This script will check for and download update files for click-to-run Office installs such as Office 365 and Office 2019. All options are added via command line switches. Options include:

  • Location to store Office installation files.
  • Keep or remove old office update files to prevent bloat.
  • The directory to output a log file to.
  • An optional email address to send the log file to.

This script requires a certain folder structure in order to operate. The script expects the Office Deployment Tool and the configuration xml file to be in the same folder. Additionally, the source path of the Office installation files in the configuration xml file should be set to the same location.

For example:

Office Deployment Tool location: \\server\share\Office-2019-x64\setup.exe

Configuration xml file location: \\server\share\Office-2019-x64\config-2019-x64.xml

Source path in the configuration xml file: \\server\share\Office-2019-x64

This configuration will result in the Office update files being downloaded and stored in: \\server\share\Office-2019-x64\Office\Data.

 

This script has been tested running on Windows Server 2016 and Windows Server 2019, updating Office 2019 volume licensed installations on Windows 10 1809 and 1903.

 

Generating A Password File

The password used for SMTP server authentication must be in an encrypted text file. To generate the password file, run the following command in PowerShell, on the computer that is going to run the script and logged in with the user that will be running the script. When you run the command you will be prompted for a username and password. Enter the username and password you want to use to authenticate to your SMTP server.

Please note: This is only required if you need to authenticate to the SMTP server when send the log via e-mail.

$creds = Get-Credential
$creds.Password | ConvertFrom-SecureString | Set-Content c:\scripts\ps-script-pwd.txt

After running the commands, you will have a text file containing the encrypted password. When configuring the -Pwd switch enter the path and file name of this file.

 

Configuration

The table below shows all the command line options available with descriptions and example configurations.

Command Line Switch Mandatory Description Example
-Office Yes The folder containing the Office Deployment Tool (ODT). \\server\share\office-19-x64\setup.exe
-Config Yes The name of the configuration xml file for the Office ODT. It must be located in the same folder as the ODT. config-19-x64.xml
-Days No The number of days that you wish to keep old update files for. If you do not configure this option, no old files will be removed. 60
-L No Location to store the optional log file. The name of the log file is automatically generated. C:\foo
-SendTo No The email address to send the log file to. me@contoso.com
-From No* The email address that the log file should be sent from.

*This switch isn’t mandatory but is required if you wish to email the log file.

example@contoso.com
-Smtp No* SMTP server address to use for the email functionality.

*This switch isn’t mandatory but is required if you wish to email the log file.

mail01.contoso.com

OR

smtp.live.com

OR

smtp.office365.com

-User No* The username of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

example@contoso.com
-Pwd No* The location of the file containing the encrypted password of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on your SMTP server.

c:\foo\ps-script-pwd.txt
-UseSsl No* Add this option if you wish to use SSL with the configured SMTP server.

Tip: If you wish to send email to outlook.com or office365.com you will need this.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

N/A

 

Change Log

2019-06-16 1.0

  • Initial release.

 

PowerShell Code


<#PSScriptInfo .VERSION 1.0 .GUID 72cb5483-744e-4a7d-bcad-e04462ea2c2e .AUTHOR Mike Galvin twitter.com/mikegalvin_ .COMPANYNAME .COPYRIGHT (C) Mike Galvin. All rights reserved. .TAGS Office 2019 365 Click-to-run updates .LICENSEURI .PROJECTURI https://gal.vin/2019/06/16/automated-office-updates .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #>

<# .SYNOPSIS Checks for updates to Office Click-to-run source files. .DESCRIPTION A simple script to check for updates to Office click-to-run source files. If updates are available they are downloaded. An email notification can be configured to be sent. Please note: to send a log file using ssl and an SMTP password you must generate an encrypted password file. The password file is unique to both the user and machine. The command is as follows: $creds = Get-Credential $creds.Password | ConvertFrom-SecureString | Set-Content c:\foo\ps-script-pwd.txt .PARAMETER Office The location of the Office Deployment Tool. Must be a folder. .PARAMETER Config The name of the configuration xml file for the Office Deployment Tool. It must be in the same folder of the Office deployment tool. .PARAMETER Days The number of days that you wish to keep old update files for. If you do not configure this option, no old update files will be removed. .PARAMETER L The path to output the log file to. The file name will be Office-Update.log. .PARAMETER SendTo The e-mail address the log should be sent to. .PARAMETER From The from address the log should be sent from. .PARAMETER Smtp The DNS name or IP address of the SMTP server. .PARAMETER User The user account to connect to the SMTP server. .PARAMETER Pwd The password for the user account. .PARAMETER UseSsl Connect to the SMTP server using SSL. .EXAMPLE Office-Update.ps1 -Office C:\officesrc -config config.xml -Days 60 -L C:\scripts\log -SendTo me@contoso.com -From Office-Update@contoso.com -Smtp exch01.contoso.com -User me@contoso.com -Pwd P@ssw0rd -UseSsl The above command will run the script, download the Office files to C:\officesrc\Office. It will use a configuration file called config.xml in the C:\officesrc folder. Any update files older than 60 days will be removed. If the download is successful a log file is generated and it can be emailed as a notification that a download occurred. #>

## Set up command line switches and what variables they map to
[CmdletBinding()]
Param(
    [parameter(Mandatory=$True)]
    [alias("Office")]
    [ValidateScript({Test-Path $_ -PathType 'Container'})]
    $OfficeSrc,
    [parameter(Mandatory=$True)]
    [alias("Config")]
    $Cfg,
    [alias("Days")]
    $Time,
    [alias("L")]
    [ValidateScript({Test-Path $_ -PathType 'Container'})]
    $LogPath,
    [alias("SendTo")]
    $MailTo,
    [alias("From")]
    $MailFrom,
    [alias("Smtp")]
    $SmtpServer,
    [alias("User")]
    $SmtpUser,
    [alias("Pwd")]
    [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
    $SmtpPwd,
    [switch]$UseSsl)

#Run update process.
& $OfficeSrc\setup.exe /download $OfficeSrc\$Cfg

## Location of the office source files.
$UpdateFolder = "$OfficeSrc\Office\Data"

## Check the last write time of the office source files folder if it is greater than the previous day.
$Updated = (Get-ChildItem -Path $UpdateFolder | Where-Object CreationTime -gt (Get-Date).AddDays(-1)).Count

## If the Updated variable returns as not 0...
If ($Updated -ne 0)
{
    ## If logging is configured, start logging
    If ($LogPath)
    {
        $LogFile = "Office-Update.log"
        $Log = "$LogPath\$LogFile"

        ##Test for the existence of the log file
        $LogT = Test-Path -Path $Log

        ## If the log file already exists, clear it.
        If ($LogT)
        {
            Clear-Content -Path $Log
        }

        Add-Content -Path $Log -Value "****************************************"
        Add-Content -Path $Log -Value "$(Get-Date -Format G) Log started"
        Add-Content -Path $Log -Value " "
    }

    Write-Host
    Write-Host -Object "Office source files were updated."
    Write-Host -Object "New version is:"

    ## List the update folder contents and the last write time.
    Get-ChildItem -Path $UpdateFolder -Directory | Where-Object CreationTime –gt (Get-Date).AddDays(-1) | Select-Object -ExpandProperty Name

    ## If logging was configured, write to the log.
    If ($LogPath)
    {
        Add-Content -Path $Log -Value "Office source files were updated"
        Add-Content -Path $Log -Value "New version is:"
        Get-ChildItem -Path $UpdateFolder | Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-1)} | Select-Object -Property Name, CreationTime | Out-File -Append $Log -Encoding ASCII
        Add-Content -Path $Log -Value " "
    }

    If ($Null -ne $Time)
    {
        ## If logging was configured, write to the log.
        If ($LogPath)
        {
            Add-Content -Path $Log -Value "Old Office source files were removed:"
            Get-ChildItem -Path $UpdateFolder | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-$Time)} | Select-Object -Property Name, LastWriteTime | Out-File -Append $Log -Encoding ASCII
            Add-Content -Path $Log -Value " "
        }

        Write-Host
        Write-Host "Old Office source files were removed:"
        Get-ChildItem -Path $UpdateFolder | Where-Object LastWriteTime –lt (Get-Date).AddDays(-$Time)

        ## If configured, remove the old files
        Get-ChildItem $UpdateFolder | Where-Object {$_.LastWriteTime –lt (Get-Date).AddDays(-$Time)} | Remove-Item -Recurse
    }

    ## If logging was configured stop the log
    If ($LogPath)
    {
        Add-Content -Path $Log -Value " "
        Add-Content -Path $Log -Value "$(Get-Date -Format G) Log finished"
        Add-Content -Path $Log -Value "****************************************"

        ## If email was configured, set the variables for the email subject and body
        If ($SmtpServer)
        {
            $MailSubject = "Office Update Log"
            $MailBody = Get-Content -Path $Log | Out-String

            ## If an email password was configured, create a variable with the username and password
            If ($SmtpPwd)
            {
                $SmtpPwdEncrypt = Get-Content $SmtpPwd | ConvertTo-SecureString
                $SmtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList ($SmtpUser, $SmtpPwdEncrypt)

                ## If ssl was configured, send the email with ssl
                If ($UseSsl)
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -UseSsl -Credential $SmtpCreds
                }

                ## If ssl wasn't configured, send the email without ssl
                Else
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -Credential $SmtpCreds
                }
            }

            ## If an email username and password were not configured, send the email without authentication
            Else
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer
            }
        }
    }
}

## End

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.