Powershell throwing Property '...' cannot be found on this object when trying to edit unattended.xml

853 views Asked by At

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

1

There are 1 answers

3
Brice On BEST ANSWER

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 )