Dash in Variable Name

by robertow at 2013-01-24 03:13:38

Dear all,

I would like to modify an XML file used in Business Objects which looks like below. Inside this file I would like to increase the session-timeout.

[code2=xml]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "Oracle Java Technologies | Oracle
<web-app>

<display-name>SAP BusinessObjects InfoView</display-name>
<description>
SAP BusinessObjects InfoView
</description>

<session-config>
<session-timeout>20</session-timeout> <!-- 20 minutes for session objects –>
</session-config>


</web-app>[/code2]



Here is my problem: I can’t access the /web-app/session-config/session-timeout section (node) because powershell dislikes the hashes in the variable name when access via a hashtable.
The SelecNodes function works but not the below code after write-host stops working after the first item with a dash inside.
Works: $xml."web-app"
Does not work: $xml."web-app"."session-config"
Any ideas?

[code2=powershell]$xml = [xml](Get-Content $FileName)
#$xml.SelectNodes("/web-app/session-config")
$xml.SelectNodes("/web-app/session-config/session-timeout")

write-host "-------------------"
#$xml.web-app.session-config
#$xml.["web-app"]
#$xml.["web-app"].["session-config"]
#$xml.'web-app'.filter
#$xml.'web-app'
#$xml."web-app"
#$xml."web-app".filter
#$xml."/web-app/session-config"
#$xml.'web-app'.filter
#$xml.'web-app'.'session-config'
#$xml."web-app"."session-config"
#$xml."web-app".filter
#$xml."web-app" + . + "session-config"
#$xml.{web-app.filter}
#$xml.{web-app}.GetAttribute("session-config")
#$xml."web-app".GetAttribute("session-config")
#$xml.{web-app}.filter
#${xml."web-app".filter}
#${xml.web-app}
#$xml.web-app.session-config
#$xml.'web-app'.'session-config'
#$xml.[$"web-app"]
#$xml.['web-app']
#$xml."web-app.filter"[/code2]
by Infradeploy at 2013-01-24 05:05:24
Can’t test it at the moment, but i suggest you try it with single quotes

$xml.SelectNodes(‘/web-app/session-config/session-timeout’)
by robertow at 2013-01-25 00:51:35
The SelectNodes statements works fine. I face the problem to access the xml hashtable with lines like above powershell code starting with line 6.
I would like to update the session timeout node and then save the xml.

Something like this
[code2=powershell]$xml = [xml](Get-Content $FileName)
$xml.employee.version = "40"
$xml.Save($FileName)[/code2]

But because of the stupid dashes in "web-app.session-config.session-timeout" I can’t update the node like $xml.employee.version = "40"
by nohandle at 2013-01-25 04:01:41
It seems to work with custom objects properly:
$a = New-Object psobject @{'new-name' = 'hash'}
$a.'new-name' = New-Object psobject @{'new-name' = 'hash'}

$a.'new-name'.'new-name'
$a."new-name"."new-name"


I casted your xml to [xml] and then it does not work for me either.
By the . notation I am able to access only the$xml.'web-app'
but doing
$xml.'web-app'.'display-name'
or
$a = $xml.'web-app'
$a.'display-name'


does not return any data.

I was able to extract the data using select-object.

Edit: Just for info this is Powershell version 2.
by Jules at 2013-01-25 05:43:15
Perhaps I’m doing something wrong.

(0001) § {~} $xml = [xml](Get-Content D:\test.xml)
(0002) § {~} $xml.'web-app'


display-name description session-config
------------ ----------- --------------
SAP BusinessObjects InfoView … session-config


(0003) § {~} $xml.'web-app'.'session-config'

session-timeout #comment
--------------- --------
20 20 minutes for session objects


(0004) § {~} $xml.'web-app'.'session-config'.'session-timeout'
20
(0005) § {~} $xml."web-app"."session-config"."session-timeout"
20
(0006) § {~}


This is using PowerShell v3.

Another thought: Could it be the file encoding?
by robertow at 2013-01-25 06:38:12
1.
I am running powershell 2
I try to update my machine to version 3.

2. File encoding. Good idea
This is the complete original file https://www.box.com/s/vqdsp4jisgmvlqcnhswl
(Hopefully you can acces the file without creating an account)
by robertow at 2013-01-27 10:55:10
To quote professor farnsworth "Good news everyone".
Thanks to Jules and Poweshell 3 I am able to handle my XML file. Using Powershell 3 I am able to extract the timeout using the same term as Jules :
$xml.‘web-app’.‘session-config’.‘session-timeout’

Thanks for the support