ckelsoe
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:
My current code has the XML Path and Attribute Name hard coded as shown below:
$node = $xml.Configuration.Servers.Serv er
$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:
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.
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
My current code has the XML Path and Attribute Name hard coded as shown below:
$node = $xml.Configuration.Servers.Serv
$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
)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)"
}
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.
I've shown you exactly how to access the properties and the XML data according to them.
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.
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.
ASKER
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.