Script to install IIS and Upgrade IIS

Product: PowerShell Universal
Version: 4.3.4

Anyone that is interested in a basic script to install and upgrade iis, you might find a need for it.

Install

$here = "E:\src"
$here = $PSScriptRoot

$appName = "psuBlank"
$zipFilename = "Universal.win7-x64.4.2.19.zip";

$iisRoot = "$here\psu"
$zip = "$here\$zipFilename"

$appPath = Join-Path -Path $iisRoot -ChildPath $appName # C:\inetpub\wwwroot\myappName
$dataPath = $appPath + "_data"
mkdir $dataPath -ErrorAction SilentlyContinue
mkdir $appPath -ErrorAction SilentlyContinue
cd $here

Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory("$zip", $appPath)
Get-ChildItem $appPath -Recurse | Unblock-File

$appSettings = "$iisRoot\appsettings.$appName.json"
Copy-Item -Path "$appPath\appsettings.json" -Destination $appSettings 

# Save app settings to new location
$jsonData = Get-Content $appSettings | ConvertFrom-Json
$jsonData.Kestrel.BasePath = "/$appName"
$jsonData.Api.Url = "/$appName"
$jsonData.Data.RepositoryPath = Join-Path -Path $dataPath -ChildPath "UniversalAutomation\Repository"
$jsonData.data.ConnectionString = "filename=$dataPath\UniversalAutomation\database.db;upgrade=true"
$jsonData.UniversalDashboard.AssetsFolder = Join-Path $dataPath -ChildPath "PowerShellUniversal\Dashboard"
$jsonData.SystemLogPath = "$dataPath\UniversalAutomation\systemLog.txt"
$jsonData | ConvertTo-Json -Depth 30 | Set-Content $appSettings

$webconfigFile = Join-Path -Path $appPath -ChildPath 'web.config'
$webconfig = (Get-Content $webconfigFile) -as [xml]
$webconfig.configuration.'system.webServer'.aspNetCore.arguments = "--appsettings $appSettings"
$webconfig.configuration.'system.webServer'.aspNetCore.hostingModel = "OutOfProcess"
$webconfig.Save("$webconfigFile")

Upgrade

function Download-PSUzip
{
    [CmdletBinding()]
    Param
    (
        $version, 
        $here,
        $winVer = 7
    )

    Begin
    {
    }
    Process
    {
        $zipFilename = "Universal.win$winver-x64.$version.zip";
        $zip = "$here\$zipFilename"
        $unzipFolder = Join-Path $here -ChildPath ($zipFilename -replace ".zip", "")

        if(Test-Path -Path $unzipFolder){
            return $unzipFolder
        }
        $downloadURL = "https://imsreleases.blob.core.windows.net/universal/production/$version/$zipFilename"
        Write-Host $downloadURL
        Invoke-WebRequest -Uri $downloadURL -OutFile $zipFilename
        Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory("$zip", $unzipFolder)
        Get-ChildItem $unzipFolder -Recurse | Unblock-File
        $unzipFolder
    }
    End
    {
    }
}

<#
.Synopsis
   Short description
.DESCRIPTION
   Long description
.EXAMPLE
   Example of how to use this cmdlet
.EXAMPLE
   Another example of how to use this cmdlet
#>
function Upgrade-Psu
{
    [CmdletBinding()]
    Param
    (
        $unzipFolder,
        $appName,
        $here
    )

    Begin
    {
    }
    Process
    {
        $iisRoot = "$here"
        $appFolder = Join-Path $iisRoot -ChildPath $appName
        if(Test-Path -Path $appFolder){
            Stop-WebAppPool -Name $appName -Verbose:$VerbosePreference
            start-sleep -Seconds 15
            Write-Host "$appName - removing old app"
            dir $appFolder -Exclude web.config, appsettings.json -Recurse -Force | Remove-Item -Force -Recurse -Verbose:$VerbosePreference
            Write-Host "$appName - Done removing old app"
            Write-Host "$appName - Moving new app in place"
            Copy-Item -Path $unzipFolder\* -Destination $appFolder -Exclude web.config, appsettings.json -Recurse -Force -Verbose:$VerbosePreference
            Write-Host "$appName - done Moving new app into place"
            start-WebAppPool -Name $appName -Verbose:$VerbosePreference
        }else{
            Write-Error "Unable to locate $appFolder to upgrade this psu instance"
        }
    }
    End
    {
    }
}


$here = $PSScriptRoot
cd $here
$unzipFolder = Download-PSUzip -version '5.0.0' -here $here -winVer ""
# https://imsreleases.blob.core.windows.net/universal/production/5.0.0-rc1/Universal.win7-x64.5.0.0-rc1.zip
# https://imsreleases.blob.core.windows.net/universal/production/5.0.0-rc1/Universal.win-x64.5.0.0-rc1.zip
#$unzipFolder = Download-PSUzip -version '5.0.0-rc1' -winVer "" -here $here

$iisFolder = Join-Path -Path $here -ChildPath psu

if($false){
    Upgrade-Psu -unzipFolder $unzipFolder -here $iisFolder -appName "psuSandbox" 

    Upgrade-Psu -unzipFolder $unzipFolder -here $iisFolder -appName "psuBlank" 
}

1 Like