Powershell: XPath cannot select when element has "xmlns" tag? - powershell

I've got a very simple xml, as below:
<?xml version="1.0" encoding="utf-8"?>
<First>
<Second>
<Folder>today</Folder>
<FileCount>10</FileCount>
</Second>
<Second>
<Folder>tomorrow</Folder>
<FileCount>90</FileCount>
</Second>
<Second>
<Folder>yesterday</Folder>
<FileCount>22</FileCount>
</Second>
</First>
Then I have a powershell script to select "Folder" element:
[xml]$xml=Get-Content "D:\m.xml"
$xml.SelectNodes("//Folder")
It outputs:
#text
-----
today
tomorrow
yesterday
No problem. But if I change the xml file to add "xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to "First" like below:
<?xml version="1.0" encoding="utf-8"?>
<First xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Second>
<Folder>today</Folder>
<FileCount>10</FileCount>
</Second>
<Second>
<Folder>tomorrow</Folder>
<FileCount>90</FileCount>
</Second>
<Second>
<Folder>yesterday</Folder>
<FileCount>22</FileCount>
</Second>
</First>
Then, my powershell script outputs nothing.
Why? How to change my powershell script to support this xmlns?
Thanks a lot.

What you added is default namespace. Unlike prefixed namespace, descendant elements inherit ancestor default namespace implicitly, unless otherwise specified (using explicit prefix or local default namespace that points to different URI).
To select element in namespace, you'll need to define prefix that point to the namespace URI and use that prefix properly in your XPath, for example :
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI)
$xml.SelectNodes("//d:Folder", $ns)

Related

Deploying communities using package.xml

I am trying to retrieve Salesforce communities names 'XXX Community' from one org into VS Code and subsequently want to deploy it into another org.
However, when I use the sfdx force:source:retrieve -u DemoMaster -x ./package.xml command, for the below package.xml file, I get the following error:
Entity of type 'CustomSite' named 'XXX_Community_Test' cannot be found
Entity of type 'SiteDotCom' named 'XXX_Community_Test1' cannot be found
Only the network and network branding elements got retrieved.
The package.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>XXX Community</members>
<name>Network</name>
</types>
<types>
<members>XXX_Community_Test</members>
<name>CustomSite</name>
</types>
<types>
<members>XXX Community_Test1</members>
<name>SiteDotCom</name>
</types>
<types>
<members>cbXXX_Community</members>
<name>NetworkBranding</name>
</types>
<version>48.0</version>
</Package>
Any help appreciated!
D

Update Build number in App config xml file on build pipeline

I have a build pipeline in Azure DevOps, I need to update the build number in my apconfig exe file that will be $(Build.BuildNumber).
I just tried this way:
Adding a variable name = BuildNumber value = $(Build.BuildNumber).
And in my apconfig.exe file have a key same like <add key="BuildNumber" value="1812201901" />.
Why I have tried like this way: thinking like it will update in the config file if variable name match with the key.
But it is not working. can anyone please help? I have just started in CI/CD.
Update Build number in App config xml file on build pipeline
Just like the Shayki said, using the Replace Tokens extension should be the directly way to resolve this issue.
But since you need to request to get this extension, as workaround, you could also use power shell scripts to resolve this issue, you can check below my test powershell scripts:
$currentDirectory = [IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
$appConfigFile = [IO.Path]::Combine($currentDirectory, 'App.config')
$appConfig = New-Object XML
$appConfig.Load($appConfigFile)
foreach($BuildNumber in $appConfig.configuration.add)
{
'name: ' + $BuildNumber.name
'BuildNumber: ' + $BuildNumber.value
$BuildNumber.value = '123456789'
}
$appConfig.Save($appConfigFile)
As result, the app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<add key="BuildNumber" value="123456789" />
</configuration>
Note: Set the powershell scripts at the same folder of the app.config file.
Hope this helps.
You can use the Replace Tokens extension and in the apconfig.exe file put this:
<add key="BuildNumber" value="__BuildNumber__" />
Configure the task to search variables with __ prefix and suffix:
Now the value will be replaced with the value of the BuildNumber variable you configured (equal to Build.BuildNumber).

Xpath compilation for Child element with namespace + Getting error - Prefix must resolve to a namespace: ns2

Sample
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<ns2:bookStore xmlns:ns2="http://bookstore.com/schemes">
<ns2:book id="1">
<ns2:name>Data Structure</ns2:name>
</ns2:book>
<ns2:book id="2">
<ns2:name>Java Core</ns2:name>
</ns2:book>
</ns2:bookStore>
</Root>
Java code
XPathExpression expr =
xpath.compile("//Root/ns2:bookStore/ns2:book/ns2:name/text()");
I get Prefix must resolve to a namespace: ns2 error with above xml with Root
tag. By removing the Root tag I'm able to parse the data. Any help would be appreciated without modifying the XML file.

How to read xml value with Powershell

<?xml version="1.0" encoding="utf-8"?>
<Foo>
<Tier Path="\\10.10.44.61\Case Study\" />
<Force Path="\\10.10.44.61\Orto Study" Atomic="False" />
</Foo>
How can I read in powershell Tier Path value?
I am expecting to get the value of \\10.10.44.61\Case Study\
$xml = '<?xml version="1.0" encoding="utf-8"?>
<Foo>
<Tier Path="\\10.10.44.61\Case Study\" />
<Force Path="\\10.10.44.61\Orto Study" Atomic="False" />
</Foo>'
([xml]$xml).Foo.Tier.Path
As long as you are using an xml type (which I do by casting) you can reference the path directly as members of the object.

Change VMConfig file using azure powershell

I have a vmConfig file. I want to change subnet and IP Address as I want to create a new VM out of config file in new subnet, rest all configurations no need to be changed. I can manually edit xml file content but I want to do it through powershell so that I can have an automated process for everything.
Here is the sample vmConfig xml-
<?xml version="1.0" encoding="utf-8"?>
<PersistentVM xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ConfigurationSets>
<ConfigurationSet xsi:type="NetworkConfigurationSet">
<ConfigurationSetType>NetworkConfiguration</ConfigurationSetType>
<InputEndpoints>
<InputEndpoint>
<LocalPort>5986</LocalPort>
<Name>PowerShell</Name>
<Port>64929</Port>
<Protocol>tcp</Protocol>
<Vip>191.237.20.225</Vip>
<EnableDirectServerReturn>false</EnableDirectServerReturn>
<IdleTimeoutInMinutes xsi:nil="true" />
</InputEndpoint>
</InputEndpoints>
<SubnetNames>
<string>mysubnet</string>
</SubnetNames>
<StaticVirtualNetworkIPAddress>12.13.14.15</StaticVirtualNetworkIPAddress>
<PublicIPs />
<NetworkInterfaces />
I am interested changing only IP Address and Subnet.
This is basically xml parsing using powershell. I hope this should work for you-
$path = 'C:\myFolder\XmlVM.xml'
[xml]$myXML = Get-Content $path
$myXML.PersistentVM.ConfigurationSets.ConfigurationSet.SubnetNames.string="MYNEWSUBNET"
$myXML.PersistentVM.ConfigurationSets.ConfigurationSet.StaticVirtualNetworkIPAddress="10.11.14.115"
$myXML.Save($path)