HOWTO?: Multidimensional Arrays

Hi,

I am working on a script to loop through a series of .config files (XML) and replace an attribute value. I have this code working with an array containing a list of files to edit. I now would like to add a second part to the array to store the XML path of the attribute I want to edit.

For example:

File        XML Path                      Attribute Name
a.config    Configuration.Servers.Server  password
b.config    Configuration.Servers.Server  password
c.config    WSProcConfig                  SystemPassword

Open in new window


My current code has the XML Path and Attribute Name hard coded as shown below:

$node = $xml.Configuration.Servers.Server
$node.password = $password

What I really would like to do is have the XML Path and Attribute Name retrieved from the array.

The question is this:
1. How do I format the array? My current array looks like this:
$ConfigFilesArray = @(
"C:\temp\a.config,
"C:\temp\b.config,
"C:\temp\c.config
)

Open in new window


2. How do I read each part of the array?

I looked at a hash table, however, that seems to require knowing the file to get the rest of the data. I am just not sure how all of that needs put together in the Powershell world.

Your help is appreciated.
ckelsoeAsked:
Who is Participating?
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.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You use an array of objects to do that. Using your original input format:
$ConfigFilesArray = @"
File	XML Path	Attribute Name
a.config	Configuration.Servers.Server	password
b.config	Configuration.Servers.Server	password
c.config	WSProcConfig	SystemPassword
"@ | convertFrom-CSV -del "`t"

Open in new window

Now you can access each array attribute by its name. I'm only showing the important parts, XML file load and save are left to you.
$ConfigFilesArray | % {
  # ... get the XML file, name is $_.Name ...
  $xml.SelectSingleNode($_."XML Path" -replace '.', '/').($_."Attribute Name") = $password
  # ... and save ...
}

Open in new window

Note that your XML Path is not really a path, and hence needs to get converted to one, and the spaces in property names force us to enclose their names either in double/single quotes or curly braces.
0

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:
So if I understand, I can do this:

$ConfigFilesArray = @"
FilePath,XML Path, Attribute	
a.config,Configuration.Servers.Server,password
b.config,Configuration.Servers.Server,password
c.config,WSProcConfig,SystemPassword
"@ | convertFrom-CSV

Foreach ($ConfigFile in $ConfigFilesArray)
{
	$ConfigFile # How do I get to each part of one row of the array? 
}

Open in new window


I am just not sure how to get to the distinct items in each row.

As far as the XML - I am using the examples at http://blogs.msdn.com/b/sonam_rastogi_blogs/archive/2014/05/14/update-xml-file-using-powershell.aspx which is why I have the path in dot format instead of slash format.
0
ckelsoeAuthor Commented:
Ok - this works. Not sure if it is the most efficient method but I can get to each part of the array.

function New-FileObject ($FilePath, $XMLPath, $AttributeName){
	New-Object PsObject -Property @{
		FilePath = $FilePath;
		XMLPath = $XMLPath;
		AttributeName = $AttributeName}
}

$ConfigFilesArray = @()
$ConfigFilesArray += New-FileObject "a.config" "Configuration.Servers.Server" "password"
$ConfigFilesArray += New-FileObject "b.config" "Configuration.Servers.Server" "password"
$ConfigFilesArray += New-FileObject "c.config" "WSProcConfig" "SystemPassword"
$ConfigFilesArray

$ConfigFilesArray | ForEach-Object{
	Write-Host "The filepath is $($_.FilePath)"
	Write-Host "The XMLPath is $($_.XMLPath)"
	Write-Host "The Attribute is $($_.AttributeName)"
}

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You can use your function to generate the object array, but it does nothing different from my way to set $ConfigFilesArray (the name of the properties are arbitrary).

I've shown you exactly how to access the properties and the XML data according to them.
0
ckelsoeAuthor Commented:
Thanks for your help.

It does look like there are two different syntax to get to the attribute - your way and the way I followed from the MSDN blog post. Since I have that part working I will leave it as is. I will follow your example fully to understand and learn that method in another similar project.
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.