Using a deployment template can greatly speed up your application deployments and they offer many advantages over writing a separate script each time you wish to deploy an application. Once you have written a template and ironed out all the bugs then you end up with something that offers advanced functionality and is repeatable and reliable.
With a template, the hard work has already been done and you just have to “fill in the blanks.”
In this chapter, you will learn all about the basic deployment template I wrote, and how to use it.
What It Does
It can install based on Microsoft Office “bitness.”
It can install based on operating system architecture.
It can process pre-installation tasks.
It can process post-installation tasks.
The installation includes a log file.
The Template
The full PowerShell template
The Template – Explained
When you examine the PowerShell code for this template there will be two mindsets: those with little to no PowerShell experience will be thinking it looks complex, and those with some PowerShell experience will be thinking, “Is that it?” And really, the mindset should be of the latter because the reality is, that this is a simple template.
The template consists of three functions and one switch. Let us start with an overview of each one to better understand how it works.
Function 1: Invoke-PreInstallation
Any code you place in this section will be executed before (or pre) the application is deployed. Any additional code you add needs to be between the curly braces. ({ and })
Examples of code I have placed in these sections are usually copying files to a specific location on an endpoint, but you could do anything that PowerShell allows. Just understand that it will execute before Function 3.
You should understand that PowerShell is a script and not a compiled language. Scripts are executed from top to bottom, line-by-line, and therefore if a function has not yet been defined first, then it cannot be called later in the script. Function 1 and Function 2 are called by Function 3, and so must be defined in the script before Function 3 so that they are ready to be used.
Function 2: Invoke-PostInstallation
Any code you place in this section will be executed after (or post) the application is deployed. Any additional code you add needs to be between the curly braces. ({ and })
Examples of code I have placed in these sections are usually along the lines of registering DLLs, registry modifications to customize the application that was just installed, or copying/replacing application INI files.
Once again, you are only limited by the constraints of PowerShell. Code placed in this section will occur after Function 3.
Function 3: Invoke-ApplicationInstall
This function is the meat-and-potatoes of the template. Here you can customize the install based on Microsoft Office “bitness,” or operating system architecture.
Begin Block
Ignoring the switch for the moment, the code in the Begin { } block starts by setting the current location to the location that the script is being executed from and then populates various variables such as the logged-in user, the operating system architecture, and registry paths where one might expect to find Microsoft Office information if it were installed. The information stored in these variables will be used later.
The current location is pushed to the location stack via the Push-Location cmdlet and then a nested foreach loop is used to iterate through the Microsoft Office registry locations where an attempt is made to set the location using the Set-Location cmdlet, to the “Outlook” registry subkey. If the location can be set, then the “bitness” value is obtained, and the variable $Is32Bit is set to either $true or $false, depending on the value.
Next, a custom PowerShell object is created to store the information obtained thus far and it will be accessed later. You do not need to use a custom object here; standard variables would work just as well, it is just my preferred method. You can read all about custom objects in the Microsoft documentation.1
Finally, because the working location was potentially changed to a registry location, the working location is set back to the script execution location using the cmdlet Pop-Location.
Process Block
The process block is where the application installation will take place.
Before that can happen, Function 1 is called: Invoke-PreInstallation. This will ensure that any pre-install tasks are carried out before the program installation occurs.
If the application is to be deployed based on Office “bitness,” then that occurs next. If the application must be installed only if Microsoft Office has been detected and is 32-bit, then your application install command line must be entered in between the curly braces of the $true statement. (Replacing the text, “Install a 32-bit application here”)
If the application must be installed only if the detected Office version is 64-bit, then the application install command line must be entered between the curly braces of the $false statement.
If you wish to do something if Office was not detected, then add code between the curly braces of the Unknown statement.
Installing the application based on detected Operating System architecture is tackled next.
If the switch to install based on Office “bitness” was not used, then the application will default to installing based on if the operating system architecture is a 32-bit or 64-bit installation of Microsoft Windows.
If you wish to only install the application if the detected architecture is 64-bit, then the application installation command line must be entered between the curly braces of the $true statement. Likewise, enter the installation command line between the curly braces of the default statement to only install the application if the detected architecture is 32-bit.
Once the application has been installed, a call to Function 2 is made: Invoke-PostInstallation. This will execute any desired code after the application installation has taken place.
End Block
The end block is the last code section to execute in Function 2 and it adds basic logging. It will write a log file containing the current logged-in username, the detected Microsoft Office “bitness” and finally whether the installed operating system is 32-bit or 64-bit.
The log file is named, Invoke-ApplicationInstall.log, and can be found in the Windows emp directory.
The Switch
The template contains a single switch, $InstallBasedOnOfficeBitness. This is defined near the top of Function 3 and it is optional to use when calling the script. If it is used, then the application will only install based on the detected Office “bitness.” If the switch is not used, then the application will install based on the detected operating system architecture.
The Template Command Line
The template installation command for Intune
This template has been written as a demonstration of a simple working example. You are free to use this as-is, modify it to suit your requirements, or use it as the basis of a template you write.
It was written to demonstrate basic template structures and concepts and has room for improvement in many areas. For example, you can add more extensive logging, encapsulate the various parameters as functions (such as deploying based on Office “bitness”), which can then be called directly from the installation command line instead of using a script entry point, or perhaps all your endpoints are 64-bit, and you can remove some of the redundant system architecture code.
The world is your oyster, and you should now feel confident to perform some of these modification tasks yourself or even write a template of your own from scratch.
Summary
In this chapter, you learned why you might use a template and examined the code of a ready-to-use example template. You learned how each part of the example template was constructed, and how to use it for your deployments.
In the next chapter, you will learn about preparing the application so that it may be imported into Intune for deployment.