param( [Parameter(Mandatory=$true)] [String]$script, [Parameter(ValueFromRemainingArguments=$true)] [String[]]$scriptArguments ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' trap { Write-Host "ERROR: $_" ($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1' | Write-Host ($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1' | Write-Host Exit 1 } # wrap the choco command (to make sure this script aborts when it fails). function Start-Choco([string[]]$Arguments, [int[]]$SuccessExitCodes=@(0)) { $command, $commandArguments = $Arguments if ($command -eq 'install') { $Arguments = @($command, '--no-progress') + $commandArguments } for ($n = 0; $n -lt 10; ++$n) { if ($n) { # NB sometimes choco fails with "The package was not found with the source(s) listed." # but normally its just really a transient "network" error. Write-Host "Retrying choco install..." Start-Sleep -Seconds 3 } &C:\ProgramData\chocolatey\bin\choco.exe @Arguments if ($SuccessExitCodes -Contains $LASTEXITCODE) { return } } throw "$(@('choco')+$Arguments | ConvertTo-Json -Compress) failed with exit code $LASTEXITCODE" } function choco { Start-Choco $Args } Set-Location c:/vagrant/provision/windows $script = Resolve-Path $script Set-Location (Split-Path $script -Parent) Write-Host "Running $script..." . $script @scriptArguments