Invoke-WebRequest不在脚本中输出RawContentStream

时间:2018-05-11 21:02:22

标签: powershell

我构建了一个脚本,该脚本查找连接到我的计算机的每个远程IP地址,并在可能的情况下返回相关信息。所有这些都可以正常工作,除了

$serv_inf.RawContentStream

在调用其变量时不返回任何输出。

这是脚本:

$h_list = Get-NetTCPConnection -AppliedSetting internet | Select-Object -Property RemoteAddress
$h_conv = $h_list.remoteaddress
foreach ($_ip in $h_conv) {
    if ($_ip -ne "127.0.0.1") {
        try {
            if (Invoke-WebRequest -Method Head -TimeoutSec 3 -DisableKeepAlive $_ip -ea Ignore) {
                echo "`n"
                echo "[+] NSLookup:`t$_ip`n"
                $x = nslookup $_ip
                foreach ($line in $x){
                    if ($line -ne "") {
                        echo " |--$line"
                    }
                }
                $serv_inf = Invoke-WebRequest -Method Head -TimeoutSec 5 -DisableKeepAlive $_ip -ea Ignore
                echo "-------------------------------------"
                echo "[+] Header Info:`n-------------------------------------"
                $serv_inf.Headers
                echo "-------------------------------------"
                echo "[+] Raw Content:`n-------------------------------------"
                $serv_inf.RawContent
                echo "-------------------------------------"
                echo "[+] Raw Content Stream:`n-------------------------------------`n"
                $serv_inf.RawContentStream
                echo "`n==============================================================================================" 
            }
            Start-Sleep -Seconds 1
        } catch {}
    }
}

返回的输出如下:

[+] NSLookup:   xx.xx.xx.xx

 |--Server:   xx.some.com
 |--Address:  xx.xx.xx.xx
 |--Name:     xx.other.some.com
 |--Address:  xx.xx.xx.xx
-------------------------------------
[+] Header Info:
-------------------------------------
Connection     close
Content-Length 0
Date           Fri, 11 May 2018 20:43:10 GMT
Server         Microsoft-IIS/8.0
X-Powered-By   ASP.NET
-------------------------------------
[+] Raw Content:
-------------------------------------

HTTP/1.1 200 OK
Connection: close
Content-Length: 0
Date: Fri, 11 May 2018 20:43:10 GMT
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET


-------------------------------------
[+] Raw Content Stream:
-------------------------------------

令我困惑的是,如果我自己运行$serv_inf.RawContentStream,它输出正常:

-------------------------------------
[+] Raw Content Stream:
-------------------------------------



CanRead      : True
CanSeek      : True
CanTimeout   : False
CanWrite     : True
Length       : 0
Capacity     : 10000
Position     : 0
ReadTimeout  :
WriteTimeout :

但如果我在此脚本中调用它,$serv_inf.RawContentStream每次都无法输出。为什么它会单独打印输出,但每次在脚本中都返回空白?

修改

我设置了一个断点,其中应该调用$serv_inf.RawContentStream并尝试自己执行它:

[DBG]: PS C:\Users\xxxxxxx\Desktop\scripts\ps>> $serv_inf.RawContentStream


CanRead      : True
CanSeek      : True
CanTimeout   : False
CanWrite     : True
Length       : 0
Capacity     : 10000
Position     : 0
ReadTimeout  :
WriteTimeout :

我看到它的内容没有任何问题,但我不确定为什么它没有显示出来。我意识到$serv_inf.BaseResponse具有类似的行为,它会单独输出但不会在脚本中输出,并且想知道它们是如何相关的。

0 个答案:

没有答案