开发者

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

开发者 https://www.devze.com 2023-03-16 13:09 出处:网络
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

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.configurati开发者_StackOverflow社区on.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.


$webAppXml.configuration.'system.web'


This works too:

e.g.

$webAppXml.configuration.["system.web"].Identity


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

0

精彩评论

暂无评论...
验证码 换一张
取 消