2
votes

I have a php script on a webserve that displays (as plain text, no html formatting) dhcp leave information. The output is in the following format, seperated by commas:

dnsname,leavetime,macaddress,ipaddress

i want to be able to parse this information using the invoke-webrequest powershell command and just get the mac address.

I have tried the following code:

invoke-webrequest "http://www.xxx.xxx"

The output to the console is:

StatusCode        : 200
StatusDescription : OK
Content           : *hostname*,in 7 days ,**Mac Address**,*ip address*

RawContent        : HTTP/1.1 200 OK
                Keep-Alive: timeout=2, max=100
                Connection: Keep-Alive
                Content-Length: 50
                Content-Type: text/plain; charset=ISO-8859-1
                Date: Mon, 25 Feb 2013 12:04:33 GMT
                Server: Apache

                ega008...
Forms             :
Headers           : {[Keep-Alive, timeout=2, max=100], [Connection, Keep-Alive],        [Content-Length, 50], [Content-Type,
                text/plain; charset=ISO-8859-1]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        :
RawContentLength  : 50

Instead of all this, all I want is the mac address (Highlighted bold)

Any help greatly appreciated.

thanks.

2
please post what you've tried and what you getCharlesB
updated with what I've tried.Jeremy Walmsley

2 Answers

2
votes

You should use System.Net.Webclient

$webclient = new-object System.Net.WebClient
$webpage = $webclient.DownloadString("http://www.xxx.xxx")
1
votes

If you have the downloaded content available as a string (perhaps with the WebClient as suggested by CharlesB), you can split it in to a dictionary of key/value-pairs with the following:

$data=@{}
$webpage.split("`n") |% { $record=$_.split(":"); if($record[1]) { $data[$record[0].trim()]=$record[1].trim() }}

then you can get the value of the "content"-line split in to an array with:

$content = $data["Content"].split(",")
#
# retrieve the mac with:
$content[2]