Kasper Katzmann
asked on
Append data to an XML file
I have a script which in the end creates an XML file. The XML file is supposed to contain information about more than 200 users, but when I run the script the XML file only contains information about one user.
How do I create an XML file that contains all users and not just the last in the array?
The part that creates the XML file starts at line 75.
This is what I get
This is what I want
How do I create an XML file that contains all users and not just the last in the array?
The part that creates the XML file starts at line 75.
Function ConvertTo-CustomXml($ADUser) {
$SID = [STRING]$ADUser.SID
$SID = $SID.Split("-")[-1]
$xml = [xml]"<User />"
$NodeUser = $xml.SelectSingleNode('/User')
$NewNode = $NodeUser.AppendChild($xml.CreateElement("Name"))
$NewNode.InnerText = $ADUser.samAccountName
$NewNode = $NodeUser.AppendChild($xml.CreateElement("Full_name"))
$NewNode.InnerText = $ADUser.Name
$NewNode = $NodeUser.AppendChild($xml.CreateElement("Comment"))
$NewNode.InnerText = $ADUser.description
$NewNode = $NodeUser.AppendChild($xml.CreateElement("Id"))
$NewNode.InnerText = $SID
$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountType"))
$NewNode.InnerText = 'Normal'
$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsEnabled"))
$NewNode.InnerText = If ($ADUser.Enabled) {1} Else {0}
$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsPasswdRequired"))
$NewNode.InnerText = If ($ADUser.PasswordNotRequired) {0} Else {1}
$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsPasswdCantChange"))
$NewNode.InnerText = If ($ADUser.CannotChangePassword) {1} Else {0}
$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsPasswdDontExpire"))
$NewNode.InnerText = If ($ADUser.PasswordNeverExpires) {1} Else {0}
$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsCurrentlyLockedOut"))
$NewNode.InnerText = If ($ADUser.LockedOut) {1} Else {0}
$NewNode = $NodeUser.AppendChild($xml.CreateElement("GroupIds"))
$NewNode.InnerText = (@($ADUser.MemberOf | Get-ADGroup | Select-Object -ExpandProperty Sid | Select-Object -ExpandProperty Value | ForEach-Object {$_.Split("-")[-1] })) -join ','
Return $xml
}
#region GROUPS
$groups = “CU2701-SG-Proteus_BLH”,
”CU2701-SG-Proteus_BON”,
”CU2701-SG-Proteus_FYN”,
”CU2701-SG-Proteus_HIM”,
”CU2701-SG-Proteus_HST”,
”CU2701-SG-Proteus_KJY”,
”CU2701-SG-Proteus_MJY”,
”CU2701-SG-Proteus_NSJ”,
”CU2701-SG-Proteus_OSJ”,
”CU2701-SG-Proteus_SDJ”,
”CU2701-SG-Proteus_SHL”,
”CU2701-SG-Proteus_STS”,
”CU2701-SG-Proteus_THY”,
”CU2701-SG-Proteus_TRE”,
”CU2701-SG-Proteus_VAD”,
”CU2701-SG-Proteus_VJY”,
”CU2701-SG-Proteus_VSJ”,
”CU2701-SG-Proteus_VSY”,
”CU2701-SG-Proteus_DRC”,
”CU2701-SG-SNS_Proteus_Adm”,
”CU2701-SG-SNS_Proteus_Godkender”,
”CU2701-SG-SNS_Proteus_Markhold”,
”CU2701-SG-SNS_Proteus_Konsulent”,
”CTX_Proteus”,
”CTX_Natura2000”,
”NST_Natura2000”
#endregion
#region EXTRACT GROUP MEMBERS
foreach($group in $groups)
{
$members = Get-ADGroupMember $group | select samAccountName
$users = $users + $members
}
$users = $users | select samAccountName -Unique
#endregion
#region EXPORT USER DATA TO XML
$data = @()
foreach($user in $users)
{
$usr = $user.samAccountName
$ud = Get-ADUser `
-Identity $usr `
-Properties description,
SID,
description,
Name,
samAccountName,
ObjectClass,
Enabled,
PasswordNotRequired,
CannotChangePassword,
PasswordNeverExpires,
LockedOut,
MemberOf
#ConvertTo-CustomXml is a function
$XMLdata = ConvertTo-CustomXml -ADUser $ud
$data = $data += $XMLdata
}
$XMLdata.Save("C:\temp\proteus.xml")
#endregion
This is what I get
<?xml version="1.0"?>
-<User>
<Name>U76454</Name>
<Full_name>Poul Young</Full_name>
<Comment> </Comment>
<Id>35953</Id>
<AccountType>Normal</AccountType>
<AccountPropsEnabled>1</AccountPropsEnabled>
<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>
This is what I want
<?xml version="1.0"?>
-<User>
<Name>U76454</Name>
<Full_name>Poul Young</Full_name>
<Comment> </Comment>
<Id>35953</Id>
<AccountType>Normal</AccountType>
<AccountPropsEnabled>1</AccountPropsEnabled>
<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>
-<User>
<Name>U09323</Name>
<Full_name>Kim Carnes</Full_name>
<Comment> </Comment>
<Id>35987</Id>
<AccountType>Normal</AccountType>
<AccountPropsEnabled>1</AccountPropsEnabled>
<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>
-<User>
<Name>U12345</Name>
<Full_name>John Doe</Full_name>
<Comment> </Comment>
<Id>35913</Id>
<AccountType>Normal</AccountType>
<AccountPropsEnabled>1</AccountPropsEnabled>
<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>
ASKER
You are right - The customer keeps calling it XML, so I tend to say the same.
For anyone interested, this is my earlier question: Customizing XML with Powershell
The customer want it in this particular format, but basically he just want a a textfile named .xml
For anyone interested, this is my earlier question: Customizing XML with Powershell
The customer want it in this particular format, but basically he just want a a textfile named .xml
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
A valid XML document requires a root node.
Since (as of your earlier question) the XML definition is not up to you, you need to first find out which format you need to incorporate multiple users, so that the output you produce can be used. The former specification was only for a single user.