locked
Powershell XML File All User to Users RRS feed

  • Question

  • Hello first question here so hope post it right.

    I have a problem with Powershell and xml file I don’t understand why this being so complicated.

    I have write some user information to XML file but need to have all <user> obejct in <Users></Users> after <Import> I have try this also with xml editor for trying but something maybe not make it so simple? Please explain how to fix this.

    <?xml version="1.0" encoding="utf-8"?>
    <Import>
      <User>
        <uid>
        </uid>
        <email>test@test1.se</email>
        <givenname>firstname1</givenname>
        <lastname>lastname1</lastname>
        <username>test1</username>
      </User>
      <User>
        <uid>
        </uid>
        <email>test2@test.se</email>
        <givenname>firstname2</givenname>
        <lastname>lastname2</lastname>
        <username>test2/username>
      </User>
    </Import>


    Sunday, September 15, 2019 10:57 AM

Answers

  • This would be a safer way to do this.

    [xml]$doc = @'
    <Import>
      <Users>
        <User>
          <uid>234234234</uid>
          <email>test1@test.se</email>
          <givenname>givn</givenname>
          <lastname>lastn</lastname>
          <username>test1</username>
        </User>
      </Users>
    </Import>
    '@
    
    $users = $doc.SelectSingleNode('//Users')
    
    foreach($e in $filePers){
    	$el = $doc.SelectSingleNode('//User').Clone()
    	$el.uid = $e.uid
    	$el.email = $e.email
    	$el.givenname = $e.givenname
    	$el.lastname = $e.lastname
    	$el.username = $e.username
    	$users.AppendChild($el)
    }
    $doc.OuterXml
    
    


    \_(ツ)_/

    • Marked as answer by Tobbe214 Tuesday, September 17, 2019 7:03 PM
    Monday, September 16, 2019 4:14 PM

All replies

  • You have missed one "<" in front of your second closing /username tag. I'd recommend using csv for structured data. XML is better for hierachical data like tree structures.

    Assumed you have your well formed xml in a file with the name sample.xml you can get all user attributes like this:

    [XML]$XML = Get-Content -Path D:\sample\sample.xml
    $XML.ChildNodes.user


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Sunday, September 15, 2019 11:45 AM
    • Proposed as answer by jrv Sunday, September 15, 2019 3:57 PM
    Sunday, September 15, 2019 11:41 AM
  • Thank you for answer! Yes ooh I miss that I send that in code, I have trying very much.. Yes I agree I would be happy if I can export just a CSV file but I cant. It’s a Export file to a system and must looking like that for import. When I use XML editor I Cant add like I want either but if I simply going in text editor and put <Users></Users> its looking like I want. Maybe I can fix this problem in code before also but I have trying in days with that also but can send that code too and see what saying about that. What happen when saving this to a XML with <Users></Users> is that "user" not in it, its in the end of all "user" code. But I dont realy care for moment about in what step I fix this just I can fix it in end of script. Thankful for all your help.

    $path = "C:\xmlfile.xml" 
    
    $doc = [xml](Get-Content -Path $path) 
    foreach($e in $filePers) 
    { 
     $element = $Doc.Import.Users.User.clone() 
     $element.uid = $e.uid
     $element.email = $e.email 
     $element.givenname = $e.givenname 
     $element.lastname = $e.lastname 
     $element.username = $e.username
     $doc.DocumentElement.AppendChild($element)
     
    } 


    Sunday, September 15, 2019 12:27 PM
  • You will need to get someone to help you with your English because it is impossible to understand what you are trying to say.  Please get help and ask your question again.


    \_(ツ)_/

    Sunday, September 15, 2019 3:59 PM
  • To create xml file by hand is a really bad idea. You should use at least Notepad++ or XML-Notepad or VSCode with the according extension. This way you'd see at least syntax errors.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Sunday, September 15, 2019 6:58 PM
  • I think this is what you are trying to ask.

    [xml]$doc = Get-Content C:\xmlfile.xm
    foreach ($e in $filePers){
    	$element = $Doc.Import.User[0].clone()
    	$element.uid = $e.uid
    	$element.email = $e.email
    	$element.givenname = $e.givenname
    	$element.lastname = $e.lastname
    	$element.username = $e.username
    	$doc.Import.AppendChild($element)
    } 


    \_(ツ)_/

    Sunday, September 15, 2019 7:11 PM
  • Hi and Thanks!

    Yes something like that. Now Little different. When run like this just my first user remains under <Users> previously <Users> were pushed into the end of code.

    I want all users in <Users>

    ####XML file template####
    <?xml version="1.0" encoding="utf-8"?>
    <Import>
      <Users>
        <User>
          <uid>234234234</uid>
          <email>test1@test.se</email>
          <givenname>givn</givenname>
          <lastname>lastn</lastname>
          <username>test1</username>
        </User>
      </Users>
    </Import>
    
    
    
    ######Write XML data###
    
    $doc = [xml](Get-Content -Path $path) 
    foreach($e in $filePers) 
    { 
     $element = $Doc.Import.Users.User.clone() 
     $element.uid = $e.uid
     $element.email = $e.email 
     $element.givenname = $e.givenname 
     $element.lastname = $e.lastname 
     $element.username = $e.username
     $doc.Import.AppendChild($element) 
    } 
    $doc.Save($path)
    
    
    ###Finish XML file###
    <?xml version="1.0" encoding="utf-8"?>
    <Import>
      <Users>
        <User>
          <uid>234234234</uid>
          <email>test@test.se</email>
          <givenname>asad</givenname>
          <lastname>jahaa</lastname>
          <username>jaja</username>
        </User>
      </Users>
      <User>
        <uid>hidden</uid>
        <email>hidden</email>
        <givenname>hidden</givenname>
        <lastname>hidden</lastname>
        <username>hidden</username>
      </User>
      <User>
        <uid>hidden</uid>
        <email>hidden</email>
        <givenname>hidden</givenname>
        <lastname>hidden</lastname>
        <username>hidden</username>
      </User>
      <User>
    </Import>

    Monday, September 16, 2019 2:02 PM
  • $element =$Doc.Import.Users.User[0].clone()

     $doc.Import.Users.AppendChild($element)

    Your XML is still broken and what you claim is impossible.

    I recommend learning what XML is and how it works before continuing:

    https://www.w3schools.com/xml/default.asp



    \_(ツ)_/



    • Edited by jrv Monday, September 16, 2019 4:06 PM
    Monday, September 16, 2019 3:50 PM
  • This would be a safer way to do this.

    [xml]$doc = @'
    <Import>
      <Users>
        <User>
          <uid>234234234</uid>
          <email>test1@test.se</email>
          <givenname>givn</givenname>
          <lastname>lastn</lastname>
          <username>test1</username>
        </User>
      </Users>
    </Import>
    '@
    
    $users = $doc.SelectSingleNode('//Users')
    
    foreach($e in $filePers){
    	$el = $doc.SelectSingleNode('//User').Clone()
    	$el.uid = $e.uid
    	$el.email = $e.email
    	$el.givenname = $e.givenname
    	$el.lastname = $e.lastname
    	$el.username = $e.username
    	$users.AppendChild($el)
    }
    $doc.OuterXml
    
    


    \_(ツ)_/

    • Marked as answer by Tobbe214 Tuesday, September 17, 2019 7:03 PM
    Monday, September 16, 2019 4:14 PM
  • Thanks! Can be better. But the same problem when printing it out. Import is missing, and I don't understand why.
    My problem is I get file back because they cant import it. If I manualy add Import in xml file its okey.
    Tuesday, September 17, 2019 12:38 PM
  • Thanks! Can be better. But the same problem when printing it out. Import is missing, and I don't understand why.
    My problem is I get file back because they cant import it. If I manualy add Import in xml file its okey.

    The code I posted does exactly what you are asking. You issue makes no sense.  It sounds like you are not saving the file or not saving it to the correct place.  You must use the full path when saving the file.


    \_(ツ)_/


    • Edited by jrv Tuesday, September 17, 2019 5:55 PM
    Tuesday, September 17, 2019 5:55 PM
  • Hello again and thank you very much for your help and time! I was a little quick to respond. Powershell hung up then your code worked fine! I think I had something old left in the variable that disturbed.
    I still don't understand why the xml-file could not be created in first way but I maybe I'll understand that later now my problem is solved! =)

    Tuesday, September 17, 2019 7:01 PM