I’ve recently started using PowerShell to make my life easier in my job.
I often have to check Web.Config files on different servers and want to do this using PS as there can be 10 different App’s running on the same server all with their own Web.Config. Rather then just search for a string I would like to use the actual key and print out the value.
The XML files have multiple levels of nested elements, and after googling there seems to be so many different opinions on how to do this.
Can someone help point me in the right direction, maybe an e-book or video that covers how to get started.
No matter what you read, you are going to have to decide what works for you. Meaning, what you can grasp, understand and internalize for future needs.
There are lots of articles all over the web regarding using PS to create, read, update and delete XML files and or their content. As you’ve obviously already found, but don’t let that overwhelm you. Be specific in the task you are after, and attack it one step at a time. PS is natively XML heavy in many respects.
There are cmdlets specifically for dealing XML as well as leveraging the .Net namespace for it.
Of course using any of what is hear requires you know what you are after and use the correct resource.
Here are few examples I’ve passed on.
PowerShell Data Basics: XML - Simple Talk
•Accessing XML data in PowerShell ◦Accessing XML with XPath.
◦Accessing XML as Objects.
◦Comparison of XPath and Object Approaches
•Modifying or Creating XML Data ◦Adding XML Data
◦Using XML for Object Serialization
Mastering everyday XML tasks in PowerShell
https://www.powershellmagazine.com/2013/08/19/mastering-everyday-xml-tasks-in-powershell
The Scripting Wife Learns to Use PowerShell to Work with XML
https://blogs.technet.microsoft.com/heyscriptingguy/2012/03/25/the-scripting-wife-learns-to-use-powershell-to-work-with-xml
Thank you very much for the links, I’ve not had a chance to go through all of them yet but that ‘Scripting Wife’ blog was a great starting point and exactly what I needed to get me going.
Following through the blog I’ve hit a bit of a snag. If i’m understanding this correctly from the online stuff I’ve read, the end result is being put into a hash table…I think, I’m looking for the value of DataPortalServer, so when I do $xml.configuration.appsettings.add.value[6] this gives me the information that I’m looking for. However, the index may not always be the same, so I want to address it by the name of the key.
I’ve tried .ContainsKey(‘DataPortalServer’), I’ve tried $xml.configuration.appsettings.add.value[‘DataPortalServer’] as well as several other things that I’ve googled but I just can’t get it to work.
Thank’s so much for what you’ve already provided.
Function QPConfigFiles{
[xml]$xml=get-content C:\test.xml
$xml.configuration.appsettings.add ('CslaDataPortalUrl')
}
QPConfigFiles
Hi, I don’t see your latest reply. Unfortunately this forum can’t handle xml. You can make a gist of the xml and link to it here. (Maybe this forum should use the technet system?) Actually select-xml has some potential. But I don’t know xpath. Xpath runs deep. Xpath filters can be used with get-winevent as well.
I was trying this. Both $a.node and $xml.objs are XmlElement types.
# Selects the first book element that is the child
# of the bookstore element
$a = select-xml /bookstore/book[1] xpath.xml
$a
$a.node
$a.node.gettype()
# Selects all the title elements (anywhere) that have a "lang" attribute
# with a value of "en"
(select-xml "//title[@lang='en']" xpath.xml).node
lang #text
---- -----
en Everyday Italian
en Harry Potter
en XQuery Kick Start
en Learning XML
[xml]$xml = get-content xpath.xml
$xml.gettype()
$xml.Objs
$xml.Objs.gettype()
# same thing
$xml = (select-xml / xpath.xml).node
$key = $configfiles[$i].param2
# selectnodes() takes an xpath string
# '//add' means search for an add node anywhere
$a = $xml.selectNodes("//add[@key='$key']")
But I don’t see an ‘ApiUrl’ key in your xml. I guess it’s in a different xml. Also the bottom closing ‘< /configuration >’ tag is missing in the xml.
Unless you want to use the “$( )” method, but it gets more complex and less readable. “$object[$index]” or “$object.property” doesn’t work inside a string the way you would want.