0

I have the following xml (Main.xml):

    <AssignedAccessConfiguration xmlns="http://schemas.microsoft.com/AssignedAccess/2017/config">
  <Profiles>
    <Profile Id="">
      <AllAppsList>
        <AllowedApps />
      </AllAppsList>
      <StartLayout />
      <Taskbar ShowTaskbar="false" />
      <Configs>
        <Config>
          <Account>
            <DefaultProfile Id="" />
          </Account>
        </Config>
      </Configs>
    </Profile>
  </Profiles>
</AssignedAccessConfiguration>

I need to add content between the opening and closing "StartLayout" tag with Powershell. To look like this:

<StartLayout>
        <![CDATA[<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
                      <LayoutOptions StartTileGroupCellWidth="6" />
                      <DefaultLayoutOverride>
                        <StartLayoutCollection>
                          <defaultlayout:StartLayout GroupCellWidth="6">
                            <start:Group Name="Group1">
                              <start:Tile Size="4x4" Column="0" Row="0" AppUserModelID="Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic" />
                              <start:Tile Size="2x2" Column="4" Row="2" AppUserModelID="Microsoft.ZuneVideo_8wekyb3d8bbwe!Microsoft.ZuneVideo" />
                              <start:Tile Size="2x2" Column="4" Row="0" AppUserModelID="Microsoft.Windows.Photos_8wekyb3d8bbwe!App" />
                              <start:Tile Size="2x2" Column="4" Row="4" AppUserModelID="Microsoft.BingWeather_8wekyb3d8bbwe!App" />
                              <start:Tile Size="4x2" Column="0" Row="4" AppUserModelID="Microsoft.WindowsCalculator_8wekyb3d8bbwe!App" />
                            </start:Group>
                            <start:Group Name="Group2">
                              <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Accessories\Paint.lnk" />
                              <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%APPDATA%\Microsoft\Windows\Start Menu\Programs\Accessories\Notepad.lnk" />
                            </start:Group>
                          </defaultlayout:StartLayout>
                        </StartLayoutCollection>
                      </DefaultLayoutOverride>
                    </LayoutModificationTemplate>
                ]]>
</StartLayout>

I already have the "StartLayout" content called "Layout.xml" it was generated as a separate XML:

<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
  <DefaultLayoutOverride LayoutCustomizationRestrictionType="OnlySpecifiedGroups">
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="6">
        <start:Group Name="CoWorkerApps">
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationID="{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\cmd.exe" />
          <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationID="Microsoft.Windows.Shell.RunDialog" />
          <start:DesktopApplicationTile Size="2x2" Column="4" Row="0" DesktopApplicationID="{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe" />
          <start:DesktopApplicationTile Size="2x2" Column="0" Row="2" DesktopApplicationID="{6D809377-6AF0-444B-8957-A3773F02200E}\Windows NT\Accessories\wordpad.exe" />
        </start:Group>
        <start:Group Name="CustomerApps">
          <start:Tile Size="2x2" Column="0" Row="0" AppUserModelID="Microsoft.WindowsCalculator_8wekyb3d8bbwe!App" />
          <start:Tile Size="2x2" Column="2" Row="0" AppUserModelID="Microsoft.Messaging_8wekyb3d8bbwe!x27e26f40ye031y48a6yb130yd1f20388991ax" />
          <start:Tile Size="2x2" Column="4" Row="0" AppUserModelID="Microsoft.XboxApp_8wekyb3d8bbwe!Microsoft.XboxApp" />
          <start:Tile Size="2x2" Column="0" Row="2" AppUserModelID="Microsoft.Getstarted_8wekyb3d8bbwe!App" />
        </start:Group>
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
</LayoutModificationTemplate>

I add the CDATA:

C:\Windows\system32> [xml]$xml =(gc "C:\StartMenu\LayoutFIX.xml")
PS C:\Windows\system32> $test = ($xml.CreateCDataSection($testMe).OuterXml)
PS C:\Windows\system32> $test
<![CDATA[<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schem
as.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"><DefaultLayou
tOverride LayoutCustomizationRestrictionType="OnlySpecifiedGroups"><StartLayoutCollection><defaultlayout:StartLayout GroupCellWidth="
6"><start:Group Name="CustomerApps" /><start:Group Name="CoWorkerApps" /></defaultlayout:StartLayout></StartLayoutCollection></Defaul
tLayoutOverride></LayoutModificationTemplate>]]>

The question is: How can I add the content "Layout.xml" to the "StartLayout" tags in Main.xml, preferably with powershell?

Just for test, I have been playing with:

element = $xmlDocument.CreateElement("RootElement")

$subElement = $xmlDocument.CreateElement("SubElement")

#Note the XML can only contain Text (String) based values. Which means that my $null variable will not work!!!

$subElement.SetAttribute($null, $AddStartLayout)
#$subElement.SetAttribute("SomeValue", "True")    

$element.AppendChild($subElement)

#This appends my newly created Sub Element to the document RootElement

#Creates a base element called RootElement

$xmlDocument.AppendChild($element)

#Append the XML RootElement to the XML Document

$xmlDocument.Save("$($LayoutPath)\$LayoutXMLFile")

The method SetAttribute() needs two string values. While the child-element "StartLayout" in Main.xml only has the content (the Layout.xml with the CDATA).

Does anyone knows how to inject the "StartLayout" child-element with just the data (any language will do. I will just have to try to translate it into powershell).

Really appreciate any tip along the way.

pixelmybit
  • 113
  • 2
  • 3
  • 9

1 Answers1

0

This should do the trick:

[xml]$main = @"
   <AssignedAccessConfiguration xmlns="http://schemas.microsoft.com/AssignedAccess/2017/config">
  <Profiles>
    <Profile Id="">
      <AllAppsList>
        <AllowedApps />
      </AllAppsList>
      <StartLayout />
      <Taskbar ShowTaskbar="false" />
      <Configs>
        <Config>
          <Account>
            <DefaultProfile Id="" />
          </Account>
        </Config>
      </Configs>
    </Profile>
  </Profiles>
</AssignedAccessConfiguration>
"@

[xml]$startLayout = @"
<StartLayout>
        <![CDATA[<LayoutModificationTemplate xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
                      <LayoutOptions StartTileGroupCellWidth="6" />
                      <DefaultLayoutOverride>
                        <StartLayoutCollection>
                          <defaultlayout:StartLayout GroupCellWidth="6">
                            <start:Group Name="Group1">
                              <start:Tile Size="4x4" Column="0" Row="0" AppUserModelID="Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic" />
                              <start:Tile Size="2x2" Column="4" Row="2" AppUserModelID="Microsoft.ZuneVideo_8wekyb3d8bbwe!Microsoft.ZuneVideo" />
                              <start:Tile Size="2x2" Column="4" Row="0" AppUserModelID="Microsoft.Windows.Photos_8wekyb3d8bbwe!App" />
                              <start:Tile Size="2x2" Column="4" Row="4" AppUserModelID="Microsoft.BingWeather_8wekyb3d8bbwe!App" />
                              <start:Tile Size="4x2" Column="0" Row="4" AppUserModelID="Microsoft.WindowsCalculator_8wekyb3d8bbwe!App" />
                            </start:Group>
                            <start:Group Name="Group2">
                              <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Accessories\Paint.lnk" />
                              <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%APPDATA%\Microsoft\Windows\Start Menu\Programs\Accessories\Notepad.lnk" />
                            </start:Group>
                          </defaultlayout:StartLayout>
                        </StartLayoutCollection>
                      </DefaultLayoutOverride>
                    </LayoutModificationTemplate>
                ]]>
</StartLayout>
"@

$main.ChildNodes.Profiles.Profile.AppendChild( $main.ImportNode( $startLayout.StartLayout, $true ) )
f6a4
  • 1,684
  • 1
  • 10
  • 13
  • Hej, Really appreciate the example I will definitly try it out. One thing though, I am aiming to generate a script that will dynamically fetch variables that will populate the xml (both the inner and outer xml). – pixelmybit Jul 05 '18 at 14:28