简体   繁体   English

从https下载Powershell文件

[英]Powershell filedownload from https

I am trying to download several files using Powershell and its Invoke-WebRequest method. 我正在尝试使用Powershell及其Invoke-WebRequest方法下载几个文件。

I'm basically looping through several filenames (I know that they are available on the server) and download them. 我基本上是遍历几个文件名(我知道它们在服务器上可用)并下载它们。

My problem is that my script works for the first file and fails for every file that follows. 我的问题是我的脚本适用于第一个文件,而之后的每个文件均失败。 When I open one of the later files (.csv`s) there is just some html code in it). 当我打开一个更高版本的文件(.csv)时,其中只有一些html代码)。

I already read a lot about passing session cookings but I am not sure if this is my problem or how I can do that. 我已经阅读了很多有关通过会议烹饪的信息,但是我不确定这是我的问题还是如何解决。

My script so far looks like this: 到目前为止,我的脚本如下所示:

$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
}

Thank you all and let me know what you think :) 谢谢大家,让我知道您的想法:)

It seems like you don't using the HttpWebRequest you created to download the file. 似乎您没有使用创建的HttpWebRequest来下载文件。 Anyway, I would recommend using System.Net.WebClient : 无论如何,我建议使用System.Net.WebClient

$wc = New-Object System.Net.WebClient
$wc.Credentials =  New-Object System.Net.NetworkCredential($httpsUser,$httpsPass)
$wc.DownloadFile($Source, $target)

Try using webclient methods DownloadFile(src,dst). 尝试使用网络客户端方法DownloadFile(src,dst)。 Should be something like this: 应该是这样的:

$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)
}

Thank you all for your answers. 谢谢大家的答案。 I figured out now that the site generates a security token while logging in. This token needs to be passed in every webrequest. 我现在发现,该站点在登录时会生成一个安全令牌。此令牌需要在每个Web请求中传递。 I was not yet able to figure out how to do that with powershell but know that perl has a build in function (called $merch) for exactly this problem. 我还无法弄清楚如何使用powershell来实现这一点,但是我知道perl确实有一个内置函数(称为$ merch)来解决此问题。

To solve my problem I had to automatize the IE :( (I know this is not the most sophisticated way but right now it is the quickest solution. If anyone is interested here is the code for that: 为了解决我的问题,我不得不自动化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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM