Author Posts

January 9, 2016 at 1:00 am

I am having trouble reading the session timeout value (20) from the following config file. I have tried different method however, they seem to be not worthy reading the value. Any idea of how to get the value of the Session Timeout. Appreciate your help.

Web config file :

———————————————————————————————————–
Here are the methods that I have tried to retrieve the value :

#Accessing the contents of Web Config file

$path = "\Web.configfile"

$xdoc = [xml] (get-content $path)

#reading the default value of SessionTimeout

$sessionTimeOut = $xdoc.configuration.System.Web.sessionState.timeout

$timeOut = $sessionTimeOut.timeout

Write-Host $timeOut´╗┐

January 9, 2016 at 3:38 am

Please post the web config file on Gist and paste a link to it here so that we can take a look.

January 9, 2016 at 8:46 pm

Please find the Gist URL below for the XML content.

I have tried and got the result with the below script :

Here is how I am able to read the value of the attribute. However, I am not able to update it by following method. The console shows the value but the web config file does not update at all.

$path = "\web.config"
$xdoc = [xml] (get-content $path)

#reading the default value of SessionTimeout
$sessionTimeOut = $xdoc.configuration.'System.Web'.sessionState.timeout
Write-Host $sessionTimeOut

$sessionTimeOut = "1"

#Save the Web Config file
$xdoc.Save($path)

Write-Host $sessionTimeOut

January 9, 2016 at 10:07 pm

Because you're assigning the value to another variable. You need to update the value of the property within the xdoc variable as that is what you're saving in the end.

January 9, 2016 at 11:03 pm

@peter – Thank you very much. It worked.

Here is how I have retrieved and updated the value of the property :

#Accessing the contents of Web Config file
$path = "\Web.config"
$xdoc = [xml] (get-content $path)

#reading the default value of SessionTimeout
$sessionTimeOut = $xdoc.configuration.'System.Web'.sessionState.timeout

$updateTimOut = $xdoc.configuration.'System.Web'.sessionState
$finalTimeOut = $updateTimOut.timeout = "1"

#Save the Web Config file
$xdoc.Save($path)

Write-Host $finalTimeOut