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

Dear Experts,

I've created a little script for creating Windows Local users and their environment for FTP access. The script is working well except it should write the details to an XML file. The script creates only one user at a time. It stores the user's name in variable named $accountName, the full name in $fullName, the generated password in $password, the home directory in $directoryPath and the date of creation in variable $date. The code below works, but it overwrites the file every time with the details of the last created user without append it to the XML file.

#Writing out details to XML file
Write-Host -fore yellow "Writing out details to XML..."

# create a template XML to hold data
$template = @'
<FtpUsers>
    <account>
        <accountName></accountName>
        <fullName></fullName>
        <password></password>
        <directoryPath></directoryPath>
        <date></date>
    </account>
</FtpUsers>
'@

$template | Out-File C:\ProgramData\InstanceLogFiles\FtpUsers.xml -encoding UTF8

# load template into XML object
$xml = New-Object xml
$xml.Load("C:\ProgramData\InstanceLogFiles\FtpUsers.xml")

# grab template user
$newuser = (@($xml.FtpUsers.account)[0]).Clone()

# use template to add local user accounts to xml 
$newuser = $newuser.clone()
$newuser.accountName = $accountName
$newuser.fullName = $fullName
$newuser.password = $password$newuser.directoryPath = $directoryPath
$newuser.date = $date
$xml.FtpUsers.AppendChild($newuser) > $null

# remove users with undefined name (remove template)
$xml.FtpUsers.account | 
Where-Object { $_.accountName -eq "" } | 
ForEach-Object  { [void]$xml.FtpUsers.RemoveChild($_) }

# save xml to file
$xml.Save("C:\ProgramData\InstanceLogFiles\FtpUsers.xml")
Write-Host -fore yellow "...Done!"

I see that the "$template..." part writes over the file every time but:

  • I tried to put the "use template..." section into a for cycle without success.
  • I tried to write the template to a separate file, but if it not exists, the script gives error.

Would somebody please help me out where is the problem and how should I code it right?

Thank you in advance!

May 6th, 2014 7:33am

Hi ketyosz,

Sorry for the delay.

" $xml.Save("C:\ProgramData\InstanceLogFiles\FtpUsers.xml") "

Method Save()  needs a existent file .

Did you try to get the content of the Ftpuser.xml then append it to a new file ?

I mean to prepare a new xml file and add one command at the end of your code :

get-content C:\ProgramData\InstanceLogFiles\FtpUsers.xml >> c:\test.xml

Best Regards

Elton Ji

Free Windows Admin Tool Kit Click here and download it now
May 9th, 2014 11:07am

Where are you getting your accountname, fullname, password, directorypath, and date variables?

Just taking a brief glance at this it doesn't look like you have any sort of loop setup to process multiple users.  Maybe you could try something like this (making large assumptions about the source of your data):

$magic = Get-DataMagically
Foreach ($user in $magic) {

  # grab template user
  $newuser = (@($xml.FtpUsers.account)[0]).Clone()

  # use template to add local user accounts to xml 
  $newuser = $newuser.clone()
  $newuser.accountName = $user.accountName
  $newuser.fullName = $user.fullName
  $newuser.password = $user.password
  $newuser.directoryPath = $user.directoryPath
  $newuser.date = $date
  $xml.FtpUsers.AppendChild($newuser) > $null
}

It also looks like you're cloning a clone, but I'm not an expert on powershell xml so maybe there's a reason for it.  Wouldn't be a problem, just seems like unnecessary commands.

May 9th, 2014 12:01pm

Hi ketyosz,

How are things going ?

Best Regards

Elton Ji

Free Windows Admin Tool Kit Click here and download it now
May 13th, 2014 4:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics