I want to change the computerName value in my unattend.xml file.

  • Load the XML File: We start by loading the unattend.xml file into a PowerShell object for easy manipulation.
[xml]$xmlDoc = Get-Content $xmlFilePath
  • Set Up Namespace Management: Since unattend.xml uses namespaces, we prepare a namespace manager to handle queries correctly.
$ns = New-Object System.Xml.XmlNamespaceManager($xmlDoc.NameTable)
$ns.AddNamespace("ns", "urn:schemas-microsoft-com:unattend")
  • Modify the ComputerName Element: We use an XPath query to locate the ComputerName element. This element is typically nested within specific settings related to the system setup phase labeled specialize.
$computerNameNode = $xmlDoc.SelectSingleNode("//ns:settings[@pass='specialize']/ns:component/ns:ComputerName", $ns)
  • Update the ComputerName: If the element is found, we update its InnerText property with the computer name obtained from our web service. After updating, we save the changes back to the file.
if ($computerNameNode -ne $null) {
    $computerNameNode.InnerText = "MaValueVariable"
    $xmlDoc.Save($xmlFilePath)
    Write-Host "The ComputerName in unattend.xml has been updated to $($response.Computername)"
} else {
    Write-Host "No ComputerName element found in XML."
}

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.