# $COMPANY must be supplied by RMM
$PRODUCT = 'O365ProPlusRetail'
$BITNESS = '64'
$MIGARCH = 'TRUE'
$CHANNEL = 'SemiAnnual'
$UPDATES = 'TRUE'
$DISPLAY = 'None' # "None", "Full"
$ACCEULA = 'TRUE'

# Make changes if the system is 32bit
If ( !(Is64Bit) ) { $BITNESS = '32'; $MIGARCH = 'FALSE' }

$CONFIG = @"
<Configuration ID="e59fdca7-843c-4bee-8edf-33b4f8fb750f"> 
  <Add OfficeClientEdition="${BITNESS}" Channel="${CHANNEL}" MigrateArch="${MIGARCH}"> 
    <Product ID="${PRODUCT}"> 
      <Language ID="en-us" /> 
      <ExcludeApp ID="Groove" /> 
      <ExcludeApp ID="Lync" /> 
    </Product> 
  </Add> 
  <Property Name="SharedComputerLicensing" Value="0" /> 
  <Property Name="FORCEAPPSHUTDOWN" Value="TRUE" /> 
  <Property Name="DeviceBasedLicensing" Value="0" /> 
  <Property Name="SCLCacheOverride" Value="0" /> 
  <Updates Enabled="${UPDATES}" Channel="${CHANNEL}" /> 
  <RemoveMSI /> 
  <Display Level="${DISPLAY}" AcceptEULA="${ACCEULA}" /> 
  <AppSettings> 
    <Setup Name="Company" Value="${COMPANY}" /> 
  </AppSettings> 
</Configuration> 
"@

# Set path to temp directory
$TEMPDIR = '{0}microsoft-office-deployment' -f (Get-TempPath)

# Recursively delete $TEMPDIR if it already exists
If ( Test-Path $TEMPDIR ) {
    Write-Output """${TEMPDIR}"" already exists and will be deleted"
    Try { Remove-Item -Path $TEMPDIR -Recurse -Force -ErrorAction SilentlyContinue }
    Catch { Write-Error $_.Exception.Message }
}

# Create $TEMPDIR
Write-Output "Creating: ${TEMPDIR}"
Try { New-Item -Path $TEMPDIR -ItemType Directory -Force -ErrorAction Stop }
Catch { Write-Error $_.Exception.Message ; Return }

# Write $CONFIG to config.xml
$CONFIGFILE = "${TEMPDIR}\config.xml"
Try { $CONFIG | WriteToFile_UTF8NoBOM -FilePath "${CONFIGFILE}" }
Catch { Write-Output "Cannot write configuration file! This script will exit." ; Write-Error $_.Exception.Message ; Return }

# Set the name of ODT setup utility
$ODT = "${TEMPDIR}\setup.exe"

# Download ODT
Write-Output "Downloading Office Deployment Tool"
Download-File -URL 'https://www.emberkom.com/odt/setup.exe' -File $ODT

# Install Office
Write-Output "Installing Microsoft Office"
Try { Start-Process $ODT -ArgumentList "/configure ""${CONFIGFILE}""" -Wait }
Catch { Write-Error $_.Exception.Message }

# Cleanup
Write-Output "Deleting temp directory and its contents"
Try { Remove-Item -Path $TEMPDIR -Recurse -Force -ErrorAction SilentlyContinue }
Catch { Write-Error $_.Exception.Message }
