I am currently testing Windows 7 and therefore I am deploying various builds quite frequently. This is nice because you have a chance to see the product evolving and at the same time you have a chance to send feedback to the product team. On the other hand installing OS frequently means to install all the other applications very often. I have created various PowerShell scripts to simplify the deployment process. Today I will try to describe my install-folder script.
The basic idea of the script is this. I want to put all my installations into a folder and then run the script against this folder. The script will enumerate over all the files in that folder and “run the installation” for each of them. The problem here is that some installations are delivered as MSI files, some are EXE files (not build using Windows Installer) and some are ISO files (those downloaded from MSDN usually).
MSI File Installation
To install an MSI file you run msiexec with /i msiFileName. If you want to run the installation automatically then add also /passive. If you want to prevent Windows Installer from automatically restarting your machine then use /norestart.
Note: When you need to troubleshoot MSI installation, it might be a good idea to turn on verbose logging in Windows Installer. To turn the logging on use /l*v logFilePath parameter. More information about this topic can be found at:
EXE File Installation
This one is tricky. If it is EXE created using Windows Installer then you might be able to use some of the Windows Installer parameters. On the other hand many other installers are available (like InnoSetup, Nullsoft Install System, etc.) and the parameters for these are different. That’s why when installing EXE I usually just invoke the EXE file.
ISO File Installation
ISO file installation is the most difficult because in the OS there is no direct support for these files. There are various utilities available for this purpose (SlySoft Virtual CloneDrive, Daemon Tools, Microsoft Virtual CD-ROM, etc.). I have decided to use SlySoft solution because I have had a really good experience with their other tools like AnyDVD (this is an awesome tool if you are dealing with DVDs from different regions for example). After you install the tool you can mount an ISO image by calling VCDMount.exe isoFilePath. To unmount the current ISO image execute VCDMount.exe /u.
InstallFile function
So to install the files from a folder we need to enumerate over all files in that folder and for each of these files execute InstallFile function:
function InstallFile($file)
{
if ($file.EndsWith(".msi"))
{
execute-command -wait -command msiexec -parameters "/i $file /passive /norestart"
}
elseif ($file.EndsWith(".exe"))
{
execute-command -wait -command "$file"
}
elseif ($file.EndsWith(".iso"))
{
execute-command -command "$programFiles32\Elaborate Bytes\VirtualCloneDrive\VCDMount.exe" -parameters "/u"
execute-command -command "$programFiles32\Elaborate Bytes\VirtualCloneDrive\VCDMount.exe" -parameters "$file"
Pause "`tPress any key when finished with this installation."
execute-command -command "$programFiles32\Elaborate Bytes\VirtualCloneDrive\VCDMount.exe" -parameters "/u"
}
}
This function references some of my other scripts/functions. First of all it uses execute-command script. The core of that script is the following:
param([string] $command = $(throw "Missing: command parameter"), [string[]] $parameters, [switch] $wait, [switch] $cmd)
#Main
if ($cmd.IsPresent)
{
cmd /C $command $parameters
}
else
{
$process = [Diagnostics.Process]::Start($command, $parameters);
if ($wait.IsPresent)
{
$process.WaitForExit();
}
}
Save this script as execute-command.ps1.
The only extra thing we have in our InstallFile folder is reference to $programFiles32 folder. This is initialized in my profile script (which I will describe in one of the latter posts). For now I will just define the variables:
#Define ProgramFiles* variables
$is64 = $(if([IntPtr]::Size -eq 8) { $true } else { $false })
if ($is64)
{
$programFiles32 = $(get-item "env:ProgramFiles(x86)").Value
$programFiles64 = $env:ProgramFiles
}
else
{
$programFiles32 = $env:ProgramFiles
$programFiles64 = $env:ProgramFiles
}
InstallFolder function
Now we have all we need to implement our main InstallFolder function:
function InstallFolder($folder)
{
$files = get-childitem $folder -recurse -force
foreach($file in $files)
{
if ($file -eq $null)
{
continue
}
$answer = Ask "Do you want to install $($file.FullName)? (y or n)?" "y"
if ($answer -ne "y") { continue }
set-color "yellow"
write-host "`tInstalling $file..."
set-color
InstallFile $($file.FullName)
}
}
In this function I am using two additional functions. Function set-color (is defined in profile script) and Ask function.
function Ask($text, $default)
{
$answer = $(read-host "$text [default: $default]")
if ($answer -eq "") { $answer = $default }
return $answer
}
function Set-Color([string] $color)
{
if ($color -eq "")
{
# $myDefaultColor initialized in profile script to [Console]::ForeGroundColor
$color = $myDefaultColor
}
$host.UI.RawUI.ForeGroundColor = $color
}
Final Install-Folder script code
With all these we can now create the install-folder.ps1 script:
param([string] $path = $(throw "Missing: path parameter"), )
#Internals
function Ask($text, $default)
{
$answer = $(read-host "$text [default: $default]")
if ($answer -eq "") { $answer = $default }
return $answer
}
function Set-Color([string] $color)
{
if ($color -eq "")
{
# $myDefaultColor initialized in profile script to [Console]::ForeGroundColor
$color = $myDefaultColor
}
$host.UI.RawUI.ForeGroundColor = $color
}
function InstallFolder($folder)
{
$files = get-childitem $folder -recurse -force
foreach($file in $files)
{
if ($file -eq $null)
{
continue
}
$answer = Ask "Do you want to install $($file.FullName)? (y or n)?" "y"
if ($answer -ne "y") { continue }
set-color "yellow"
write-host "`tInstalling $file..."
set-color
InstallFile $($file.FullName)
}
}
function InstallFile($file)
{
if ($file.EndsWith(".msi"))
{
execute-command -wait -command msiexec -parameters "/i $file /passive /norestart"
}
elseif ($file.EndsWith(".exe"))
{
execute-command -wait -command "$file"
}
elseif ($file.EndsWith(".iso"))
{
execute-command -command "$programFiles32\Elaborate Bytes\VirtualCloneDrive\VCDMount.exe" -parameters "/u"
execute-command -command "$programFiles32\Elaborate Bytes\VirtualCloneDrive\VCDMount.exe" -parameters "$file"
Pause "`tPress any key when finished with this installation."
execute-command -command "$programFiles32\Elaborate Bytes\VirtualCloneDrive\VCDMount.exe" -parameters "/u"
}
}
#Main
#Define ProgramFiles* variables
$is64 = $(if([IntPtr]::Size -eq 8) { $true } else { $false })
if ($is64)
{
$programFiles32 = $(get-item "env:ProgramFiles(x86)").Value
$programFiles64 = $env:ProgramFiles
}
else
{
$programFiles32 = $env:ProgramFiles
$programFiles64 = $env:ProgramFiles
}
write-host "Installing $path..."
InstallFolder $path
This is just one of my scripts that I use daily. I will describe more of them in later posts.
Attachments:
PowerShell
powershell