Powershell variable howto question


I have this code snippet that works as expected

	$xml = [xml](Get-Content $path)
	$node = $xml.Configuration.Servers.Server

Open in new window

What I really want to do is pass "Configuration.Servers.Server" as a parameter. The various methods I have tried are failing.

So ultimately I want $node = $xml.$XMLPath where $XMLPath =  "Configuration.Servers.Server"
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Didn't I show you how to do it in your previous question, http://www.experts-exchange.com/Q_28682878.html ?
ckelsoeAuthor Commented:
Technically it is a different question - how to combine two variables to get the same result. I did try your method and did not have success with it. The code I have is working fine with the exception of not wanting to hard code the path to the attributes.

I am finding there are multiple ways to perform the same task. I understand that your method is at least one of two methods to change the value of an attribute. As such I do appreciate your help.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I asked because it is important to know what your real issue is.
First, you need to differ between an XML path and attributes. Using the attribute notation for XML is an adaption to allow easy access.
On the other site you can use variables to keep property names dynamic.
But you can't use a variable to provide the path (XML path or hierarchical properties) the way you want to do - at least that easily.
You can always build a full dynamic expression. That would then be something like
 invoke-expression "ˋ$xml.$XMLPath"
If you know the number of hierarchical elements you can write
where each of the vars contains exactly one XML "property" name valid at that XML path level.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ckelsoeAuthor Commented:
Now that did exactly what I wanted!! Thank You.

I was working through your preferred method. Passing "//configuration/servers/server" from the array. Did not get that fully working before I saw this answer. I will play with that method again shortly.

I do appreciate your help very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.