Link to home
Start Free TrialLog in
Avatar of ckelsoe
ckelsoeFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ckelsoe

ASKER

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.
Avatar of ckelsoe

ASKER

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

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.
Avatar of ckelsoe

ASKER

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.