Skip to content

Instantly share code, notes, and snippets.

@rileyz
Last active June 26, 2023 21:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileyz/bf3f599247c4b13a96d1f4d60ef53d70 to your computer and use it in GitHub Desktop.
Save rileyz/bf3f599247c4b13a96d1f4d60ef53d70 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Reusable script to stage/unzip large deployment packages, then launch the
script to trigger the silent install.
.DESCRIPTION
Intended Use
This script was produced to stage a large deployment package with many files,
the many files caused the ESD tool to error during file transfer to the
client.
This script will create a temporary directory in a secure location, create a
directory junction to avoid long file path issues (if enabled), then launch
the script installer via the junction. Once the install is complete, house
cleaning will be preformed.
Please note your script installer MUST return success/error codes.
Requires .Net 4.5 for the system.io.compression.filesystem class.
Error Codes
This script will return the exit code from script installer unless it traps
housekeeping issues.
Error -10
The version of .Net must be 4.5 or greater for use of the
system.io.compression.filesystem method.
Error -11
The directory path already exsists, will not use exisiting directory.
Code Snippet Credits
* https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#ps_a
Version History
1.2 31/01/2017
Fixed error with .Net 4.5 detection.
Fixed script formatting.
1.1 13/01/2017
Updated to launch .cmd, .bat and .vbs.
Updated PowerShell script name to reflect input of other script types.
Updated to enable/disable directory junction option for long file path issues.
Added .Net 4.5 detection.
Published as Gist finally.
1.0 17/05/2016
Initial script written but not published.
Copyright & Intellectual Property
Feel to copy, modify and redistribute, but please pay credit where it is due.
Feed back is welcome, please contact me on LinkedIn.
.LINK
Author:.......http://www.linkedin.com/in/rileylim
Source Code:..https://gist.github.com/rileyz/bf3f599247c4b13a96d1f4d60ef53d70
Article:......
.EXAMPLE
Launch the script with the required parameters, this will execute from the Windows\Temp directory.
BatchFile.bat
PowerShell -ExecutionPolicy ByPass -File UnzipAndExecuteScript.ps1 -ZipFile CompressedInstallerSourceWithScripts.zip -ScriptLauncher SilentInstall.bat
BatchFile.cmd
PowerShell -ExecutionPolicy ByPass -File UnzipAndExecuteScript.ps1 -ZipFile CompressedInstallerSourceWithScripts.zip -ScriptLauncher SilentInstall.cmd
VisualBasic.vbs
PowerShell -ExecutionPolicy ByPass -File UnzipAndExecuteScript.ps1 -ZipFile CompressedInstallerSourceWithScripts.zip -ScriptLauncher SilentInstall.vbs
.EXAMPLE
Launch the script with 'UseDirectoryJunction' switch to resolve long file path issues, this will execute from SystemVolume:\<PartGUID> directory.
BatchFile.bat
PowerShell -ExecutionPolicy ByPass -File UnzipAndExecuteScript.ps1 -ZipFile CompressedInstallerSourceWithScripts.zip -ScriptLauncher SilentInstall.bat -UseDirectoryJunction
BatchFile.cmd
PowerShell -ExecutionPolicy ByPass -File UnzipAndExecuteScript.ps1 -ZipFile CompressedInstallerSourceWithScripts.zip -ScriptLauncher SilentInstall.cmd -UseDirectoryJunction
VisualBasic.vbs
PowerShell -ExecutionPolicy ByPass -File UnzipAndExecuteScript.ps1 -ZipFile CompressedInstallerSourceWithScripts.zip -ScriptLauncher SilentInstall.vbs -UseDirectoryJunction
#>
Param ([Parameter(Mandatory=$True)][String]$ZipFile,
[Parameter(Mandatory=$True)][String]$ScriptLauncher,
[Parameter(Mandatory=$False)][Switch]$UseDirectoryJunction)
# Setting up housekeeping #########################################################################
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$FreshGUID = [System.GUID]::NewGuid().ToString().ToUpper()
#<<< End of Setting up housekeeping >>>
# Start of script work ############################################################################
$GUIDArray = $FreshGUID.split('-')
$WindowsTempFolder = "$env:systemroot\Temp\" + $GUIDArray[2]
#Testing for .Net 4.5 or greater.
#https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#ps_a
If ((Get-ChildItem "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemProperty -Name Release).Release -le 378389)
{Exit -10}
#Creating staging directory.
If (Test-Path $WindowsTempFolder)
{Exit -11}
Else{New-Item -ItemType Directory -Force -Path $WindowsTempFolder}
If ($UseDirectoryJunction -eq $True)
{#Setting up for install via directory junction.
$InstallPoint = "$env:systemdrive\" + $GUIDArray[1]
If (Test-Path $InstallPoint)
{Exit -11}
Else{&CMD /C MKLINK /J $InstallPoint $WindowsTempFolder}
$ZipPath = Resolve-Path -Path $ZipFile
Add-Type -Assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($ZipPath, $InstallPoint)}
Else{#Setting up for install via Windows Temp directory.
$InstallPoint = $WindowsTempFolder
$ZipPath = Resolve-Path -Path $ZipFile
Add-Type -Assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($ZipPath, $InstallPoint)}
#Running installer.
Set-Location $InstallPoint
&CMD /C $ScriptLauncher
$ScriptExitCode = $LASTEXITCODE
Set-Location $ScriptPath
#Cleaning up directory junction.
If ($UseDirectoryJunction -eq $True)
{$RemoveJunction = Get-Item $InstallPoint
$RemoveJunction.Delete()}
#Cleaning up staging directory.
Remove-Item $WindowsTempFolder -Recurse
Exit $ScriptExitCode
#<<< End of script work >>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment