【问题标题】:powershell invoke-webrequest plain text filepowershell invoke-webrequest 纯文本文件
【发布时间】:2013-02-25 12:24:46
【问题描述】:

我在 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地址(突出显示的粗体)

非常感谢任何帮助。

谢谢。

【问题讨论】:

  • 请发表您的尝试和收获
  • 更新了我尝试过的内容。

标签: powershell-3.0


【解决方案1】:

你应该使用System.Net.Webclient

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

【讨论】:

    【解决方案2】:

    如果您将下载的内容作为字符串提供(可能使用 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]
    

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多