Bulk add entries to XML files
Learn how to bulk add entries to XML files
John Savill
December 17, 2016
1 Min Read
Q. How can I bulk add entries to XML files?
A. PowerShell can utilize the System XML Writer to read in XML files and modify while actually understanding the nodes that make up an XML file. In the script below I add a large number of entries to an NPS policy export with each consisting of two blocks of XML which use some variables for the SIDs to be granted access and some identifiers. In this example I created an entry manually before I exported the source XML containing a policy which I look for and add the new entries under its entry. I import in two hash tables which contains the list of groups (and their SIDs) that I use to create all the policies I need.
[Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | out-null[Reflection.Assembly]::LoadWithPartialName("System.Xml") | out-null$templateFile = "C:PowerShellNPSConfig_template.xml"$outputFile= "C:PowerShellNPSConfig_output.xml"$doc = [System.Xml.Linq.XDocument]::Load($templateFile);$radiusPlaceholder = $doc.Root.Element("Children").Element("Microsoft_Internet_Authentication_Service").Element("Children").Element("RadiusProfiles").Element("Children").Element("Wireless_Authentication_VLAN291___LondonLab")$networkPolicyPlaceholder = $doc.Root.Element("Children").Element("Microsoft_Internet_Authentication_Service").Element("Children").Element("NetworkPolicy").Element("Children").Element("Wireless_Authentication_VLAN291___LondonLab")$vlangrouphash = Import-Clixml -Path "C:PowerShellvlans.xml"$LABgrouphash = Import-Clixml -Path "C:PowerShellLABs.xml"foreach ($LABNameentry in $LABgrouphash.Keys){ foreach ($vlanNameentry in $vlangrouphash.Keys) {$vlanGroupSID = $vlangrouphash[$vlanNameentry].Value$LABGroupSID = $LABgrouphash[$LABNameentry].Value$vlanName = $vlanNameentry.TrimEnd("GRP")$vlanNumber = $vlanName.TrimStart("VLAN")$LABName = $LABNameentry.TrimEnd("VLGRP")$elementName = "Wireless_Authentication_$($vlanName)___$($LABName)"$name = "Wireless Authentication $($vlanName) - $($LABName)"$radiusProfilesTemplate = @"<$elementName name="$name">{00000000-0000-0000-0000-000000000000}{00000000-0000-0000-0000-000000000000}1190000000000000000000000000000001a0000000000000000000000000000005016$vlanNumber130"@$NetworkPolicyTemplate = @"<$elementName name="$name">10{00000000-0000-0000-0000-000000000000}$nameUSERNTGROUPS("$vlanGroupSID")USERNTGROUPS("$LABGroupSID")MATCH("NAS-Identifier=$LABName")2"@$radiusElement = [System.Xml.Linq.XElement]::Parse($radiusProfilesTemplate);$networkPolicyElement = [System.Xml.Linq.XElement]::Parse($NetworkPolicyTemplate);$radiusPlaceholder.AddAfterSelf($radiusElement);$networkPolicyPlaceholder.AddAfterSelf($networkPolicyElement);}}$settings = new-object System.Xml.XmlWriterSettings$settings.Indent = $true;$settings.Encoding = [System.Text.Encoding]::UTF8$xmlWriter = [System.Xml.XmlWriter]::Create($outputFile,$settings)try{ $doc.WriteTo($xmlWriter)}finally{ $xmlWriter.Close()}
About the Author
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
You May Also Like