Reputation: 45
I have one xml file , it has few occurrence of DSCID="cbbfa194-54de-4b81-932f-eaacfc593e12"
entry.
I want to extract the first occurrence of DSCID
i.e cbbfa194-54de-4b81-932f-eaacfc593e12
and assign it to a variable for further use.
I am unable to get the result. I was trying the following syntax.
(Select-String -Path "C:\xxx.xml" -Pattern "DSCID=(.*)").Matches.Groups[0]
But as a result I am getting all the lines after DSCID
.
Upvotes: 0
Views: 449
Reputation:
Your try was close, but to grep values in between double quotes you can't use double quotes around your RegEx
(Select-String -Path ".\xxx.xml" -Pattern 'DSCID="([^"]+)"').Matches.Groups[1].Value
cbbfa194-54de-4b81-932f-eaacfc593e12
To get all matches you will need a foreach
Select-String -Path ".\xxx.xml" -Pattern 'DSCID="([^"]+)"'|ForEach-Object {
$_.Matches.Groups[1].Value
}
Upvotes: 0
Reputation: 307
Given your XML example you can retrieve your DSCID like this:
[XML]$xmlDocument = Get-Content -Path "C:\xxx.xml"
$DSCID = $xmlDocument.DistributionService.DSCID
Upvotes: 2