header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Accepting an array from XML
Last Post 05 Mar 2010 02:57 PM by bobdee1980. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
bobdee1980User is Offline
New Member
New Member
Posts:57
Avatar

--
05 Mar 2010 01:15 PM  
Hi,

I'm trying to create an array in a XML config file, then pass it through to a script but I am having problems creating the array in the script.  It's recognising the data, but only as a string rather than an array...  If that makes sense?

IE - my desired outcome would be like this.

$Servers = ('DPROVA01','DPROVA02')
foreach ($Server in $Servers) {
Write-Host $Server
}

So, the XML is (the node)
  add key="SERVERS" value="('QPROVA01','QPROVA02')"

And the script...
$xmldata = [xml](Get-Content C:\test\config.xml)
$Servers = $xmldata.configuration.appSettings.add | where { $_.key -eq "SERVERS" }
$Servers = $Servers.value

foreach ($Server in $Servers) {
Write-Host $Server
}

Can anyone point me in the right direction of building the array correctly?  Any help would be appreciated.

Thanks in advance

Rob.


marco.shawUser is Offline
Site Moderator
Advanced Member
Advanced Member
Posts:593
Avatar

--
05 Mar 2010 01:31 PM  
Not as pretty as it could be, but try:
$servers=$xmldata.configuration.appsettings.add| where { $_.key -eq "SERVERS" }|foreach{$_.value.split(",")}|foreach{$_.replace("'","").replace("(","").replace(")","")}
Marco

*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
*Blog - http://marcoshaw.blogspot.com
bobdee1980User is Offline
New Member
New Member
Posts:57
Avatar

--
05 Mar 2010 02:30 PM  
Thanks for the reply... But, now it's not returning any value.

$xmldata = [xml](Get-Content C:\test\config.xml)
$Servers = $xmldata.configuration.appSettings.add | where { $_.key -eq "SERVERS" } |foreach{$_.value.split(",")}| foreach{$_.replace("'","").replace("(","").replace(")","")}
$Servers = $Servers.value

foreach ($Server in $Servers) {
Write-Host $Server
}

Before i modified the script, it was returning the string that was in the xml file.  I've tried

$Servers = $Servers.value -replace "'",""""

too, but this also just creates a string rather than a nice array!

I might just hard code it lol
marco.shawUser is Offline
Site Moderator
Advanced Member
Advanced Member
Posts:593
Avatar

--
05 Mar 2010 02:38 PM  
Remove this line and retry:
$servers=$servers.value
Marco

*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
*Blog - http://marcoshaw.blogspot.com
bobdee1980User is Offline
New Member
New Member
Posts:57
Avatar

--
05 Mar 2010 02:48 PM  
Thats currently the way I have it....  I had tried the -replace previous to creating this thread.

For the record, the script is currently like this.

$xmldata = [xml](Get-Content C:\test\config.xml)
$Servers = $xmldata.configuration.appSettings.add | where { $_.key -eq "SERVERS" } |foreach{$_.value.split(",")}| foreach{$_.replace("'","").replace("(","").replace(")","")}
$Servers = $Servers.value

foreach ($Server in $Servers) {
Write-Host $Server
}

and returns no value.
marco.shawUser is Offline
Site Moderator
Advanced Member
Advanced Member
Posts:593
Avatar

--
05 Mar 2010 02:53 PM  
You *must* remove this line from your script:
$servers=$servers.value
Marco

*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
*Blog - http://marcoshaw.blogspot.com
bobdee1980User is Offline
New Member
New Member
Posts:57
Avatar

--
05 Mar 2010 02:57 PM  
Sorry Marco - I misinderstood what you meant, I thought you were saying to remove the -replace.

Thats worked, thank you very much - it's very appreciated.

Robbie.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer