此“Idownload”html下载链接可以与PowerShell脚本自动下载过程一起使用吗?

t30tvxxf  于 2023-06-29  发布在  Shell
关注(0)|答案(1)|浏览(113)

我的最终目标是使用PowerShell从一个网站下载一个zip文件,该网站首先需要通过初始页面登录。我发现的一个简单的powershell脚本示例如下:

# URL and Destination
$url = "https://example.com/reports/test.zip"
$dest = "c:\temp\testfiles"
# Define username and password
$username = 'User1'
$password = 'Password123'
# Convert to SecureString
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
# Create Credential Object
$credObject = New-Object System.Management.Automation.PSCredential ($username, $secPassword)
# Download file
Invoke-WebRequest -Uri $url -OutFile $dest -Credential $credObject

我主要关心的是我从我实际下载的zip文件中得到的html下载链接。链接如下:https://example.com/webclient/idownload(实际链接确实以/webclient/idownload结尾)。我还没有看到一个答案,什么是'idownload'扩展意味着在下载链接的类型方面。这是某种类型的安全下载链接,其中下载项目名称不像许多标准下载链接那样在html中:https://example.com/reports/test.zip.网页上有多个下载链接,我发现所有下载的html链接都是完全一样的,都以'idownload'结尾,尽管下载文件本身有不同的名称。总的来说,我希望深入了解我正在处理的这种类型的下载链接,以及如何使用PowerShell脚本有效地自动化下载过程。

mspsb9vt

mspsb9vt1#

我有一个类似的案例,这对我很有效。请相应更新您的情况

#variables
$url = "https://blah/../../file_to_download"
$output = "c:\...\file.zip" #local path including filename

$SecretFile = "c:\....\securestring.txt"

#Uncomment following line to create the secretfile
#Read-Host -Prompt "Enter password" -AsSecureString | convertfrom-securestring | out-file $SecretFile
$Username = "user"

#downloading data
$wc = new-object System.Net.WebClient
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential($Username,(Get-Content $SecretFile | ConvertTo-SecureString))
$credCache.Add($url, "Basic", $creds)

$wc.Credentials = $credCache
$wc.DownloadFile($url, $output)

相关问题