Skip to content

Instantly share code, notes, and snippets.

@jamesperrin
Created July 22, 2021 15:39
Show Gist options
  • Save jamesperrin/a04edb32745da95098e4473aecbc98b6 to your computer and use it in GitHub Desktop.
Save jamesperrin/a04edb32745da95098e4473aecbc98b6 to your computer and use it in GitHub Desktop.
<#
::##################################################################################
::#
::# Purpose: To configure HTTP Strict Transport Security (HSTS) Window Server versions 2019 with IIS 10.
::# OS: Window Server versions 2019 with IIS 10
::# SEE: https://docs.microsoft.com/en-us/iis/get-started/whats-new-in-iis-10-version-1709/iis-10-version-1709-hsts#iis-100-version-1709-native-hsts-support
::#
::# James Perrin, MIT License, https://www.countrydawgg.com, | @_jamesperrin
::#
::##################################################################################
#>
Import-Module IISAdministration
Reset-IISServerManager -Confirm:$false
Start-IISCommitDelay
$sitesCollection = Get-IISConfigSection -SectionPath "system.applicationHost/sites" | Get-IISConfigCollection
$siteElement = Get-IISConfigCollectionElement -ConfigCollection $sitesCollection -ConfigAttribute @{"name"="Default Web Site"}
$hstsElement = Get-IISConfigElement -ConfigElement $siteElement -ChildElementName "hsts"
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "enabled" -AttributeValue $true
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "max-age" -AttributeValue 31536000
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "redirectHttpToHttps" -AttributeValue $true
Stop-IISCommitDelay
Remove-Module IISAdministration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment