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?

1
  • Which GUI are you talking about? There are several GUIs that can be used to edit web.config files.
    – austinian
    Jul 24, 2015 at 4:15

2 Answers 2

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 
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.

1
  • The Web.config Validator also checks for the content. It's not possible to call it from PowerShell though. Maybe it should? Oct 17, 2023 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .