We help IT Professionals succeed at work.

parsing xml using powershell

DevSupport
DevSupport asked
on
510 Views
Last Modified: 2017-03-24
Hi Experts,

I am parsing an xml file and I'm trying to get some values from it remotely.

Attached is the xml.

I can get the url from context file as follows:

$tchome = c:\app\location
$pointr = someuser2

$output = Invoke-Command -ComputerName appserver -ArgumentList ($tchome,$pointr) -ScriptBlock {Param($tchome, $pointr); $xmldata = [xml](Get-Content $tchome\conf\context.xml); ($xmldata.SelectSingleNode("//Resource[@username='$pointr']")).url}

Is there a way by which I can get $output as databasename - db2

I would also like to get the database name after // in this case DBServer2. Is it possible?

Thanks
DevSupport
context.xml
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2014

Commented:
You're pretty much down to just parsing out the string (nothing real programmatic about it).  So, taking the string
jdbc:sqlserver://DBServer2;databaseName=db2;SelectMethod=cursor;SendStringParametersAsUnicode=false
Say it's assigned to a variable
$urlValue = "jdbc:sqlserver://DBServer2;databaseName=db2;SelectMethod=cursor;SendStringParametersAsUnicode=false"
$urlValuesplit = $urlValue -split ";"
$DBServer = ($urlValuesplit[0] -split "//")[-1]
$DBName = $urlValuesplit[1]     # or maybe  $DBName = ($urlValuesplit[1] -split "=")[-1]

Open in new window

More sophisticated regex could be utilized to try to parse/match the entire string into it's components, but that could be overkill for your needs.

Edit:  I see oBdA posted while I was typing.  My approach is pretty similar, but I'll leave the post just so you can examine differences.  No need for any points.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
On a side note: why go through all the trouble invoking remote PS, instead of just pulling the file directly?
$xmldata = [xml](Get-Content -Path "\\appserver\$($tchome.Replace(':', '$'))\conf\context.xml")
$output = ($xmldata.SelectSingleNode("//Resource[@username='$pointr']")).url

Open in new window


footech,
there's no specific order for the elements of a connection string, so relying on the order in this particular xml might break the script as soon as the configuration is changed.
That's why I chose the approach with the ConvertFrom-StringData (feeling too lazy for a regex ...).
CERTIFIED EXPERT
Top Expert 2014

Commented:
I'm glad you used the ConvertFrom-StringData method.  I considered it, but didn't pursue it when I saw the first element in the string didn't fit the format (which you worked around by using the Where-Object filter).  Not knowing how the elements could change is also the reason I didn't pursue a regex.

Author

Commented:
Thank You so much OBdA!!

Author

Commented:
I am using invoke command because I am passing credentials too as the user who is executing the powershell doesn't have access to the share folder.