0

I am trying to write a Script to easily customize my MDT Server / Deployment in the future.

$xml = New-Object System.Xml.XmlDocument
$xml.Load($filePath)

$xml.unattend.settings.component.UserData.FullName = "MYUSER"
$xml.unattend.settings.component.UserAccounts.AdministratorPassword.Value = "Password"


Write-Verbose $xml.OuterXml
$xml.Save($filePath)

This is the code I have scrapped together this far. I am using the Sample XML file from https://virtualfeller.com/unattend-xml-windows-10/ to debug.

The AdministratorPassword>Value node is being changed no problem but the FullName node throws me a "Object cannot be found error".

Interestingly if I use a copy of the XML used in Production neither nodes can get found even tho the "paths" are the same.

I am quite new to Powershell scripting so I'm unsure if or how to use the path expressions explained here https://www.w3schools.com/xml/xpath_syntax.asp

I have tried Indexing settings and component like described here here, sadly without success.

Thank you in advance for any help

  • That is because in that file there are multiple `` and `` nodes. You will have to specify which node(s) you are after – Theo Apr 07 '22 at 10:00

1 Answers1

1

Your XML have multiple Settings and each Settings have multplie Components.

So you need to find the correct indexes to access to the correct data. To take your example you need to modify the FullName which is in the UserData which is in the 2nd Component which is in the 1st Settings :

$xml.unattend.settings[0].component[1].UserData.FullName = "MYUSER"

A safest method is to use Xpath, but since there are different namespaces in that file, it may be a little harder! (check this answer for examples: PowerShell XML SelectNodes fails to process XPath )

Brice
  • 786
  • 4
  • 16
  • This works perfectly for the Sample file, thank you! When I try to use it on my production XML I'm getting following error "Cannot index into a null array" even tho I am using the exact same commands to index the file. My XML: https://pastebin.com/qRt972B2 – Noweapping Apr 07 '22 at 10:14
  • In your pastebin example, 1. you are missing "" . 2. the tags does not have the same structure. You only have 1 tag after . So is not an array in that case and should be directly accessed. Which litterally is you need to modify the which is in the which is the 2nd which is in which is in Just display $xml.unattend.settings.component[x] where you change the x value and you should see the difference I tried to explain. Hope it's clear :) – Brice Apr 07 '22 at 13:20
  • 1. You are absolutely right and I feel like an idiot. I wonder how that can happen with a freshly generated xml 2. Yes that actually made it clear for me thank you very much! – Noweapping Apr 08 '22 at 11:32