2013-02-25 33 views
1

我有一個web服務器上顯示的(作爲純文本,沒有html格式)dhcp留下信息的PHP腳本。輸出是在下面的格式,用逗號分隔:powershell invoke-webrequest純文本文件

dnsname,leavetime,macaddress,ipaddress 

我希望能夠使用Invoke-WebRequest的PowerShell命令來分析這些信息,並剛剛獲得MAC地址。

我曾嘗試下面的代碼:

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

輸出到控制檯:

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 

取而代之的這一切,我要的是MAC地址(高亮粗體)

任何幫助非常感謝。

謝謝。

+0

請發佈您嘗試過的內容,並獲得 – CharlesB 2013-02-25 12:27:13

+0

已更新的內容。 – 2013-02-25 13:44:16

回答

0

如果您有可用下載的內容作爲字符串(可能由CharlesB建議WebClient的),你可以把它分解爲鍵/值對的字典有以下幾點:

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

那麼您可以在得到「內容」直插式分割的值與數組:

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

您應該使用System.Net.Webclient

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