来自https的Powershell文件下载

时间:2016-04-08 06:38:06

标签: file powershell cookies https download

我正在尝试使用Powershell及其Invoke-WebRequest方法下载多个文件。

我基本上循环遍历多个文件名(我知道它们在服务器上可用)并下载它们。

我的问题是我的脚本适用于第一个文件,并且对于后面的每个文件都失败了。当我打开其中一个后来的文件(.csv`s)时,其中只有一些html代码)。

我已经阅读了很多关于传递会话烹饪的内容,但我不确定这是我的问题还是我能做到的。

到目前为止,我的脚本看起来像这样:

$httpsUser = 'XXX'
$httpsPass = 'YYY'

foreach ($instrument in 'ivv','ijh','ijr','iwm') {

$Source = 'https://***', `
$instrument, '-en_us.csv' -join ""

$Target = 'C:\User\', `
$instrument, '-en_us.csv' -join ""

$uri = New-Object “System.Uri” “$Source”
$WebClient = [System.Net.HttpWebRequest]::Create($uri) 
$webclient.Proxy.Credentials =
[System.Net.CredentialCache]::DefaultNetworkCredentials
$webclient.Credentials =
New-Object System.Net.NetworkCredential($httpsUser,$httpsPass)

Invoke-WebRequest -Uri $Source -OutFile $Target
}

谢谢大家,让我知道你的想法:)

3 个答案:

答案 0 :(得分:0)

您似乎没有使用自己创建的override viewDidLoad() { fiName?.text = object.firstName laName?.text = object.lastName miName?.text = object.middleName aess?.text = object.address phoNumber?.text = object.phoneNumber yearBirth?.text = String(object.age) navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem() navigationItem.leftItemsSupplementBackButton = true } 来下载文件。无论如何,我建议使用HttpWebRequest

System.Net.WebClient

答案 1 :(得分:0)

尝试使用webclient方法DownloadFile(src,dst)。应该是这样的:

$httpsUser = 'XXX'
$httpsPass = 'YYY'

foreach ($instrument in 'ivv','ijh','ijr','iwm') {

$Source = 'https://***', `
$instrument, '-en_us.csv' -join ""

$Target = 'C:\User\', `
$instrument, '-en_us.csv' -join ""

$webclient = New-Object -TypeName Net.WebClient
$webclient.Encoding = [System.Text.Encoding]::UTF8
$webclient.UseDefaultCredentials = $true
$webclient.Proxy.Credentials = New-Object System.Net.NetworkCredential($httpsUser,$httpsPass)
$webclient.DownloadFile($Source,$Target)
}

答案 2 :(得分:0)

谢谢大家的回答。我现在发现该站点在登录时会生成安全令牌。此令牌需要在每次webrequest中传递。我还没有弄清楚如何使用powershell做到这一点但是知道perl有一个内置函数(称为$ merch)来解决这个问题。

为了解决我的问题,我不得不自动化IE :((我知道这不是最复杂的方法,但现在它是最快的解决方案。如果有人对此感兴趣,那就是代码:

$ie = new-object -ComObject 'InternetExplorer.Application'
$requestUri = 'https://www.trololo.com'
$userIdFragment = "userName";
$passwordIdFragment = "password";
$buttonIdFragment = "submitLogin";
$ie.visible = 'false'


$ie.navigate($requestUri)
while($ie.Busy) { Start-Sleep -Milliseconds 100 }


$doc1 = $ie.Document
$doc1.getElementsByTagName("input") | % {
    if ($_.id -ne $null){
        if ($_.id.Contains($buttonIdFragment)) { $btn = $_ }
        if ($_.id.Contains($passwordIdFragment)) { $pwd = $_ }
        if ($_.id.Contains($userIdFragment)) { $user = $_ }
    }
}

$user.value = "XXXX"
$pwd.value = "YYYY
$btn.disabled = $false
$btn.click()
while($ie.Busy) { Start-Sleep -Milliseconds 5000 }
$ie.navigate($requestUri)
while($ie.Busy) { Start-Sleep -Milliseconds 200 }
$doc1 = $ie.Document


$link = $doc1.getElementsByTagName("a") | where-object {$_.href -match "Your String"}
$link.click()
Start-Sleep -Milliseconds 1000
$wshell = new-object -com wscript.shell
$wshell.appactivate("Internet Explorer")
$wshell.sendkeys("%s")