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
DevSupportAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

oBdACommented:
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

1

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
footechCommented:
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.
1
oBdACommented:
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 ...).
1
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

footechCommented:
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.
1
DevSupportAuthor Commented:
Thank You so much OBdA!!
0
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.
0
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
Powershell

From novice to tech pro — start learning today.