I suspect your problem is with namespaces. If you remove the "xmlns=...." from the WLANProfile element your search works.
Powershell XPATH seems to be not working
I am using Wifi profile xml files as an example.
In the below code if i use xpath as $XPath = "//*"
I get the list of all nodes in the xml.
But if I specify specific node and all nodes under it with $XPath = "//WLANProfile//*"
, I get nothing.
Is there something wrong I am doing or does Powershell xpath work differently
$path = "c:\something\wifi.xml"
$XPath = "//WLANProfile//*"
$xmlContent = [xml](Get-Content $path)
#This does not work unless $XPath is changed to "//*"
Select-Xml -Xml $xmlContent -XPath $XPath | Select-Object -ExpandProperty Node
#Just testing another way and same here only works when $XPath is equal to "//*"
$xmlContent.SelectNodes("//*")
XML
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>Guest</name>
<SSIDConfig>
<SSID>
<hex>436170552D477485693</hex>
<name>dd</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>manual</connectionMode>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>true</protected>
<keyMaterial>secretstring</keyMaterial>
</sharedKey>
</security>
</MSM>
<MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
<enableRandomization>false</enableRandomization>
</MacRandomization>
</WLANProfile>
Thanks
3 answers
Sort by: Most helpful
-
-
Limitless Technology 44,221 Reputation points
2023-03-21T15:41:16.0433333+00:00 Hello there,
Maybe the issue is with the xml namespace. Try using the -namespace to get the correct xpath query.
Test this by removing the xmlns from a feature.xml file and running your command.
The below thread discusses the same issue and you can try out some troubleshooting steps from this and see if that helps you to sort the Issue.
Hope this resolves your Query !!
--If the reply is helpful, please Upvote and Accept it as an answer--
-
RT-7199 511 Reputation points
2023-03-21T22:03:25.86+00:00 @Limitless Technology and @Rich Matheisen
Yes this seems to be caused by namespace as removing the line xmlns="http://www.microsoft.com/networking/WLAN/profile/v1" does make it work.
And I tried using the -Namespace as below and it doesn't work
Select-Xml '//WLANProfile//*' -Namespace @{'xml'='http://www.microsoft.com/networking/WLAN/profile/v1'} -xml $xmlContent
when namespace is manually removed from file
Node Path Pattern
---- ---- -------
name InputStream //WLANProfile//*
SSIDConfig InputStream //WLANProfile//*
SSID InputStream //WLANProfile//*
hex InputStream //WLANProfile//*
name InputStream //WLANProfile//*
connectionType InputStream //WLANProfile//*
connectionMode InputStream //WLANProfile//*
MSM InputStream //WLANProfile//*
security InputStream //WLANProfile//*
authEncryption InputStream //WLANProfile//*
authentication InputStream //WLANProfile//*
encryption InputStream //WLANProfile//*
useOneX InputStream //WLANProfile//*
sharedKey InputStream //WLANProfile//*
keyType InputStream //WLANProfile//*
protected InputStream //WLANProfile//*
keyMaterial InputStream //WLANProfile//*
MacRandomization InputStream //WLANProfile//*
enableRandomization InputStream //WLANProfile//*
I think i am almost there if you help me with that. Thanks