Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you use PowerShell's . (dot) notation to walk through elements with periods in the element name?

Tags:

powershell

xml

PowerShell makes working with XML fairly easy in most cases. However, I'm having trouble using the "dot" notation to walk through a web.config file. Everything works

Import-Module WebAdministration
$site = get-website | ? { $_.name -eq "My Website - 80" }
$WebConfigFile = $site.physicalPath + "\web.config"
[xml]$WebAppXml = Get-Content $WebConfigFile
$webAppXml.configuration.system.web

On the last line, PowerShell uses intellisense to fill in system.web, but if you try to run it, the output is null. I know I can use Xpath or the .NET Xml objects, but I'd rather not if I don't have to.

like image 815
Jim Avatar asked Jun 29 '11 20:06

Jim


Video Answer


3 Answers

$webAppXml.configuration.'system.web' 
like image 192
Aasmund Eldhuset Avatar answered Sep 19 '22 07:09

Aasmund Eldhuset


This works too:

e.g.

$webAppXml.configuration.["system.web"].Identity 
like image 34
tellingmachine Avatar answered Sep 19 '22 07:09

tellingmachine


Alternatively, you can use variables:

$subproperty = 'System.Web'
$webAppXml.configuration.$subproperty

I find this to be the most useful technique, because it lets me switch properties easily.

BTW, you can use the same trick to get to a method definition, and not run it.

Hope this Helps

like image 44
Start-Automating Avatar answered Sep 20 '22 07:09

Start-Automating