Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MarcelMeurer/1d5ea8177ac6d82b0f9f7e08dac40621 to your computer and use it in GitHub Desktop.
Save MarcelMeurer/1d5ea8177ac6d82b0f9f7e08dac40621 to your computer and use it in GitHub Desktop.
Hydra-Script: Install applications from a file share (example)
# Script framework to install applications from a folder.
# Name of your installation
$name="Remote Desktop Client"
# Path of the application installation folder, including the executable of the installer (or ps1. or MSI)
$installer="\\wvduserdata.file.core.windows.net\apps-2-install\MSRDC-AVD-Client\Deploy-Application.exe"
# Parameter needed for an application (only for exe or ps1 files)
$parameters="Install"
LogWriter ("Installing application $name from $installer")
if ($installer -like "\\*") {
# UNC path
$fileServerHost=$installer.Substring(0,$installer.Replace("\\","##").IndexOf("\"))
$fileServerShare=$installer.Replace($fileServerHost,"").Substring(1,$installer.Replace("$fileServerHost\","").IndexOf("\"))
@(Get-PSDrive -Name AppInstall -ErrorAction SilentlyContinue) | Remove-PSDrive
# Authenticate to the share
if ($global:Hydra_ServiceAccount_PSC -eq $null) {
LogWriter ("Authentication to $($fileServerHost)\$($fileServerShare) with the computer account")
New-PSDrive -Name AppInstall -PSProvider FileSystem -Root "$($fileServerHost)\$($fileServerShare)"
} else {
LogWriter ("Authentication to $($fileServerHost)\$($fileServerShare) with the service account")
New-PSDrive -Name AppInstall -PSProvider FileSystem -Root "$($fileServerHost)\$($fileServerShare)" -Credential $global:Hydra_ServiceAccount_PSC
}
}
LogWriter ("Attaching share to temp folder")
$guid=[guid]::NewGuid().Guid
$workingDirectory="$($env:temp)\$($guid)"
$remoteDirectory=(Get-ChildItem -Path $installer).PSParentPath.Replace("Microsoft.PowerShell.Core\FileSystem::","")
$workingFile=$installer.Replace($remoteDirectory,$workingDirectory)
cmd /c mklink /d "$workingDirectory" "$remoteDirectory"
If ($installer -like "*.ps1") {
LogWriter ("Executing a powershell script")
$rval=Start-Process -wait -PassThru -WorkingDirectory $workingDirectory -FilePath PowerShell -ArgumentList "-ExecutionPolicy Bypass -File `"$($workingFile)`" $($parameters)" -RedirectStandardOutput "$($LogDir)\PS1-Installation-$($name).Out.txt" -RedirectStandardError "$($LogDir)\PS1-Installation-$($name).Warning.txt"
} elseif ($installer -like "*.cmd") {
LogWriter ("Executing a bat script")
$rval=Start-Process -wait -PassThru -WorkingDirectory $workingDirectory -FilePath cmd.exe -ArgumentList "/c `"$($workingFile)`" $($parameters)" -RedirectStandardOutput "$($LogDir)\BAT-Installation-$($name).Out.txt" -RedirectStandardError "$($LogDir)\BAT-Installation-$($name).Warning.txt"
} elseif ($installer -like "*.bat") {
LogWriter ("Executing a bat script")
$rval=Start-Process -wait -PassThru -WorkingDirectory $workingDirectory -FilePath cmd.exe -ArgumentList "/c `"$($workingFile)`" $($parameters)" -RedirectStandardOutput "$($LogDir)\CMD-Installation-$($name).Out.txt" -RedirectStandardError "$($LogDir)\CMD-Installation-$($name).Warning.txt"
} elseif ($installer -like "*.msi") {
LogWriter ("Executing a MSI")
$rval=Start-Process -wait -PassThru -WorkingDirectory $workingDirectory -FilePath msiexec.exe -ArgumentList "/i `"$workingFile`" /q /L*V `"$($LogDir)\MSI-Installation-$($name).log`"" -RedirectStandardOutput "$($LogDir)\MSI-Installation-$($name).Out.txt" -RedirectStandardError "$($LogDir)\MSI-Installation-$($name).Warning.txt"
} elseif ($installer -like "*.exe") {
LogWriter ("Executing an EXE")
$rval=Start-Process -wait -PassThru -WorkingDirectory $workingDirectory -FilePath $workingFile -ArgumentList "$($parameters)" -RedirectStandardOutput "$($LogDir)\EXE-Installation-$($name).Out.txt" -RedirectStandardError "$($LogDir)\EXE-Installation-$($name).Warning.txt"
} else {
cmd /c "rmdir `"$workingDirectory`""
LogWriter ("Path doesn't point to a MSI, BAT, CMD, PS1 or EXE file.")
throw "Path doesn't point to a MSI, BAT, CMD, PS1 or EXE file."
}
cmd /c "rmdir `"$workingDirectory`""
$okCodes=(0,1707,3010,1641,618,1337,5)
if ($rval.ExitCode -in $okCodes) {
Outputwriter ("Installation of $name ready with exit code: $($rval.ExitCode.tostring())")
} else {
Outputwriter ("Installation of $name failed with exit code: $($rval.ExitCode.tostring())")
throw "Installation of $name failed with exit code: $($rval.ExitCode.tostring())"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment