我在 webserve 上有一个 php 脚本,它显示(作为纯文本,没有 html 格式)dhcp 离开信息。输出采用以下格式,以逗号分隔:
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地址(突出显示的粗体)
非常感谢任何帮助。
谢谢。
最佳答案
如果您将下载的内容作为字符串提供(也许使用 CharlesB 建议的 WebClient),您可以将其拆分为键/值对字典,如下所示:
$data=@{}
$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]
关于powershell-3.0 - powershell invoke-webrequest 纯文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15066937/