param( [string]$OutputPath = "$(Join-Path $PSScriptRoot 'applicationhost.config')", [string]$BaseConfigPath = "$env:ProgramFiles\IIS Express\AppServer\applicationhost.config", [string]$SiteName = "AspClassicPublicSite", [string]$PhysicalPath = "H:\AI - ASP - Classic\Public", [string]$BindingInformation = "*:8080:localhost" ) if (-not (Test-Path -LiteralPath $BaseConfigPath)) { $BaseConfigPath = "$env:ProgramFiles(x86)\IIS Express\AppServer\applicationhost.config" } if (-not (Test-Path -LiteralPath $BaseConfigPath)) { Write-Error "IIS Express base applicationhost.config not found." exit 1 } [xml]$xml = Get-Content -LiteralPath $BaseConfigPath $sitesNode = $xml.configuration.'system.applicationHost'.sites if (-not $sitesNode) { Write-Error "Base config does not contain system.applicationHost/sites." exit 1 } $existingSite = $sitesNode.site | Where-Object { $_.name -eq $SiteName } if ($existingSite) { [void]$sitesNode.RemoveChild($existingSite) } $siteIds = @($sitesNode.site | ForEach-Object { [int]$_.id }) $nextId = if ($siteIds.Count -gt 0) { ($siteIds | Measure-Object -Maximum).Maximum + 1 } else { 1 } $site = $xml.CreateElement('site') $site.SetAttribute('name', $SiteName) $site.SetAttribute('id', [string]$nextId) $site.SetAttribute('serverAutoStart', 'true') $application = $xml.CreateElement('application') $application.SetAttribute('path', '/') $application.SetAttribute('applicationPool', 'Clr4IntegratedAppPool') $virtualDirectory = $xml.CreateElement('virtualDirectory') $virtualDirectory.SetAttribute('path', '/') $virtualDirectory.SetAttribute('physicalPath', $PhysicalPath) [void]$application.AppendChild($virtualDirectory) $bindings = $xml.CreateElement('bindings') $binding = $xml.CreateElement('binding') $binding.SetAttribute('protocol', 'http') $binding.SetAttribute('bindingInformation', $BindingInformation) [void]$bindings.AppendChild($binding) [void]$site.AppendChild($application) [void]$site.AppendChild($bindings) [void]$sitesNode.AppendChild($site) $oldLocation = @($xml.configuration.location | Where-Object { $_.path -eq $SiteName }) foreach ($node in $oldLocation) { [void]$xml.configuration.RemoveChild($node) } $location = $xml.CreateElement('location') $location.SetAttribute('path', $SiteName) $systemWebServer = $xml.CreateElement('system.webServer') $directoryBrowse = $xml.CreateElement('directoryBrowse') $directoryBrowse.SetAttribute('enabled', 'false') $asp = $xml.CreateElement('asp') $asp.SetAttribute('scriptErrorSentToBrowser', 'true') $asp.SetAttribute('enableParentPaths', 'true') $limits = $xml.CreateElement('limits') $limits.SetAttribute('scriptTimeout', '00:10:00') [void]$asp.AppendChild($limits) [void]$systemWebServer.AppendChild($directoryBrowse) [void]$systemWebServer.AppendChild($asp) [void]$location.AppendChild($systemWebServer) [void]$xml.configuration.AppendChild($location) $xml.Save($OutputPath) Write-Host "Generated IIS Express config at $OutputPath"