locked
Powershell XML file modification RRS feed

  • Question

  • Hi,

    Could someone assist me with a Powershell / XML type question please, i am trying to write out element values in a nmde but its being writen at the wrong level, i cant seem to get it working.

    Thanks

    Source XML file

    <?xml version="1.0" encoding="utf-8"?>
    <manifest>
      <meta>
        <manifestVersion>1.1</manifestVersion>
        <createdDateTime>23.01.2014 14:16:53</createdDateTime>
        <createdBy>EnterpriseTransporter, Version=5.0.3.7, Culture=neutral, PublicKeyToken=null</createdBy>
        <modifiedDateTime>24.01.2014 16:15:41</modifiedDateTime>
        <lastRunDateTime>24.01.2014 16:15:38</lastRunDateTime>
        <lastRunDuration>2.74</lastRunDuration>
        <lastRunMode>preview</lastRunMode>
      </meta>
      <options bulk="false" turbo="false" ignoreWarnings="true" triggerSlowsync="false" migrateWlan="false" />
      <datasources>
        <sourceDomain label="Source">
          <defaults serverName="" />
        </sourceDomain>
        <destinationDomain label="Target">
          <defaults serverName="" groupName="something" ITPolicyName="something2" SwConfigName="" />
        </destinationDomain>
      </datasources>
      <users>
        <user email="" destServer="" groupName="" ITPolicyName="" SwConfigName="" />
     </users>
    </manifest>


    Script

    $path = "C:\Script\bet\manifestsource.xml"
    $csvPath = "C:\Script\bet\import.csv"
    $doc = [xml](Get-Content -Path $path)

    foreach($e in (Import-Csv -Path $csvPath))
    {

     $element = $doc.Manifest.users.user.clone()
     $element.email = $e.useremail
     $element.destServer = $e.destServer
     $element.groupName = "Something"
     $element.ITPolicyName = "Something2"
     $element.SwConfigName = ""
     $doc.DocumentElement.AppendChild($element)

    }
    $doc.Save("C:\Script\bet\manifest.xml")

    This is what i get

    <?xml version="1.0" encoding="utf-8"?>
    <manifest>
      <meta>
        <manifestVersion>1.1</manifestVersion>
        <createdDateTime>23.01.2013 14:16:53</createdDateTime>
        <createdBy>EnterpriseTransporter, Version=5.0.3.7, Culture=neutral, PublicKeyToken=null</createdBy>
        <modifiedDateTime>24.03.2013 16:15:41</modifiedDateTime>
        <lastRunDateTime>24.03.2013 16:15:38</lastRunDateTime>
        <lastRunDuration>2.74</lastRunDuration>
        <lastRunMode>preview</lastRunMode>
      </meta>
      <options bulk="false" turbo="false" ignoreWarnings="true" triggerSlowsync="false" migrateWlan="false" />
      <datasources>
        <sourceDomain label="Source">
          <defaults serverName="" />
        </sourceDomain>
        <destinationDomain label="Target">
          <defaults serverName="" groupName="HSFProd-Baseline" ITPolicyName="HSFGlobal" SwConfigName="" />
        </destinationDomain>
      </datasources>
      <users>
        <user email="" destServer="" groupName="" ITPolicyName="" SwConfigName="" />
      </users>
      <user email="someone1@somewhere.com" destServer="server1" groupName="something" ITPolicyName="something2" SwConfigName="" />
      <user email="someone2@somewhere.com" destServer="server2" groupName="something" ITPolicyName="something2" SwConfigName="" />
      <user email="someone3@somewhere.com" destServer="server1" groupName="something" ITPolicyName="something2" SwConfigName="" />
    </manifest>

    This is what i need

    <?xml version="1.0" encoding="utf-8"?>
    <manifest>
      <meta>
        <manifestVersion>1.1</manifestVersion>
        <createdDateTime>23.01.2013 14:16:53</createdDateTime>
        <createdBy>EnterpriseTransporter, Version=5.0.3.7, Culture=neutral, PublicKeyToken=null</createdBy>
        <modifiedDateTime>24.03.2013 16:15:41</modifiedDateTime>
        <lastRunDateTime>24.03.2013 16:15:38</lastRunDateTime>
        <lastRunDuration>2.74</lastRunDuration>
        <lastRunMode>preview</lastRunMode>
      </meta>
      <options bulk="false" turbo="false" ignoreWarnings="true" triggerSlowsync="false" migrateWlan="false" />
      <datasources>
        <sourceDomain label="Source">
          <defaults serverName="" />
        </sourceDomain>
        <destinationDomain label="Target">
          <defaults serverName="" groupName="HSFProd-Baseline" ITPolicyName="HSFGlobal" SwConfigName="" />
        </destinationDomain>
      </datasources>
      <users>
        <user email="someone1@somewhere.com" destServer="server1" groupName="something" ITPolicyName="something2" SwConfigName="" />
        <user email="someone2@somewhere.com" destServer="server2" groupName="something" ITPolicyName="something2" SwConfigName="" />
        <user email="someone3@somewhere.com" destServer="server1" groupName="something" ITPolicyName="something2" SwConfigName="" />
      </users>
    </manifest>

    Monday, January 27, 2014 2:04 PM

Answers

  • The following work perfectly for me:

    $doc=[xml]@'
    <?xml version="1.0" encoding="utf-8"?>
     <manifest>
       <meta>
         <manifestVersion>1.1</manifestVersion>
         <createdDateTime>23.01.2014 14:16:53</createdDateTime>
         <createdBy>EnterpriseTransporter, Version=5.0.3.7, Culture=neutral, PublicKeyToken=null</createdBy>
         <modifiedDateTime>24.01.2014 16:15:41</modifiedDateTime>
         <lastRunDateTime>24.01.2014 16:15:38</lastRunDateTime>
         <lastRunDuration>2.74</lastRunDuration>
         <lastRunMode>preview</lastRunMode>
       </meta>
       <options bulk="false" turbo="false" ignoreWarnings="true" triggerSlowsync="false" migrateWlan="false" />
       <datasources>
         <sourceDomain label="Source">
           <defaults serverName="" />
         </sourceDomain>
         <destinationDomain label="Target">
           <defaults serverName="" groupName="something" ITPolicyName="something2" SwConfigName="" />
         </destinationDomain>
       </datasources>
       <users>
         <user email="" destServer="" groupName="" ITPolicyName="" SwConfigName="" />
      </users>
     </manifest>
    '@
    
    $user=$doc.SelectSingleNode('//users/user')
    Import-Csv -Path c:\scripts\testman.csv |
         ForEach-Object{
               $u=$user.Clone()
               $u.email = $_.useremail
               $u.destServer = $_.destserv 
               $u.groupName = 'group'
               $u.ITPolicyName = "Something2"
               $uSwConfigName = "" 
               $doc.Manifest.users.AppendChild($u) 
         }
    


    ¯\_(ツ)_/¯

    • Marked as answer by Stejaznat Tuesday, January 28, 2014 11:44 AM
    Tuesday, January 28, 2014 1:18 AM

All replies

  • You are just opening and save the same document. Try and explain what you need to do.

    What is the CSV file for?

    What is an nmde? What level are we talking about. You append to a nodes children. That is where the added elements will end up.


    ¯\_(ツ)_/¯

    Monday, January 27, 2014 2:15 PM
  • Ok - I see it:

    $doc.DocumentElement.AppendChild($element)

    You are appending to the root document.  You want to append to the target node which is:

    $doc.Manifest.users

    You are adding a user node.  It needs to be added to the 'users' node's children.


    ¯\_(ツ)_/¯

    Monday, January 27, 2014 2:24 PM
  • I updated the script, so the correct users node is written to, however the foreach loop is now not working, it writes the first line with the varibles from the csv file and then completes.  

    The CSV files contains 2 columns called useremail and destserver in each row is an email address and servername. my test csv file has 3 rows only row 1 is being writen to the xml file.

    $path = "C:\Script\bet\manifestsource.xml"
    $csvPath = "C:\Script\bet\import.csv"
    $doc = [xml](Get-Content -Path $path)

    foreach($e in (Import-Csv -Path $csvPath)) {
     $element = $doc.manifest.users.user.clone()
     $element.email = $e.useremail
     $element.destServer = $e.destServer
     $element.groupName = "something"
     $element.ITPolicyName = "something2"
     $element.SwConfigName = ""
     $node=$doc.SelectSingleNode('//users')
     $node.AppendChild($element)
     }

    $doc.Save("C:\Script\bet\manifest.xml")


    • Edited by Stejaznat Monday, January 27, 2014 3:28 PM
    Monday, January 27, 2014 3:27 PM
  • The following work perfectly for me:

    $doc=[xml]@'
    <?xml version="1.0" encoding="utf-8"?>
     <manifest>
       <meta>
         <manifestVersion>1.1</manifestVersion>
         <createdDateTime>23.01.2014 14:16:53</createdDateTime>
         <createdBy>EnterpriseTransporter, Version=5.0.3.7, Culture=neutral, PublicKeyToken=null</createdBy>
         <modifiedDateTime>24.01.2014 16:15:41</modifiedDateTime>
         <lastRunDateTime>24.01.2014 16:15:38</lastRunDateTime>
         <lastRunDuration>2.74</lastRunDuration>
         <lastRunMode>preview</lastRunMode>
       </meta>
       <options bulk="false" turbo="false" ignoreWarnings="true" triggerSlowsync="false" migrateWlan="false" />
       <datasources>
         <sourceDomain label="Source">
           <defaults serverName="" />
         </sourceDomain>
         <destinationDomain label="Target">
           <defaults serverName="" groupName="something" ITPolicyName="something2" SwConfigName="" />
         </destinationDomain>
       </datasources>
       <users>
         <user email="" destServer="" groupName="" ITPolicyName="" SwConfigName="" />
      </users>
     </manifest>
    '@
    
    $user=$doc.SelectSingleNode('//users/user')
    Import-Csv -Path c:\scripts\testman.csv |
         ForEach-Object{
               $u=$user.Clone()
               $u.email = $_.useremail
               $u.destServer = $_.destserv 
               $u.groupName = 'group'
               $u.ITPolicyName = "Something2"
               $uSwConfigName = "" 
               $doc.Manifest.users.AppendChild($u) 
         }
    


    ¯\_(ツ)_/¯

    • Marked as answer by Stejaznat Tuesday, January 28, 2014 11:44 AM
    Tuesday, January 28, 2014 1:18 AM
  • Thanks for your help, your responce pointed me in the right direction and the script now does what i need to to.
    Tuesday, January 28, 2014 11:44 AM