powershell-3.0 - powershell invoke-webrequest 纯文本文件

标签 powershell-3.0

我在 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/

相关文章:

powershell - 使用 Task Scheduler 运行脚本,用户上下文完全静默,没有弹出窗口或 cmd 闪烁

ip-address - 如何获取在hyper V上运行的所有虚拟机的IP地址

csv - 如何在多个CSV文件中比较,匹配和附加多个值?

windows - PowerShell:(ConvertTo-* | Set-Content) 和 (Export-*) 之间的区别?

powershell - Get-Aduser -Filter将不接受变量

powershell - 如何在 PowerShell 中传递可选参数?

powershell-3.0 - 我似乎无法将安全字符串传递给我的 cmdlet

powershell - 创建powershell参数默认值为当前目录

powershell - Powershell属性计数

powershell - 使用 for 循环更改 pscustomobject 数组的值