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
<?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>
<?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
Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.
TRUSTED BY
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.