Worklet: Install Slack on Windows

Installs Slack on Windows.

Evaluation code:

#REQUIRES -Version 2.0
<#
.SYNOPSIS
    This Test script checks to see if Slack is installed
.DESCRIPTION
    This script queries the installed files for 32 and 64bit software
    and returns a 0 if product is installed or 1 if not
.Notes
    File Name       :Slack_Install_Test.ps1
    Author          :Automox
    Prerequisite    :PowerShell V2 over win7 and upper
#>
#Handle Exit Codes:
trap {  $host.ui.WriteErrorLine($_.Exception); exit 90 }

function Slack_Install_Test {

	<#
    .SYNOPSIS
        This function Checks for the Slack is Installed on system or not .
    .DESCRIPTION
        After checking the Slack availability, based on the exit code tool will decide to call remediation code or not.
    .EXAMPLE
        Slack_Install_Test
    .NOTES
    #>

	# Name of the desired application.
	$appName = 'Slack'
	# Finding the Systen Directory (system32 Directory).
	$sysDir = [Environment]::SystemDirectory

	# Making the 32 bit and 64 bit path names blank.
	$32BIT = ""
	$64BIT = ""

	#Get Registry value for the 32 bit software installed on both 32-bit and 64-bit machine
	Try {  $32BIT = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' -ErrorAction Stop |
			Select-Object DisplayName |
			Select-String "$appName" |
			Out-String
	}
	Catch { $Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber;
			Write-Output $Exception
			exit 90
	}
	# Script blcok to execute with powershell
	# Match name in the block should be hard coded.
	$scriptBlock = {$key = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' |
					Where-Object { $_.DisplayName -match 'Slack'}
					return $key
					}

	# Get the Registry value for the 64 bit software installed on the 64 bit machine. as the automox process is 32 bit
	if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "64-bit")
	{
		# Call for accessing the 64 bit registry incase 32 bit process not access the registry.
		Try
		{
			$installed64 = @(& "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptBlock)
			$64BIT = $installed64.DisplayName
		}
		Catch
		{
			$Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber;
			Write-Output $Exception
			exit 90
		}

	}
	# Check for the Availability of the software and exit with relevent exit code.
	if ($32BIT.Trim() -match $appName -or ($64BIT -ne $null -and $64BIT.Trim() -match $appName)) {
		#Application Found, Automox can handle the updates!
		exit 0
	} else {
		# Application is not installed! Run Remediation Script to install it.
		exit 1
	}
}

Slack_Install_Test

Remediation code:

#REQUIRES -Version 2.0
<#
.SYNOPSIS
    This script allows an admin to install Slack
.DESCRIPTION
    This script downloads the system-wide-slack installer. Once installed, the program
    checks to see if slack is installed WHEN A USER LOGS IN. If slack is not installed
    the program installs it in the packground.
    If a user has uninstalled slack, this will not reinstall it.
.NOTES
    File Name       :Slack_Install.ps1
    Author          :Automox
    Prerequisite    :PowerShell V2 over win7 and upper
#>
#Handle Exit Codes:
trap {  $host.ui.WriteErrorLine($_.Exception); exit 90 }

function Slack_Install{

    <#
    .SYNOPSIS
        This function allows automox to Install the Slack on Windows .
    .DESCRIPTION
        This function download and installs the latest version of Slack on system.
    .EXAMPLE
        Slack_Install
    .NOTES
    #>

    #############Change the settings in this block#######################
    # Save the installer in the powershell current working directory.
    $saveFilePath = "$PSScriptRoot\SlackSetup.msi"
	
	# If any old installer file is existed, Delete the old installer.
    if (Test-Path -Path "$saveFilePath") {
		Remove-Item -force "$saveFilePath" -ErrorAction SilentlyContinue
    }

    # Download msi Installer based on the bit version of the Windows OS
    if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "64-bit") {
        $URL = "http://slack.com/ssb/download-win64-msi"
    } else  {
        $URL = "http://slack.com/ssb/download-win-msi"
    }

    ###### Download the msi installer file for the installation ######
    $downloader = (new-object System.Net.WebClient)
    Try {
        $downloader.DownloadFile("$URL", "$saveFilePath")
        Write-Output "Slack Installer download finished..."
    }
    Catch { Write-Output "File download failed. Check URL [$URL] and try again."
            exit 90
    }

    ###### Installing  slack on system ######
    Try {
        $process=Start-Process -FilePath msiexec.exe -ArgumentList '/i',$saveFilePath,'/q' -Wait -PassThru -ErrorAction Stop
        Write-Output "Slack Installion Finished..."
    }
    Catch { $Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber
            Write-Output $Exception
            exit 90
    }

    # Avoid error while deleting the installer as it is still in the process of installation.
    do {
        if ((Get-Content $saveFilePath)) {
            Write-Output "Installer complete...cleaning up installation files"
            Remove-Item -force "$saveFilePath" -ErrorAction SilentlyContinue
            exit $process.ExitCode
        } else {
            Write-Output "Waiting for Installer File Unlock"
        }
        $fileunlocked = 1
    }	Until ($fileunlocked)

}

Slack_Install