【问题标题】:PowerShell to download a file with dynamic size via an indirect php link (by Invoke-WebRequest or COM object)PowerShell 通过间接 php 链接(通过 Invoke-WebRequest 或 COM 对象)下载具有动态大小的文件
【发布时间】:2019-01-10 16:21:12
【问题描述】:

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

我设法使用 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 标头的唯一区别是引用属性。所以我手动构建标题如下(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

【问题讨论】:

  • 也许this blog 可以帮助你。它描述了使用 PowerShell 下载文件的三种方法。

标签: powershell download


【解决方案1】:

我们先来看看页面上的表单和方法:

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

这有望为您提供有关 download_logs 操作的一些信息。

您能找到您要下载的文件的直接网址吗?也许您可以将您的Invoke-WebRequest 指向文件并提供-OutFile 路径。也许您可以通过“顶级 HTML 视图”找到它?

如果您知道需要提供的标头是什么,可以试试这个:

$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

【讨论】:

  • setting_support.php 有两种形式。 Form[0] 没有动作。 Form[1] 有一个动作 /adminui/search.php
  • 我试过你建议附加一个 outfile 参数。只有我在帖子中更新的登录页面。
猜你喜欢
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多