Public/Install-specApplication.ps1

Function Install-specApplication {
    <#
    .SYNOPSIS
        Installs a specified application with optional parameters and checks for a specific service.
 
    .DESCRIPTION
        The Install-specApplication function installs a specified application and provides options for additional configurations.
        It can check for the existence of a service before installation and handle various exit codes upon completion.
 
    .PARAMETER FilePath
        Specifies the path to the executable file of the application to be installed. (Mandatory)
 
    .PARAMETER Arguments
        Specifies optional command-line arguments for the application. (Optional)
 
    .PARAMETER WindowStyle
        Specifies the window style for the application during installation. Valid values are 'Normal', 'Hidden', 'Minimized', or 'Maximized'. Default is 'Hidden'. (Optional)
 
    .PARAMETER ServiceToCheck
        Specifies the name of a service to check before installation. If the service exists, the installation is skipped. (Optional)
 
    .PARAMETER SuccessfulExitCode
        Specifies the exit code that indicates a successful installation. Default is 0. (Optional)
 
    .EXAMPLE
        Install-specApplication -FilePath "C:\Path\To\Setup.exe" -Arguments "/S" -WindowStyle "Hidden" -ServiceToCheck "MyService" -SuccessfulExitCode 0
        Installs the specified application with provided arguments, and expects a successful exit code of 0.
 
    .NOTES
        RETURN_CODES
        0 - Successfully installed the application.
        1500 - Service to check is already installed; installation skipped.
        1501 - Failed to install the application with specified arguments.
        1502 - Failed to install the application without arguments.
        1503 - Failed to install the application with an unexpected exit code.
        1603 - Error 1603: Failed to install the application; check permissions.
 
        File: Install-specApplication.ps1
        Author: owen.heaume
        Version: 1.0
    #>


    [cmdletbinding()]

    param (
        [Parameter(Mandatory = $true)]
        [string]$FilePath,

        [Parameter(Mandatory = $false)]
        [string]$Arguments,

        [Parameter(Mandatory = $false)]
        [ValidateSet('Normal', 'Hidden', 'Minimized', 'Maximized')]
        [string] $WindowStyle = 'Hidden',

        [Parameter(Mandatory = $false)]
        [int]$SuccessfulExitCode = 0
    )

    begin { }

    process {

        if ($PSBoundParameters.ContainsKey('Arguments') -and $Arguments -ne $null) {
            try {
                write-host "Arguments supplied, installing $FilePath with arguments '$Arguments'" -ForegroundColor DarkGray
                $procExitCode = Start-Process -FilePath $FilePath -ArgumentList $Arguments -Wait -PassThru -ErrorAction Stop -WindowStyle $WindowStyle
            } catch {
                Write-Host "Failed to install $FilePath with arguments '$Arguments'" -ForegroundColor DarkYellow
                Write-host "The error was: $_" -ForegroundColor DarkYellow
                return 1501
            }
        } else {
            try {
                write-host "No arguments supplied, installing $FilePath" -ForegroundColor DarkGray
                $procExitCode = Start-Process -FilePath $FilePath -Wait -PassThru -ErrorAction Stop -WindowStyle $WindowStyle
            } catch {
                Write-Host "Failed to install $FilePath" -ForegroundColor DarkYellow
                Write-host "The error was: $_" -ForegroundColor DarkYellow
                return 1502
            }
        }

        switch ($procExitCode.ExitCode) {
            $SuccessfulExitCode {
                Write-Host "Successfully installed $FilePath with arguments '$Arguments'" -ForegroundColor DarkGreen
                return 0
            }
            1603 {
                Write-Host "Error 1603: Failed to install $FilePath. Do you have the correct permissions?" -ForegroundColor DarkYellow
                return 1603
            }
            default {
                Write-Host "Failed to install $FilePath with arguments '$Arguments'" -ForegroundColor DarkYellow
                Write-Host "The error was: $($procExitCode.exitcode)"
                return 1503
            }
        }
    }

    end {}
}