PowerShell通过间接php链接下载动态大小的文件(通过Invoke-WebRequest或COM对象)

时间:2019-01-09 12:30:50

标签: powershell download

我正在创建一个脚本,以从应用程序自动下载日志文件。

我设法用Invoke-WebRequest进行登录,但是卡住了下载。日志下载链接由设备在网页上提供: https://some.thing.com/common/download_logs.php

如果单击链接,将下载具有相同名称的文件(kbox_logs.tgz)。但是我不知道如何通过Invoke-WebRequest做到这一点。

当我根据WireShark执行以下操作时,我将退回到登录页面(welcome.php),然后重定向到主页(summary.php)。

Invoke-WebRequest -uri ("https://some.thing.com/adminui/settings_support.php") -WebSession $ms -Method POST

我比较了直接下载download.php(登录后)和浏览站点之间的软件包。 HTTP标头中的唯一区别是Referer属性。因此,我按如下所示手动构建了标题(cookie信息来自会话变量,因此它们是有效的),但结果却不同:我获得了登录页面。

Name                           Value
----                           -----
User-Agent                     Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
Host                           some.thing.com
cookie                         kboxid=97d0e93e002f4846ef9211d013f4b261; KACE_CSRF_TOKEN=aad3f8c254d2f25bd24e35c51d654541822080da825d6353937a7fe294216089689cc68480299b657f4fb1e9be77ac711a658a96e2df50ffc5e242b94bd9baf4
Accept-Encoding                gzip, deflate
Referer                        http://some.thing.com/adminui/settings_support.php
accept                         text/html, application/xhtml+xml, image/jxr, */*
Accept-Language                en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3

不确定标题是否正确。

=============================

我也尝试了COM对象方法。我面前有最后两个步骤:

1,单击下载链接(对不起,我只能导航,无法下载。据报道Click()方法不存在。我在登录页面中使用的方法在这里不起作用。)

2,保存文件(我想在后台运行整个程序,因此发送热键“ S”不是很理想)。

代码如下:

$username ="user"
$password ='pass'

$ie = new-object -ComObject "InternetExplorer.Application"

$init_url = "http://some.thing.com/adminui/welcome.php"
$ie.visible = $true
$ie.silent = $true
$ie.navigate($init_url)

while ($ie.busy -eq $true) {start-sleep 1}

$textbox_name = $ie.Document.getElementsBytagName("INPUT")|where {$_.name -eq 'LOGIN_name'}
$textbox_password = $ie.Document.getElementsBytagName("INPUT")|where {$_.name -eq 'LOGIN_PASSWORD'}
$button_login = $ie.Document.getElementsBytagName("BUTTON")

$textbox_name.value = $username
$textbox_password.value = $password
$button_login.item().click()

$setting_url = "http://some.thing.com/adminui/settings_control_panel.php"
$ie.navigate($setting_url)
while ($ie.busy -eq $true) {start-sleep 1}
$support_url = "http://some.thing.com/adminui/settings_support.php"
$ie.navigate($support_url)
while ($ie.busy -eq $true) {start-sleep 1}

$ie.document.getElementsByTagName("a")|where {$_.innertext -eq 'Retrieve appliance activity logs'}.item.click()
enter code here

1 个答案:

答案 0 :(得分:0)

Let's start by taking a look at the forms and methods on the page:

(Invoke-WebRequest -Uri "https://some.thing.com/adminui/settings_support.php").Forms

This should hopefully give you some information about the download_logs Action.

Can you locate a direct url to the file you are trying to download? Perhaps then you can point your Invoke-WebRequest at the file and provide an -OutFile path. Maybe you can locate this through the 'Top-level HTML view'?

If you know what the header you need to supply is, you can try this:

$headers = @{"name"="value"}  
$uri = "https://some.thing.com/adminui/settings_support.php"
$outpath = "$PSScriptRoot\kbox_logs.tgz"

Invoke-WebRequest -Headers $headers -Uri $uri -Method POST -OutFile $outpath