0

I am writing a powershell script to notify me if two users are using the same username but different IP when playing from a plex server.

I managed to get an xml display of current connections that are steaming at that time.

What I need to do is to come up with a reg expression where I extract the User id and IP address and then I can do a search to see if there are duplicate user ID and that has different IP.

I managed to find the regex for IP addresses which is '\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b’

But having a hard time to extract the User Id from it as well. Note the User ID will always be numbers but no set limit.

Here is the example of the data

<User id="13456" title="usersmith" />
<Player address="2.2.2.2" device="Windows" machineIdentifier="a9b222ef940" 
Peter
  • 11
  • 3
  • For User ID: `^ – heemayl May 10 '16 at 11:40
  • is there anyway to only take the ID number and not the User id as well? Also correction I tried the syntax on the full data and it didnt find anything but found it when only using the above data, here is the full string for User id – Peter May 10 '16 at 11:46
  • 3
    You have an xml document, so use an xml parser. Regex is the wrong tool for this job. – arco444 May 10 '16 at 11:48

1 Answers1

1

I would parse this as xml, ex:

#$xml = [xml](Get-Content myfile.xml)
#$xml = [xml](Invoke-WebRequest ... whatever).Content
$xml = [xml]@"
<?xml version="1.0" encoding="utf-8"?>
<root>
<User id="13456" title="usersmith" />
<Player address="2.2.2.2" device="Windows" machineIdentifier="a9b222ef940" />
</root>
"@

$xml.root.user.id

but if you really want regex, try @heemayl's solution except the start-of-line ^-anchor which might not fit your real xml-data (in that case you've provided bad sampledata). Ex:

if('User id <User id="1354" thumb="plex.tv/users/a51d"; title="bob" />' -match '<User\s+id="([^"]+)"') { $Matches[1] }
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • I don't have to use regex, as long I able to get the string in a variable so I can do a comparison. – Peter May 10 '16 at 16:31
  • Either way I've shown you both solutions. You choose :-) – Frode F. May 10 '16 at 16:32
  • Going to give it a try later tonight, just finishing from work, will report back. thanks – Peter May 10 '16 at 22:06
  • so I gave it a try and I think the example I was providing didnt really have everything, so I am still having issues using the xml parse. Here is the full data which I want to loop through and get the User ID and IP address. – Peter May 11 '16 at 19:10
  • Did you try yourself? The "property-path" for that is `$xml.MediaContainer.Video.User.id`. You just need to follow the nodes until you reach the target... :-) – Frode F. May 11 '16 at 19:25