header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Accepting an array from XML
Last Post 05 Mar 2010 06:57 AM by Bobdee. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BobdeeUser is Offline
Basic Member
Basic Member
Posts:130
Avatar

--
05 Mar 2010 05:15 AM
    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 Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    05 Mar 2010 05:31 AM
    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
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    05 Mar 2010 06:30 AM
    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 Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    05 Mar 2010 06:38 AM
    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
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    05 Mar 2010 06:48 AM
    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 Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    05 Mar 2010 06:53 AM
    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
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    05 Mar 2010 06:57 AM
    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.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer