windows - Powershell script for creating local users and write details to XML file -


I have created a small script to create an environment for Windows local users and their FTP access. The script is working well, apart from it, it should write the details in the XML file. The script creates only one user at a time stores the name of the user in the variable named $ accountName , the full name in $ fullname , $ password generated password works in the home directory $ directoryPath and the variable $ date creates the date code of creation, but it is always accompanied by the details of the last-created user Adds the file to an XML file.

  To specify the XML file - Host - First yellow "Write a description to XML ..." # # Create a Template XML to keep the data $ template @ @ '& lt; FtpUsers & gt; & Lt; Account & gt; & Lt; Account's & gt; & Lt; / Account & gt; & Lt; FULLNAME & gt; & Lt; / FULLNAME & gt; & Lt; Password & gt; & Lt; / Password & gt; & Lt; DirectoryPath & gt; & Lt; / DirectoryPath & gt; & Lt; Date & gt; & Lt; / Date & gt; & Lt; / Account & gt; & Lt; / FtpUsers & gt; '$ $ Template | ( "C: \ ProgramData \ InstanceLogFiles \ FtpUsers.xml") \ ProgramData \ InstanceLogFiles \ FtpUsers.xml XML object -encoding UTF8 # load template = new-object XML $ xml.Load: # grab template file C users out $ XML $ Newuser = (@ ($ xml.FtpUsers.account) [0]). To use the clone () # to add the local user account XML $ newuser = $ newuser.clone () $ newuser.accountName = $ accountName $ newuser .fullName = $ fullName $ newuser.password = $ password $ newuser.directoryPath = $ Directory $ newuser.date = $ date $ xml.FtpUsers.AppendChild ($ newuser) & gt; $ Null # Remove users with undefined names (delete templates) $ xml.FtpUsers.account | Where-object {$ _. Accountant-eq ""} | For writing e-objects, [xx] $ xml.FtpUsers.RemoveChild ($ _)} # save xml $ xml.Save ("C: \ ProgramData \ InstanceLogFiles \ FtpUsers.xml") - First yellow ". .done!"  

I think the "$ template ..." section writes files every time but:

- I tried to use the template ". "Successfully without section for cycle".

- I tried to write the template in a separate file, but if it does not exist, then the script returns error.

Anyone will help please let me know where the problem is and how should I code it right? Thank you in advance!

You can try something like this:

The fact is that c: \ temp \ Ftpusers.xml contains

  & lt ;? xml version = "1.0" encoding = "UTF-8"? & Gt; & gt; FtpUsers & gt; & quot; Lt; / FtpUsers & gt;  

The following code (you have to replace the hard coded string with your wires and create a function in it):

  #Open Documentation [xml] $ XmlDoc = [xml] (Received-content "C: \ temp \ ftpusers.xml") # Obtain our root node $ root = selection- xml-xml $ xmlDoc -XPath "FtpUsers" # node build and text $ xmlElt = $ xmlDoc.CreateElement ( "Account"), a sub-node $ xmlSubElt1 = $ xmlDoc.CreateElement ( "ACCOUNTNAME") $ xmlSubText1 = $ xmlDoc.CreateTextNode ( "user1 ") # Build of $ xmlSubElt1.AppendChild ($ xmlSubText1) | Out-null $ xmlElt.AppendChild ($ xmlSubElt1) | Outside $ xmlSubElt2 = $ xmlDoc.CreateElement ("full name") $ xmlSubText2 = $ xmlDoc.CreateTextNode ("full name user1") $ xmlSubElt2.AppendChild ($ xmlSubText2) | Out-null $ xmlElt.AppendChild ($ xmlSubElt2) | Outside $ xmlSubElt3 = $ xmlDoc.CreateElement ("password") $ xmlSubText3 = $ xmlDoc.CreateTextNode ("password user1") $ xmlSubElt3.AppendChild ($ xmlSubText3) | Out-null $ xmlElt.AppendChild ($ xmlSubElt3) | Out null $ xmlSubElt4 = $ xmlDoc.CreateElement ( "DirectoryPath") $ xmlSubText4 = $ xmlDoc.CreateTextNode ( "DirectoryPath user1") $ xmlSubElt4.AppendChild ($ xmlSubText4) | Out-null $ xmlElt.AppendChild ($ xmlSubElt4) | Out null $ xmlSubElt5 = $ xmlDoc.CreateElement ( "full") $ xmlSubText5 = $ xmlDoc.CreateTextNode ( "2014/12/05") $ xmlSubElt5.AppendChild ($ xmlSubText5) | Out-null $ xmlElt.AppendChild ($ xmlSubElt5) | Add the # node to the route $ root.Node.AppendChild ($ xmlElt) | Store Null # file in $ xmlDoc.Save ("C: \ temp \ ftpusers.xml")  

returns:

  & lt ;; Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; FtpUsers & gt; & Lt; Account & gt; & Lt; Account's & gt; User1 & lt; / Account & gt; & Lt; Full name & gt; Full name user1 & lt; / FullName & gt; & Lt; Password & gt; Password user1 & lt; / Password & gt; & Lt; Directory Path & gt; DirectoryPage User 1 & lt; / DirectoryPath & gt; & Lt; FULLNAME & gt; 12/05/2014 and lt; / FULLNAME & gt; & Lt; / Account & gt; & Lt; / FtpUsers & gt;  

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -