英文:
Can this 'Idownload' html download link be used with PowerShell script automate download process?
问题
我的最终目标是使用 PowerShell 从一个需要登录初始页面的网站下载一个 zip 文件。我找到的一个简单的 PowerShell 脚本示例如下:
# URL 和目标路径
$url = "https://example.com/reports/test.zip"
$dest = "c:\temp\testfiles"
# 定义用户名和密码
$username = 'User1'
$password = 'Password123'
# 转换为 SecureString
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
# 创建凭据对象
$credObject = New-Object System.Management.Automation.PSCredential ($username, $secPassword)
# 下载文件
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 脚本自动化下载过程。
英文:
My end goal is to use PowerShell to download a zip file from a website that first requires one to login through the initial page. A simple powershell script example I have found is the following:
# 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
My main concern is the html download link I get from the zip file I am actually downloading. The link goes like: https://example.com/webclient/idownload (the actual link does end in /webclient/idownload). I have not seen an answer as to what the 'idownload' extension means in terms of types of download links. Is this some type of secure download link where the download item name is not in the html like many standard download links: https://example.com/reports/test.zip. There are multiple download links on the webpage, and I find that the html link is exactly the same for all downloads, all ending with 'idownload', even though the download files themselves have different names.
Overall I am looking to gain insight into this type of download link I am dealing with, and how I can effectively automate the download process with a PowerShell script.
答案1
得分: 0
以下是翻译好的代码部分:
#变量
$url = "https://blah/../../file_to_download"
$output = "c:\...\file.zip" #本地路径,包括文件名
$SecretFile = "c:\....\securestring.txt"
#取消注释以下行以创建密钥文件
#Read-Host -Prompt "输入密码" -AsSecureString | convertfrom-securestring | out-file $SecretFile
$Username = "user"
#下载数据
$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)
英文:
I have kind of a similar case and this works for me. Please update accordingly your situation
#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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论