windows - 在 Powershell 中下载带密码的文件
问题描述
我需要从互联网上下载一个 51gb 的 zip 文件,打开需要输入密码的网站。
我想用 PowerShell (PSVersion:5.1.18362.752) 中的一个函数来做到这一点,但我不能再进一步了。我的功能如下所示:
$securepassword = "thepassword"
function Get-Files {
$Properties = @{
URI = "https://theurl"
Credential = $securepassword
}
Invoke-WebRequest @Properties -OutFile "C:\Users\$env:USERNAME\Desktop\thefiles.zip"
}
参数 Credential 是否只能用于 Windows Credential?
非常感谢您的帮助
解决方案
我认为您使用的凭据错误。凭据不仅仅是密码。它是用户名加密码,它们应该是ICredentials的实例
请注意,只有请求身份验证的网页才能以这种方式传递凭据HTTP 401
。如果网页使用基于表单的身份验证,则应将其作为非标准情况处理,凭据将不起作用,您需要发布数据、保留 cookie 等。
$localPath = [System.IO.Path]::Combine(
[System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::DesktopDirectory),
'OutFileName.zip')
$wc = [System.Net.WebClient]::new()
$wc.Credentials = [System.Net.NetworkCredential]::new($username, $plainTextPassword)
$wc.DownloadFile($uri, $localPath)
$wc.Dispose()
推荐阅读
- nlp - 如何使用 spaCy 获取令牌 ID(我想将文本句子映射到整数序列)
- mysql - 撇号丛林
- python - 通过 Ansible 运行带有模块的 python 脚本
- javascript - Javascript 在数组上未定义
- node.js - node js中的静态类方法或纯函数
- javascript - 提取标签中的准确文本
- c# - C# Selenium xpath 找不到内部 div 节点
- ios - 解码混淆值 - 无法将数据转换为字符串
- go-gorm - 没有使用 Gorm 在 Postgresql 中创建表
- apache-spark - Pyspark - saveAsTable - 如何将新数据插入现有表?