Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Modifying XML with Powershell

$
0
0

I am new to modifying XML with PowerShell so please bare with me.  ;-)

You may or may not notice this is an XML file that is generate by the User Driven Installation Wizard as part of the MDT 2012 Toolkit.  (Code block below)

In the section in bold, I need to effectively duplicate those nodes dynamically based on an array of values. 

Example:  I am querying AD for a list  of "Locations" for a tech to select during the UDI Wizard.  This node is a combo box (denoted by the combo1 "data name") that I need to build prior to launching the UDI Wizard.  An example of what this would look like would be:

<DataItem>
    <Setter Property="DisplayName">MO, St. Louis | Corporate | CDT</Setter>
    <Setter Property="Value">MO, St. Louis | Corporate | CDT</Setter>
</DataItem>
<DataItem>
    <Setter Property="DisplayName">China, Dongguan | China-Dongguan | CHT</Setter>
    <Setter Property="Value">China, Dongguan | China-Dongguan | CHT</Setter>
</DataItem>

I am doing this today using WiseScript which makes it quite easy by having the keywords "replaceme" to look for, then "insert after",  however I am trying to move this functionality into PowerShell and am struggling to find a good example of managing these types of nodes.  Most likely do to my lack of solid understanding of anything other than basic XML.  :-/

I can read the XML file and get the node as shown below but have tried a thousand examples from the internet on how duplicate and re-write the XML and have failed miserably.  Hoping someone can jump start my brain on how to attack this.

$xml = [xml](gc 'C:\Tmp\NEWCOMPUTER.XML')
$xml | Select-Xml "//Data" |
Foreach {
 If ($_.Node.Name -eq 'combo1') {
            Write-Host $_
        }
}

<?xml version="1.0" encoding="utf-8"?><Wizard><DLLs><DLL Name="OSDRefreshWizard.dll" /><DLL Name="SharedPages.dll" /></DLLs><Style><Setter Property="bannerFilename">UDI_Wizard_Banner.bmp</Setter><Setter Property="title">Operating System Deployment (OSD) Wizard</Setter></Style><Pages><Page Name="BuildYourOwnPageView" DisplayName="Select Location" Type="Microsoft.Wizard.CustomPage"><Fields><Field Name="combo1" Enabled="true" Summary="Location" VarName="Location"><Default>REPLACEME</Default></Field><Field Name="label2" Enabled="true" Summary="" VarName=""><Default>Destination Location:</Default></Field><Field Name="label3" Enabled="true" Summary="" VarName=""><Default>Your selection here determines the following information:</Default></Field><Field Name="label5" Enabled="true" Summary="" VarName=""><Default>- Which Organizational Unit (OU) the machine will be placed in.</Default></Field><Field Name="label6" Enabled="true" Summary="" VarName=""><Default>- Which Timezone to apply to the Operating System.</Default></Field><Field Name="label7" Enabled="true" Summary="" VarName=""><Default>- Which Language Pack to apply (if applicable).</Default></Field><Field Name="line8" Enabled="true" Summary="" VarName=""></Field></Fields><Data Name="CustomFields"><DataItem><Setter Property="ControlType">combo</Setter><Setter Property="ID">1</Setter><Setter Property="X">10.62</Setter><Setter Property="Y">26.09</Setter><Setter Property="Width">242</Setter><Setter Property="Height">13</Setter><Setter Property="Name">combo1</Setter></DataItem><DataItem><Setter Property="ControlType">label</Setter><Setter Property="ID">2</Setter><Setter Property="X">9.7</Setter><Setter Property="Y">12.73</Setter><Setter Property="Width">64</Setter><Setter Property="Height">11</Setter><Setter Property="Name">label2</Setter><Setter Property="Label">Destination Location:</Setter></DataItem><DataItem><Setter Property="ControlType">label</Setter><Setter Property="ID">3</Setter><Setter Property="X">9.57</Setter><Setter Property="Y">104.28</Setter><Setter Property="Width">168</Setter><Setter Property="Height">13</Setter><Setter Property="Name">label3</Setter><Setter Property="Label">Your selection here determines the following information:</Setter></DataItem><DataItem><Setter Property="ControlType">label</Setter><Setter Property="ID">5</Setter><Setter Property="X">24.54</Setter><Setter Property="Y">115.79</Setter><Setter Property="Width">192</Setter><Setter Property="Height">10</Setter><Setter Property="Name">label5</Setter><Setter Property="Label">- Which Organizational Unit (OU) the machine will be placed in.</Setter></DataItem><DataItem><Setter Property="ControlType">label</Setter><Setter Property="ID">6</Setter><Setter Property="X">24.54</Setter><Setter Property="Y">126.1</Setter><Setter Property="Width">175</Setter><Setter Property="Height">10</Setter><Setter Property="Name">label6</Setter><Setter Property="Label">- Which Timezone to apply to the Operating System.</Setter></DataItem><DataItem><Setter Property="ControlType">label</Setter><Setter Property="ID">7</Setter><Setter Property="X">24.54</Setter><Setter Property="Y">135.81</Setter><Setter Property="Width">159</Setter><Setter Property="Height">11</Setter><Setter Property="Name">label7</Setter><Setter Property="Label">- Which Language Pack to apply (if applicable).</Setter></DataItem><DataItem><Setter Property="ControlType">line</Setter><Setter Property="ID">8</Setter><Setter Property="X">0</Setter><Setter Property="Y">92.13</Setter><Setter Property="Width">252</Setter><Setter Property="Height">1</Setter><Setter Property="Name">line8</Setter></DataItem></Data><Data Name="combo1"><DataItem><Setter Property="DisplayName">REPLACEME</Setter><Setter Property="Value">REPLACEME</Setter></DataItem></Data></Page><Page Name="SelectDeploymentType" DisplayName="Selection Deployment Type" Type="Microsoft.Wizard.CustomPage"><Fields><Field Name="label4" Enabled="true" Summary="" VarName=""><Default>Select your Deployment Type</Default></Field><RadioGroup Name="Group1" Locked="false" Enabled="true" Summary="Deployment Type" VarName="DeploymentType"><Default>radiobutton1</Default></RadioGroup></Fields></Page></Pages>


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>