2

When you have a malformed web.config and try and edit part of it in the GUI it will popup a box saying so and telling you the line it broke on.

Is there a way to access this functionality thought the cmd/powershell?

Gilsham
  • 123
  • 1
  • 4

2 Answers2

1

You can use code like this:

$site = "Default Web Site"

try
{
    Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$site" -filter "system.webServer" -name . | Out-Null
    Write-Output "All is Well"
}
catch [System.Exception]
{
    Write-Output $_ 
}

The output may include:

Get-WebConfigurationProperty : Filename: \\?\C:\inetpub\wwwroot\web.config
Line number: 7
Error: Configuration file is not well-formed XML

or

Get-WebConfigurationProperty : Filename: \\?\C:\inetpub\wwwroot\web.config
Line number: 5
Error: The configuration section 'foo' cannot be read because it is missing a section declaration 
Peter Hahndorf
  • 14,058
  • 3
  • 41
  • 58
0

Unfortunately @Peter Hahndorf answer will only check for syntax issues, it does not capture the scenario where you have collisions between multiple levels of configuration.

I have created a function that addresses this, full answer is here: https://stackoverflow.com/questions/68260818/how-do-i-validate-an-iis-web-config-in-powershell/68260819#68260819

But the gist of it is you have to find all the "filters" and test each of them individually.

Justin
  • 101
  • 2