How do I check and Update a web.config file (XML)

I am working on a script to check for a particular node in a web.config file for the SharePoint Team.
The idea is to check if the node exist. If so, check to see if the request limits are set to “maxAllowedContentLength”,”83886080″
If so, all is well. If not set “maxAllowedContentLength”,”83886080″.
If the node does not exist, add it.
I am also logging my results and traping any errors. You may want to add or delete directories in the $xdir array. The -recurse function is an all or nothing sort of thing. Excluding directories is not supported. Hopefully in the next release!

BTW: if you copy this, you will need to remove the back ticks (`) from the begining of lines 73-79. I had to add them so WordPress would show the (Greater than, Less than) properly.

###### Start Posh Script ########

function write-log([string]$info){
if($loginitialized -eq $false){
#$FileHeader > $logfile
$script:loginitialized = $True
$info >> $logfile
#---------Logfile Info----------#
$script:UseInfo = $($(get-date -format MM/dd/yyyy-HH:mm:ss) + '`t' + $env:username + '`t')

Trap [Exception] {
write-log $('$UseInfo`t$_. - Line:(' + $($_.InvocationInfo.ScriptLineNUmber)+':'+$($_.InvocationInfo.OffsetInLine)+ ') ' + $($_.InvocationInfo.Line))

$xdir = @('_app_bin','_vti_pvt','App_Browsers','App_GlobalResources','aspnet_client','bin','wpcatalog','wpresources')
$LogPath = Test-Path 'E:\LogFiles\ps\UpdateWebConfig' -pathType container
If ($LogPath) {
$script:logfile = 'E:\LogFiles\ps\UpdateWebConfig\WebConfig Update - $(get-date -format MMddyy-HHmmss).log'
$script:loginitialized = $false
} Else { New-Item 'E:\LogFiles\ps\UpdateWebConfig' -type directory
$script:logfile = 'E:\LogFiles\ps\UpdateWebConfig\WebConfig Update - $(get-date -format MMddyy-HHmmss).log'
$script:loginitialized = $false
$d = Get-Date -f MMddyy-HHmmss
Write-log '***Application Information***'
Write-log 'Filename: UpdateWebConfig.ps1'
Write-Log 'Created by: $env:username'
Write-Log 'Last Modified: $d'
Write-Log '***Application Information***'

$ErrorActionPreference = 'SilentlyContinue'
$rl = '83886080'
#$wc = Get-ChildItem -Recurse C:\Scripts\xml -include *.config
$wc = Get-ChildItem -Recurse E:\inetpub\wwwroot\wss\VirtualDirectories -include web.config |
where {$xdir -notcontains (Split-Path $_.DirectoryName -Leaf -EA SilentlyContinue)}

$XML = New-Object xml

foreach ($item in $wc){
$wtc = gc $item | Select-String 'requestLimits' -quiet
If ($wtc) {
$wcToChange = $XML.configuration.'system.webServer'.security.requestFiltering.requestLimits.GetAttribute('maxAllowedContentLength')
If ($wcToChange -eq $rl) {
Write-Host '$item is OK' -ForegroundColor Green
write-log '$item is OK'
Else {
Write-Host '$item is NOT OK maxAllowedContentLength = $wcToChange' -ForegroundColor Red
write-log '$item is NOT OK maxAllowedContentLength = $wcToChange'
write-log 'Backup File: `t$Item.bak-$d'
$wtc = $XML.configuration.'system.webServer'.security.requestFiltering.requestLimits.GetAttribute('maxAllowedContentLength')
write-log '$item is NOW OK maxAllowedContentLength = $rl'
write-log }
} Else {
Write-Host '$item is NOT OK' -ForegroundColor Red
Write-Host 'Need to Add RequestFiltering' -ForegroundColor Red
write-log 'Need to Add RequestFiltering to web.config file'
write-log 'Backup File: `t$Item.bak-$d'
# new system.webServer node and child nodes
[Xml]$sweb = @'
`<requestLimits maxAllowedContentLength='83886080'/>

# import and set the new system.webServer node
$newNode = $XML.ImportNode($sweb.'system.webServer', $true)
# append new system.webServer node
# check (this displays the results on the screen)
write-log $item
write-log `"requestLimits maxAllowedContentLength='83886080'"
write-log 'added to web.config file'

############# End POSH Script ###############

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s