Avatar of DevSupport
DevSupport
 asked on

parsing xml using powershell

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
PowershellXML* Parsing

Avatar of undefined
Last Comment
DevSupport

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
footech

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.
oBdA

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 ...).
footech

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
DevSupport

ASKER
Thank You so much OBdA!!
DevSupport

ASKER
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.