Get and Set the compilation debug flag in web.config


Since the main technology I support as a PFE is IIS, I often find myself writing scripts for IIS or ASP.NET management. For example, one of the first things I check when troubleshooting ASP.NET applications is the debug flag in the web.config.

For this, I’ve written the following two example functions that use the accelerator to cast the web.config file contents as an xmlDocument and use PowerShell’s dot notation to get the debug flag value, or use the .NET methods of the dom document to set the debug flag value:

 

function Get-WebConfigDebugFlag {

    PARAM(

        [string[]]$Path = $env:SystemDrive\inetpub\wwwroot”

    )

    foreach($Folder in $Path) {

        Get-ChildItem $Folder -Recurse -Include ‘web.config’ | ForEach-Object {

            if (Test-Path -Path $_.FullName -PathType Leaf) {

                $DebugFlag = (([xml](Get-Content -Path $_)).configuration.‘system.web’.compilation.debug)

                if (!$DebugFlag) { $DebugFlag = $null }

                New-Object -TypeName PSObject -Property @{‘Path’=$_; ‘DebugFlag’=$DebugFlag}

            }

        }

    }

}

 

 

function Set-WebConfigDebugFlag {

    PARAM(

        [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]

        [string]$Path,

 

        [Parameter(Mandatory=$true)]

        [ValidateSet(‘True’, ‘False’)]

        $DebugFlag = $false

    )

 

    process {

        foreach($Folder in $Path) {

            Get-ChildItem $Folder -Recurse -Include ‘web.config’ | ForEach-Object {

           

                if (Test-Path -Path $_ -PathType Leaf) {

                    $config = [xml](Get-Content -Path $_.FullName)

 

                    try {

                        if (($config.SelectSingleNode(‘configuration/system.web/compilation’)) -eq $null) {

                            $compilation = $config.CreateElement(‘compilation’)

                            $sw = $config.SelectSingleNode(‘configuration/system.web’)

                            [void] $sw.AppendChild($compilation)

                        }

                       

                        if ($config.SelectSingleNode(‘configuration/system.web/compilation/debug’) -eq $null) {

                            $compilation = $config.SelectSingleNode(‘configuration/system.web/compilation’)

                            $compilation.SetAttribute(‘debug’, ($DebugFlag.ToString()).ToLower())

                        } else {

                            $config.configuration.‘system.web’.compilation.debug = $DebugFlag

                        }

 

                        $config.Save($_)

                        if($?) {

                            $Result = ‘Success’

                            $ThisDebugFlag = $DebugFlag

                        } else {

                            $Result = ‘Error saving the config file’

                            $ThisDebugFlag =

                        }

 

                    } catch {

                        $Result = $_.Exception.Message

                        if ($Result -eq ‘You cannot call a method on a null-valued expression.’) {

                            $Result = “Configuration does not contain a ‘system.web’ section”

                            $ThisDebugFlag =

                        }

                    }

                    New-Object -TypeName PSObject -Property @{‘Path’=$_; ‘DebugFlag’=$ThisDebugFlag; ‘Result’=$Result }

                }

            }

        }

    }

}

 

# Usage Examples:

Get-WebConfigDebugFlag -Path ‘C:\inetpub\wwwroot’

Get-WebConfigDebugFlag -Path ‘\\myRemoteServer1\myShare$\wwwroot’,‘\\myRemoteServer2\myShare$\wwwroot’

Get-WebConfigDebugFlag -Path ‘C:\inetpub\wwwroot’ |

    Where-Object { $_.DebugFlag -eq ‘True’ } |

        Set-WebConfigDebugFlag -DebugFlag False

 

 

For more details on the debug flag and the issues it presents, see:

Debug Mode in ASP.NET Applications

ASP.NET Memory: If your application is in production… then why is debug=true

 

HTH,

\Martin.

Comments (0)

Skip to main content