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 labeledspecialize
.
$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