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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
$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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You're pretty much down to just parsing out the string (nothing real programmatic about it).  So, taking the string
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.
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

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 ...).
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

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.
DevSupportAuthor Commented:
Thank You so much OBdA!!
DevSupportAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.