I'm trying to select a node in PowerShell which exist more then once. I declared the namespaces and try to select with XPath without success. I only get back NULL.
I'm not sure what I did wrong but I can select all settings with
$selectedNode = $xml.SelectSingleNode("//ns:settings[@pass='oobeSystem']", $NSMGR)
but I want to reach component with the attribute @name="Microsoft-Windows-Shell-Setup".
I tried different ways, but the result is always NULL.
Code snippet:
$NSMGR = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
#$NSMGR.AddNamespace("ns", $xml.DocumentElement.NamespaceURI) default
$NSMGR.AddNamespace("ns", "urn:schemas-microsoft-com:unattend")
$NSMGR.AddNamespace("xsi", "http://www.w3.org/2001/xmlSchema-instance")
#first try
$selectedNode = $xml.SelectSingleNode("//ns:settings[@pass='oobeSystem']/component[@name='Microsoft-Windows-International-Core']", $NSMGR)
$selectedNode = $xml.SelectSingleNode("//ns:settings[@pass='oobeSystem']/xsi:component[@name='Microsoft-Windows-International-Core']", $NSMGR)
$selectedNode
#second try
$selectedNode = $xml.SelectSingleNode("//ns:settings[@pass='oobeSystem']", $NSMGR)
$selectedNode = $selectedNode.SelectSingleNode("//ns:component[@name='Microsoft-Windows-Shell-Setup']", $NSMGR) #Edit to MS Shell instead of internal core for my example
$selectedNode
XML snippet:
<?xml version="1.0" encoding="utf-8" ?>
<unattend xmlns="urn:schemas-microsoft-com:unattend" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:ew="urn:schemas-microsoft-com:embedded.unattend.internal.v1">
<settings pass="specialize">
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<ComputerName>*</ComputerName>
<TimeZone>UTC</TimeZone>
</component>
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<InputLocale>0407:00000407</InputLocale>
<UILanguage>en-US</UILanguage>
<SystemLocale>en-US</SystemLocale>
<UserLocale>en-US</UserLocale>
<UILanguageFallback>de-DE</UILanguageFallback>
</component>
</settings>
<settings pass="oobeSystem">
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<OOBE>
<ProtectYourPC>3</ProtectYourPC>
<NetworkLocation>Work</NetworkLocation>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
</OOBE>
</component>
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Embedded-Core-Settings" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<HideEvaluationWarning>1</HideEvaluationWarning>
</component>
</settings>
<settings pass="windowsPE">
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<SetupUILanguage>
<UILanguage>en-US</UILanguage>
<WillShowUI>OnError</WillShowUI>
</SetupUILanguage>
<UserLocale>en-US</UserLocale>
<UILanguage>en-US</UILanguage>
<InputLocale>0407:00000407</InputLocale>
<SystemLocale>en-US</SystemLocale>
</component>
</settings>
</unattend>
$xmlNewUnattend, but select nodes from a variable$xml. Your second attempt worked fine for me when I created the namespace manager from$xmlas well.