Saturday, 13 February 2016

Powershell Script to install MS Office Templates

To deploy custom Office templates for users first create the templates , preview image files and thumbnail image files per the MS article: https://technet.microsoft.com/en-us/library/cc178976%28v=office.14%29.aspx#section4 and create a .reg file to set the provider keys to point to the XML, create different reg files for office 2010 and 2013, change the service URL to whatever location the XML is located at :


Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Common\Spotlight\Providers\Workaround]
"ServiceURL"="C:\\Program Files (x86)\\Microsoft Office\\Templates\\Workaround\\Workaround_Templates.xml"

Place templates, images and the XML into a folder within the script and adjust the variables in the script accordingly:

#############Install FICO templates####################
#Author: tausif
#Company: FICO
#Date: 13/02/2016

#---------------------------------------------------------[Initialisations]--------------------------------------------------------

#Set Error Action to Silently Continue
#$ErrorActionPreference = "SilentlyContinue"

#----------------------------------------------------------[Declarations]----------------------------------------------------------

#Script Version
$sScriptVersion = "1.0"
#script root

$PSScriptRoot = ($MyInvocation.MyCommand.Path | Split-Path | Resolve-Path).ProviderPath

#logging variables.
$logdir = "$($env:ALLUSERSPROFILE)\InstallLogs"
$Logfile = "$($logdir)\FICO_2016_Templates_$(get-date -format `"yyyyMMdd_hhmmsstt`").log"

$FICOTEMPLATESDIR = "$($env:ALLUSERSPROFILE)\FICO Templates"
$PROGRAMDATA = $env:ALLUSERSPROFILE

Function LogWrite($string, $color)
{
   if ($Color -eq $null) {$color = "white"}
   write-host $string -foregroundcolor $color
   $string | out-file -Filepath $logfile -append
}

if(!(Test-Path -Path $logdir )){
    New-Item -ItemType directory -Path $logdir
}


########################################

$CP_USER_NAME = (Get-WmiObject Win32_ComputerSystem | Select-Object -ExpandProperty UserName).Split('\')[1]
$user = New-Object System.Security.Principal.NTAccount("corp", "$CP_USER_NAME") 
$sid = $user.Translate([System.Security.Principal.SecurityIdentifier]) 
$userprofile = Get-WmiObject -Namespace root/cimv2 -Class win32_userprofile -Filter "SID='$($sid.value)'"
$CP_USER_HOME = $userprofile.localpath
$CP_USER_TEMPLATESDIR = "$CP_USER_HOME" + "\" + "AppData\Roaming\Microsoft\Templates"
$FICO_USER_TEMPLATESDIR = "$CP_USER_HOME" + "\" + "AppData\Roaming\Microsoft\Templates\FICO Templates"

if(Test-Path -Path $FICOTEMPLATESDIR){
    Remove-Item -Path $FICOTEMPLATESDIR -Force -Recurse
    LogWrite "Removed existing older Template provider keys"
}

Copy-Item "$PSScriptRoot\FICO Templates" -Destination $PROGRAMDATA -Force -Recurse
LogWrite "Copied Office templates to destination"

# Set PowerPoint exe locations
$x86_PP_2013 = "${env:PROGRAMFILES(X86)}\Microsoft Office\Office15\POWERPNT.EXE"
$x64_PP_2013 = "${env:COMMONPROGRAMFILES}\Microsoft Office\Office15\POWERPNT.EXE"
$x86_PP_2010 = "${env:PROGRAMFILES(X86)}\Microsoft Office\Office14\POWERPNT.EXE"
$x64_PP_2010 = "${env:COMMONPROGRAMFILES}\Microsoft Office\Office14\POWERPNT.EXE"

#Set providers using reg files
$2010 = "$PSScriptRoot\2010.reg"
$2013 = "$PSScriptRoot\2013.reg"

# X64 OS and Office 2013
if (test-path $x64_PP_2013)
    { 
        LogWrite "64 bit office present"
        Remove-Item -Path HKCU:\Software\Microsoft\Office\15.0\Common\Spotlight\Providers\* -Recurse
        Remove-Item -Path HKCU:\Software\Microsoft\Office\15.0\Common\Spotlight\Content\* -Recurse
        $import = $(Start-Process "regedit.exe" -argumentlist ("/s", "$2013") -Wait -PassThru)
        if ($import.ExitCode -eq 0){
            LogWrite "Templates for Office 2013 have been successfully installed" -color yellow
        }
            else {
            LogWrite "installer exit code  $($import.ExitCode) for file  $($msifile)" -color red
        }
    }
 
# X64 OS and Office 2010
if (test-path $x64_PP_2010)
    { 
        LogWrite "64 bit office 2010 present"
        Remove-Item -Path HKCU:\Software\Microsoft\Office\14.0\Common\Spotlight\Providers\* -Recurse
        Remove-Item -Path HKCU:\Software\Microsoft\Office\15.0\Common\Spotlight\Content\* -Recurse
        $import = $(Start-Process "regedit.exe" -argumentlist ("/s", "$2010") -Wait -PassThru)
        if ($import.ExitCode -eq 0){
            LogWrite "Templates for Office 2010 have been successfully installed" -color yellow
        }
            else {
            LogWrite "installer exit code $($import.ExitCode)" -color red
        }
    }
# X86 OS and Office 2013
if (test-path $x86_PP_2013)
    { 
        LogWrite "32 bit office 2013 present"
        Remove-Item -Path HKCU:\Software\Microsoft\Office\14.0\Common\Spotlight\Providers\* -Recurse
        Remove-Item -Path HKCU:\Software\Microsoft\Office\15.0\Common\Spotlight\Content\* -Recurse
        $import = $(Start-Process "regedit.exe" -argumentlist ("/s", "$2013") -Wait -PassThru)
        if ($import.ExitCode -eq 0){
            LogWrite "Templates for Office 2013 have been successfully installed" -color yellow
        }
            else {
            LogWrite "installer exit code $($import.ExitCode)" -color red
        }
    }
# X86 OS and Office 2010
if (test-path $x86_PP_2010)
    { 
        LogWrite "32 bit office 2010 present"
        Remove-Item -Path HKCU:\Software\Microsoft\Office\14.0\Common\Spotlight\Providers\* -Recurse
        Remove-Item -Path HKCU:\Software\Microsoft\Office\15.0\Common\Spotlight\Content\* -Recurse
        $import = $(Start-Process "regedit.exe" -argumentlist ("/s", "$2010") -Wait -PassThru)
        if ($import.ExitCode -eq 0){
            LogWrite "Templates for Office 2010 have been successfully installed" -color yellow
        }
            else {
            LogWrite "installer exit code $($import.ExitCode)" -color red
        }
    } 

This script creates logs in C:\ProgramData\InstallLogs
and copies templates, images and XML to C:\ProgramData\FICO Templates


Detect autopilot session

  Ensuring that some apps only install during autopilot is not easily accomplished, you can use the below powershell script as a requiremen...