Office-Update.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<#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