Skip to content

Instantly share code, notes, and snippets.

@mikegoatly
Last active March 18, 2022 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikegoatly/2c8494c31b8522b73089896ca05ac42b to your computer and use it in GitHub Desktop.
Save mikegoatly/2c8494c31b8522b73089896ca05ac42b to your computer and use it in GitHub Desktop.
Useful PowerShell functions for working with local IIS sites and app pools
Import-Module IISAdministration
# Sets an environment variable for an IIS site in the applicationHost.config, rather than the web.config
# This allows for secrets to be stored securely, without any risk of them being added to source control.
function SetIISSiteEnvironmentVariable([string]$SiteName, [string]$Name, [string]$Value) {
$sm = Get-IISServerManager
$Env = $sm.GetApplicationHostConfiguration().GetSection("system.webServer/aspNetCore", $SiteName).
GetChildElement("environmentVariables").
GetCollection()
$CurrentVariable = $Env.GetCollection() | Where-Object {$_["name"] -eq $Name}
if ($CurrentVariable) {
Write-Host "Setting existing environment variable $Name on site $SiteName"
$CurrentVariable["value"] = $Value
}
else {
Write-Host "Creating new environment variable $Name on site $SiteName"
$NewVariable = $Env.CreateElement()
$NewVariable["name"] = $Name
$NewVariable["value"] = $Value
$Env.Add($NewVariable) | out-null
}
$sm.CommitChanges()
}
function DeleteExistingIISWebSite([string]$SiteName) {
$sm = Get-IISServerManager
$CurrentSite = $sm.Sites[$SiteName]
if ($CurrentSite) {
Write-Host "Deleting existing site"
$CurrentSite.Delete() | out-null
}
$CurrentPool = $sm.ApplicationPools[$SiteName]
if ($CurrentPool) {
Write-Host "Deleting existing app pool"
$CurrentPool.Delete() | out-null
}
$sm.CommitChanges()
}
function CreateIISWebSite([string]$HostName, [string]$SiteName, [string]$SitePath) {
Write-Host "$SiteName will use '$SitePath' as its physical path"
DeleteExistingIISWebSite $SiteName
$sm = Get-IISServerManager
Write-Host "Creating app pool $SiteName"
$NewPool = $sm.ApplicationPools.Add($SiteName)
$NewPool.ProcessModel.IdentityType = "LocalSystem"
$NewPool.ManagedRuntimeVersion = ""
Write-Host "Creating site $SiteName"
$NewSite = $sm.Sites.Add($SiteName, "http", ":80:$HostName", $SitePath)
$NewSite.Bindings.Add(":443:$HostName", "https") | Out-Null
Write-Host "Linking $SiteName app pool with site"
$NewSite.Applications["/"].ApplicationPoolName = $SiteName
$sm.CommitChanges()
}
function AssignSslCertificateToIISExpress([string]$HostName, [string]$CertThumbprint) {
$HostNamePort = $HostName + ":443"
$AppId = "{214124cd-d05b-4309-9af9-9caa44b2b74a}" # IIS Express
Write-Host "Assigning self-signed cert to IIS Express for $HostName"
netsh http delete sslcert hostnameport=$HostNamePort | out-null
netsh http add sslcert hostnameport=$HostNamePort certhash=$CertThumbprint appid=$AppId certstorename=my
}
function AssignSslCertificateToIIS([string]$HostName, [string]$CertThumbprint) {
$HostNamePort = $HostName + ":443"
$AppId = "{4dc3e181-e14b-4a21-b022-59fc669b0914}" # IIS
Write-Host "Assigning self-signed cert to IIS for $HostName"
netsh http delete sslcert hostnameport=$HostNamePort | out-null
netsh http add sslcert hostnameport=$HostNamePort certhash=$CertThumbprint appid=$AppId certstorename=my
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment