I have been using PowerShell recently to create scripted builds for virtual machines. This method provides some benefits over imaging.
- A single base image per OS can be used
- Applications which have issues with imaging can be easily installed such as SQL Server, BizTalk, Exchange, Etc.
- Scripted installs can be changed or updated easier than images
- Additional configuration changes such as registry settings and/or configuration files can be easily done
One challenge I faced with this process was automating the installation of applications which do not have unattended install capabilities.
A great tool I found for this was AutoIT. It provides a method to script the windows GUI and enables automating those installs. AutoIT consists of a COM object, scripting language, editor, compiler, and a cool window info tool. I will not get into the details of AutoIT here it has good documentation. I found PowerShell to be much stronger at the scripting part, but the COM object provides excellent window control functionality to PowerShell. The window info tool that comes with AutoIT is also very handy. Here is a simple example:
1 2 3 4 5 6 |
$AutoIt = New-Object -ComObject AutoItX3.Control $result = $AutoIt.Run("notepad.exe") $wintitle = "Untitled - Notepad" $AutoIt.WinWaitActive($wintitle) | Out-Null $AutoIt.Send("This is a test") |
To run the code above you will need to install AutoIT or just register the AutoItX.dll COM object. The code will launch notepad and type “This is a test”. While this is a pretty useless example it shows the basic purpose of AutoIT and how it is used from PowerShell.
The next piece I looked at was creating a function library to make it easy to use AutoIT from PowerShell.
Example: AutoIT PowerShell function library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# Installation path $InstallDir = 'SERVERNAMEInstalls$' # -- Copy and register AutoIt COM object ------------------------------------- Copy-item $InstallDirAutoItX3.dll $env:windirsystem32 & regsvr32 AutoItX3.dll /s # Wait for registration and create AutoIT instance Start-Sleep 2 $AutoIt = New-Object -ComObject AutoItX3.Control # --------------------------------------------------------------------------- ### <Function name='Test-ProcessRunning'> ### <Author>David Muegge</Author> ### <CreateDate>20080510</CreateDate> ### <Description> ### Test if process in running give name ### </Description> ### <Usage> ### $result = Test-ProcessRunning -processname <string> ### </Usage> ### <Notes> ### Helper function used to wait for process to complete ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Test-ProcessRunning([String]$processname){ $currentprocess = Get-Process | where {$_.Name -eq $processname} if($currentprocess){ return 0 }else{ return 1 } } # --------------------------------------------------------------------------- ### <Function name='Test-PIDRunning'> ### <Author>David Muegge</Author> ### <CreateDate>20080510</CreateDate> ### <Description> ### Test if process in running given process ID ### </Description> ### <Usage> ### $result = Test-PIDRunning ### </Usage> ### <Notes> ### Helper function used to wait for process to complete ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Test-PIDRunning([Int]$pidnumber){ $currentprocess = Get-Process | where {$_.ID -eq $pidnumber} if($currentprocess){ return 0 }else{ return 1 } } # --------------------------------------------------------------------------- ### <Function name='Start-WindowControl'> ### <Author>David Muegge</Author> ### <CreateDate>20080516</CreateDate> ### <Description> ### Wait for window presence and activate ### </Description> ### <Usage> ### Start-WindowControl -wintitle <window title> -wintext <window text> -keyarray <("v1","v2","v3")> ### </Usage> ### <Notes> ### The sleeps and the winactivate winwaitactive sequence was required for greater reliability. ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Start-WindowControl{ param([String]$wintitle, [String]$wintext=$null, [String[]]$keyarray=$null) # Window text was supplied if($wintext){ do{Start-Sleep -Milliseconds 100}until($AutoIt.WinExists($wintitle,$wintext)) $AutoIt.WinActivate($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 200 $AutoIt.WinWaitActive($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 200 } else { # Only window title was supplied do{Start-Sleep -Milliseconds 100}until($AutoIt.WinExists($wintitle)) $AutoIt.WinActivate($wintitle) | Out-Null Start-Sleep -Milliseconds 200 $AutoIt.WinWaitActive($wintitle) | Out-Null Start-Sleep -Milliseconds 200 } # If keys were supplied if($keyarray){ # Send Keys foreach($key in $keyarray){ $AutoIt.Send($key) } } } # --------------------------------------------------------------------------- ### <Function name='Start-WindowControlAB'> ### <Author>David Muegge</Author> ### <CreateDate>20081125</CreateDate> ### <Description> ### Wait for window presence and activate ### will accept one of two windows texts ### </Description> ### <Usage> ### Start-WindowControlAB -wintitle <window title> -wintext <window text> -altwintext <alternate window text> -keyarray <("v1","v2","v3")> ### </Usage> ### <Notes> ### The sleeps and the winactivate winwaitactive sequence was required for greater reliability. ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Start-WindowControlAB{ param([String]$wintitle, [String]$wintext=$null, [String]$altwintext=$null, [String[]]$keyarray=$null) # Wait for window to exist and activate $winfoundflag = $false do{ Start-Sleep -Milliseconds 200 if($AutoIt.WinExists($wintitle,$wintext)){$winfoundflag = $true} if($AutoIt.WinExists($wintitle,$altwintext)){$winfoundflag = $true;$wintext = $altwintext} }until($winfoundflag) $AutoIt.WinActivate($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 200 $AutoIt.WinWaitActive($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 500 # If keys were supplied if($keyarray){ # Send Keys foreach($key in $keyarray){ $AutoIt.Send($key) } } } # --------------------------------------------------------------------------- ### <Function name='Start-WindowControlQAB'> ### <Author>David Muegge</Author> ### <CreateDate>20081125</CreateDate> ### <Description> ### Wait for window presence and activate ### will accept one of two windows texts and Title texts ### </Description> ### <Usage> ### Start-WindowControlQAB -wintitle <window title> -altwintitle <window title> -wintext <window text> -altwintext <alternate window text> -keyarray <("v1","v2","v3")> ### </Usage> ### <Notes> ### The sleeps and the winactivate winwaitactive sequence was required for greater reliability. ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Start-WindowControlQAB{ param([String]$wintitle, [String]$altwintitle, [String]$wintext=$null, [String]$altwintext=$null, [String[]]$keyarray=$null) # Wait for window to exist and activate $winfoundflag = $false do{ Start-Sleep -Milliseconds 200 if($AutoIt.WinExists($wintitle,$wintext)){$winfoundflag = $true} if($AutoIt.WinExists($altwintitle,$altwintext)){$winfoundflag = $true;$wintext = $altwintext;$wintitle = $altwintitle} }until($winfoundflag) $AutoIt.WinActivate($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 200 $AutoIt.WinWaitActive($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 500 # If keys were supplied if($keyarray){ # Send Keys foreach($key in $keyarray){ $AutoIt.Send($key) } } } # --------------------------------------------------------------------------- ### <Function name='Start-WindowControlN'> ### <Author>David Muegge</Author> ### <CreateDate>20080516</CreateDate> ### <Description> ### Checks for window presence once a second for specified number of times ### Sends keys when found ### </Description> ### <Usage> ### Start-WindowControlN -check <integer> -wintitle <window title> -wintext <window text> -keyarray <@("'n")> ### </Usage> ### <Notes> ### The sleeps and the winactivate winwaitactive sequence was required for greater reliability. ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Start-WindowControlN{ param([Int32]$check, [String]$wintitle, [String]$wintext=$null, [String[]]$keyarray=$null) # Check if window exists once a second for N times for($i = 0; $i -lt $check; $i++){Start-Sleep -Seconds 1 if($AutoIt.WinExists($wintitle,$wintext)){ $AutoIt.WinActivate($wintitle,$wintext) Start-Sleep -Milliseconds 100 $AutoIt.WinWaitActive($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 200 # If keys were supplied if($keyarray){ # Send Keys foreach($key in $keyarray){ $AutoIt.Send($key) } } break } } } # --------------------------------------------------------------------------- ### <Function name='Start-WindowControlS'> ### <Author>David Muegge</Author> ### <CreateDate>20080516</CreateDate> ### <Description> ### Wait for window presence and activate ### </Description> ### <Usage> ### Start-WindowControlS -wintitle <window title> -wintext <window text> -keyarray <("v1","v2","v3")> ### </Usage> ### <Notes> ### The sleeps and the winactivate winwaitactive sequence was required for greater reliability. ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Start-WindowControlS{ param([String]$wintitle, [String]$wintext=$null, [String[]]$keyarray=$null) # Window text was supplied if($wintext){ do{ Start-Sleep -Milliseconds 200 }until($AutoIt.WinExists($wintitle,$wintext)) do{ $AutoIt.WinActivate($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 200 $AutoIt.WinWaitActive($wintitle,$wintext) | Out-Null Start-Sleep -Milliseconds 500 # If keys were supplied if($keyarray){ # Send Keys foreach($key in $keyarray){ $AutoIt.Send($key) } } }until(-not $AutoIt.WinExists($wintitle,$wintext)) } else { # Only window title was supplied do{ Start-Sleep -Milliseconds 200 }until($AutoIt.WinExists($wintitle)) do{ $AutoIt.WinActivate($wintitle) | Out-Null Start-Sleep -Milliseconds 200 $AutoIt.WinWaitActive($wintitle) | Out-Null Start-Sleep -Milliseconds 500 # If keys were supplied if($keyarray){ # Send Keys foreach($key in $keyarray){ $AutoIt.Send($key) } } }until(-not $AutoIt.WinExists($wintitle)) } } # --------------------------------------------------------------------------- ### <Function name='Write-Log'> ### <Author>David Muegge</Author> ### <CreateDate>20080517</CreateDate> ### <Description> ### Writes output to host with timestamp ### </Description> ### <Usage> ### Write-Log <logentry> ### </Usage> ### <Notes> ### Intended for us with Transcript functionality ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Write-Log{ param([String]$logentry) $logtime = Get-Date -Format T Write-Host "$logtime - $logentry" } |
Then using the functions from the library above I created application installation functions for various packages.
Example: PowerShell Application Installation Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# --------------------------------------------------------------------------- ### <Function name='Install-TortoiseSVN152'> ### <Author>David Muegge</Author> ### <CreateDate>20080826</CreateDate> ### <Description> ### Installs Tortoise Subversion Client 1.5.2.13595-win32-svn-1.5.1 ### </Description> ### <Usage> ### $result = Install-TortoiseSVN152 ### </Usage> ### <Notes> ### Requires reboot when complete ### Will not always prompt for reboot and will restart automatically ### Always put at end of install step ### </Notes> ### </Function> # --------------------------------------------------------------------------- function Install-TortoiseSVN152{ param([switch]$restart) $result = $AutoIt.Run("msiexec /i $InstallDirTortoiseSVN-1.5.2.13595-win32-svn-1.5.1.msi") Write-Log "Tortoise Subversion Client 1.5.2 Install Launched PID:$result" Start-WindowControl -wintitle "TortoiseSVN 1.5.2.13595 (32 bit) Setup" -wintext "Welcome to the TortoiseSVN 1.5.2.13595 (32 bit) Setup Wizard" -keyarray @("!n") Start-WindowControl -wintitle "TortoiseSVN 1.5.2.13595 (32 bit) License Agreement" -wintext "I &accept the terms in the License Agreement" -keyarray @("!a","!n") Start-WindowControl -wintitle "TortoiseSVN 1.5.2.13595 (32 bit) Setup" -wintext "Custom Setup" -keyarray @("!n") Start-WindowControl -wintitle "TortoiseSVN 1.5.2.13595 (32 bit) Setup" -wintext "Ready to Install" -keyarray @("!i") Start-WindowControl -wintitle "TortoiseSVN 1.5.2.13595 (32 bit) Setup" -wintext "Click the Finish button to exit the Setup Wizard." -keyarray @("!f") Start-WindowControl -wintitle "Changelog.txt - Notepad" -keyarray @("!{F4}") Start-WindowControl -wintitle "Installer Information" -keyarray @("&Yes") # Check if reboot window exists once a second for five seconds Start-WindowControlN -check 5-wintitle "Installer Information" -wintext "&Yes" -keyarray @("!n") Write-Log "Tortoise Subversion Client 1.5.2 Install Completed" # Restart if selected if($restart){ Write-Host "$logtime - Restarting..." Shutdown /r /t 1 /d P:2:4 } } |
Using the method above I created a library of PowerShell installation functions to be called from automated build scripts.
This seems to be working pretty well so far and it allows automated builds to be scripted quickly. PowerShell and AutoIT work well together, the AutoIT COM object provides excellent GUI control and PowerShell provides strong scripting and debugging features.
Best Regards,
Dave