Skip to content

Instantly share code, notes, and snippets.

@anderssonjohan
Created January 19, 2013 21:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save anderssonjohan/4575105 to your computer and use it in GitHub Desktop.
Save anderssonjohan/4575105 to your computer and use it in GitHub Desktop.
A basic PowerShell script used to push IIS 7 Application Request Routing (tl;dr; reverse proxy features for UrlRewrite) to several servers. Assumptions: - cURL is in the path on the source machine - servers are specified as objects on the pipeline with properties telling the unc path and local path to a writable network share on the server - Win…
param(
[parameter(mandatory=$true, valuefrompipeline=$true)]
$TargetHost,
[switch] $force
)
begin {
$packages = @( `
@{ Name = "rewrite.msi"; Url = "http://download.microsoft.com/download/6/7/D/67D80164-7DD0-48AF-86E3-DE7A182D6815/rewrite_2.0_rtw_x64.msi" }, `
@{ Name = "webpi.msi"; Url = "http://download.microsoft.com/download/B/0/0/B00FEF21-79DE-48B0-8731-F9CFE70CE613/WebPlatformInstaller_3_10_amd64_en-US.msi" }, `
@{ Name = "webfarm.msi"; Url = "http://download.microsoft.com/download/3/4/1/3415F3F9-5698-44FE-A072-D4AF09728390/webfarm_amd64_en-US.msi" }, `
@{ Name = "arr.msi"; Url = "http://download.microsoft.com/download/A/A/E/AAE77C2B-ED2D-4EE1-9AF7-D29E89EA623D/requestRouter_amd64_en-US.msi" }, `
@{ Name = "extcache.msi"; Url = "http://download.microsoft.com/download/3/4/1/3415F3F9-5698-44FE-A072-D4AF09728390/ExternalDiskCache_amd64_en-US.msi" } `
)
Push-Location $env:TEMP
function download( $url, $filename ) {
if(!(Test-Path $filename) -or $force) {
curl -o $filename $url
if( $LASTEXITCODE -ne 0 -or !(Test-Path $filename) ) {
Pop-Location
throw "Failed to download $url to $filename"
exit 1
}
}
}
Write-Host "Downloading MSI packages..."
$packages | %{
Write-Host ("Downloading MSI package: {0}" -f $_.Name)
download $_.Url $_.Name
}
}
process {
$remotePackageDir = join-path $targetHost.ScriptSharePath "\"
$packageNames = $packages | %{ $_.Name }
Write-Host "Copying packages to server: $packageNames"
$packageNames | cp -Destination $remotePackageDir
Write-Host "Done. Performing installation..."
$Session = $targetHost.Connect()
Invoke-Command -Session $Session -ArgumentList $packageNames -ScriptBlock {
if( Get-Service was ) {
Write-Host "Stopping IIS and WAS..."
Stop-Service was -Force
}
Push-Location $targetHost.ScriptShareLocalPath
$args | %{
Write-Host "Installing MSI package: $_"
$exitCode = (Start-Process -FilePath "msiexec" -ArgumentList "/q /i $_ /L*v install.log" -PassThru -Wait).ExitCode
if( $exitCode -ne 0 ) {
Pop-Location
throw "MSIEXEC exited $exitCode Failed to install $_"
exit 1
}
rm $_
}
Pop-Location
if( Get-Service was ) {
Start-Service was,w3svc -Verbose
}
}
if( $? ) {
Write-Host "ARR installed!"
} else {
cp $remotePackageDir\install.log -Verbose
gc install.log | ?{ $_ -match "error|fail|requisite" }
}
}
end {
Pop-Location
}
@haf
Copy link

haf commented Jan 19, 2013

Can you first do

    throw "Failed to download $url to $filename"

and then:

exit 1

?

Interesting script. Will study it further during upcoming days.

Btw, are you up for a fika someday next week?

@KennethanCeyer
Copy link

KennethanCeyer commented Sep 13, 2016

This code working on AWS Beasntalk instance. (IIS 8.5, Windows 2012 RC2)

begin {
    $TargetPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(.\)
    iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
    choco install curl

    $packages = @( `
        @{ Name = "webfarm.msi"; Url = "https://download.microsoft.com/download/F/E/2/FE2E2E07-22B5-4875-9A36-8B778D157F91/WebFarm2_x64.msi" }, `
        @{ Name = "rewrite.msi"; Url = "https://download.microsoft.com/download/6/7/D/67D80164-7DD0-48AF-86E3-DE7A182D6815/rewrite_2.0_rtw_x64.msi" }, `
        @{ Name = "arr.msi"; Url = "https://download.microsoft.com/download/E/9/8/E9849D6A-020E-47E4-9FD0-A023E99B54EB/requestRouter_amd64.msi" }
    )

    Push-Location $env:TEMP
    function download( $url, $filename ) {
        if(!(Test-Path $filename) -or $force) {
            curl -OutFile $filename $url
            if( $LASTEXITCODE -ne 0 -or !(Test-Path $filename) ) {
                Pop-Location
                throw "Failed to download $url to $filename"
                exit 1
            }
        }
    }

    Write-Host "Downloading MSI packages..."
    $packages | %{ 
        Write-Host ("Downloading MSI package: {0}" -f $_.Name)
        download $_.Url $_.Name
    }
}
process {
    $remotePackageDir = join-path $TargetPath "\"
    $packageNames = $packages | %{ $_.Name } 
    Write-Host "Copying packages to server: $packageNames"
    $packageNames | cp -Destination $remotePackageDir
    Write-Host "Done. Performing installation..."
    #$Session = $targetHost.Connect()
    Invoke-Command -ArgumentList $packageNames -ScriptBlock {
        if( Get-Service was ) {
            Write-Host "Stopping IIS and WAS..."
            Stop-Service was -Force
        }

        Push-Location $targetHost.ScriptShareLocalPath
        $args | %{
            Write-Host "Installing MSI package: $_"
            $exitCode = (Start-Process -FilePath "msiexec" -ArgumentList "/q /i $_ /L*v install.log" -PassThru -Wait).ExitCode
            #if( $exitCode -ne 0 ) {
            #   Pop-Location
            #   throw "MSIEXEC exited $exitCode Failed to install $_"
            #   exit 1
            #}
            rm $_
        }
        Pop-Location

        if( Get-Service was ) {
            Start-Service was,w3svc -Verbose
        }
    }
    if( $? ) {
        Write-Host "ARR installed!"
    } else {
        cp $remotePackageDir\install.log -Verbose
        gc install.log | ?{ $_ -match "error|fail|requisite" }
    }
}
end {
    Pop-Location
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment