Skip to content

Instantly share code, notes, and snippets.

@shirhatti
Created December 7, 2018 02:13
Show Gist options
  • Save shirhatti/f68b0f13639150b813b7c9e25c16654e to your computer and use it in GitHub Desktop.
Save shirhatti/f68b0f13639150b813b7c9e25c16654e to your computer and use it in GitHub Desktop.
Powershell script to work around shared config block in ANCM
Import-Module IISAdministration
# Stop WAS/W3SVC
Stop-Service -Name WAS -Force
# Disable shared config
$sm = Get-IISServerManager
$sm.GetRedirectionConfiguration().GetSection("configurationRedirection").Attributes["enabled"].Value = $false
$sm.CommitChanges()
# Initialize variables
$aspNetCoreModuleFilePath="$env:windir\system32\inetsrv\aspnetcore.dll"
$aspNetCoreModuleV2FilePath="$env:ProgramFiles\IIS\Asp.Net Core Module\V2\aspnetcorev2.dll"
$sm = Get-IISServerManager
# Set Allow for handlers section
$appHostconfig = $sm.GetApplicationHostConfiguration()
$section = $appHostconfig.GetSection("system.webServer/handlers")
$section.OverrideMode="Allow"
# Add aspNetCore section to system.webServer
$sectionaspNetCore = $appHostConfig.RootSectionGroup.SectionGroups["system.webServer"].Sections.Add("aspNetCore")
$sectionaspNetCore.OverrideModeDefault = "Allow"
$sm.CommitChanges()
# Configure globalModule
$globalModules = Get-IISConfigSection "system.webServer/globalModules" | Get-IISConfigCollection
New-IISConfigCollectionElement $globalModules -ConfigAttribute @{"name"="AspNetCoreModule";"image"=$aspNetCoreHandlerFilePath}
New-IISConfigCollectionElement $globalModules -ConfigAttribute @{"name"="AspNetCoreModuleV2";"image"=$aspNetCoreModuleV2FilePath}
# Configure module
$modules = Get-IISConfigSection "system.webServer/modules" | Get-IISConfigCollection
New-IISConfigCollectionElement $modules -ConfigAttribute @{"name"="AspNetCoreModule"}
New-IISConfigCollectionElement $modules -ConfigAttribute @{"name"="AspNetCoreModuleV2"}
# Enable shared config
$sm = Get-IISServerManager
$sm.GetRedirectionConfiguration().GetSection("configurationRedirection").Attributes["enabled"].Value = $true
$sm.CommitChanges()
# Start WAS/W3SVC
Start-Service -Name W3SVC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment