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!