PowerShell script function file download, unblock and install with multiple parameters and return installed version

Just another Tech site

PowerShell script function file download, unblock and install with multiple parameters and return installed version

Here’s a PowerShell script that defines a function to download a file, unblock it, install it, and return the installed version. The function accepts multiple parameters for flexibility.

function Install-Software {
    param (
        [Parameter(Mandatory = $true)]
        [string]$DownloadUrl,

        [Parameter(Mandatory = $true)]
        [string]$DestinationPath,

        [Parameter(Mandatory = $true)]
        [string]$InstallerArguments,

        [Parameter(Mandatory = $true)]
        [string]$VersionCommand
    )

    try {
        # Step 1: Download the file
        Write-Host "Downloading file from $DownloadUrl to $DestinationPath..."
        Invoke-WebRequest -Uri $DownloadUrl -OutFile $DestinationPath -ErrorAction Stop

        # Step 2: Unblock the file
        Write-Host "Unblocking the downloaded file..."
        Unblock-File -Path $DestinationPath

        # Step 3: Install the software
        Write-Host "Installing the software..."
        Start-Process -FilePath $DestinationPath -ArgumentList $InstallerArguments -Wait -NoNewWindow

        # Step 4: Retrieve the installed version
        Write-Host "Retrieving the installed version..."
        $InstalledVersion = Invoke-Expression $VersionCommand

        Write-Host "Installation completed successfully. Installed version: $InstalledVersion"
        return $InstalledVersion
    }
    catch {
        Write-Error "An error occurred: $_"
        return $null
    }
}

# Example usage:
# Replace the parameters below with actual values for your use case.
$DownloadUrl = "https://example.com/software-installer.exe"
$DestinationPath = "C:\Temp\software-installer.exe"
$InstallerArguments = "/quiet /norestart"
$VersionCommand = "software --version"  # Replace with the actual command to check the version

$InstalledVersion = Install-Software -DownloadUrl $DownloadUrl -DestinationPath $DestinationPath -InstallerArguments $InstallerArguments -VersionCommand $VersionCommand

if ($InstalledVersion) {
    Write-Host "Successfully installed version: $InstalledVersion"
} else {
    Write-Host "Installation failed."
}

Explanation:

  1. Parameters:
    • DownloadUrl: URL of the file to download.
    • DestinationPath: Path where the file will be saved.
    • InstallerArguments: Arguments for the installer (e.g., silent installation).
    • VersionCommand: Command to retrieve the installed version.
  2. Steps:
    • Downloads the file using Invoke-WebRequest.
    • Unblocks the file using Unblock-File.
    • Installs the software using Start-Process.
    • Retrieves the installed version using the provided command.
  3. Error Handling:
    • Catches any errors during the process and logs them.

This script is modular and can be adapted for various software installations.