parsing xml using powershell

DevSupport
DevSupport used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
For example like this:
$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
}
$Connection = $output.Split(';')
$DBServer = ($Connection | Where-Object {$_ -like '*:sqlserver:*'}).Split('/')[-1]
$DBName = (($Connection | Where-Object {$_.Contains('=')}) -join "`r`n" | ConvertFrom-StringData)['databaseName']
Write-Host "DBServer: $($DBServer), DBName: $($DBName)"

Open in new window

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.
Most Valuable Expert 2018
Distinguished 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 ...).
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial