2015-06-29 62 views
0

真的很難與這一個。我們從URL下載每週文件。我只需要每週下載最新的文件。我無法弄清楚如何獲取最新的文件。該文件將永遠是WAYYYYMMDD.zip從URL下載最新的文件

$Url = "http://files.test.com/zips/weekly/WAYYYYMMDD.zip" 
$Path = "C:\temp\WA2343.zip" 
$Username = "*******" 
$Password = "********" 

$WebClient = New-Object System.Net.WebClient 
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password) 
$WebClient.DownloadFile($url, $path) 
+0

會是一個解決方案,以倒數日期,並停在第一個文件中找到? – Benj

+0

該文件在每週的同一天發佈,因此它可以查找GetDate()或其他東西。只是不知道如何將它整合到scrdip中 –

回答

1

試試這個:

$CurrentDate = Get-Date -Format yyyyMMdd 
$Url = "http://files.test.com/zips/weekly/WA$CurrentDate.zip" 
$Path = "C:\temp\WA$CurrentDate.zip" 
$Username = "*******" 
$Password = "********" 
$WebClient = New-Object System.Net.WebClient 
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password) 
$WebClient.DownloadFile($url, $path) 

保存腳本並運行它作爲一週的一天一​​個Windows計劃任務的文件被釋放時。

+0

像魅力一樣工作。謝謝! –

0

提出的解決方案應該工作,但這裏是另一個版本的一些錯誤處理:

$filename = 'WA{0}.zip' -f (Get-Date -Format 'yyyyMMdd') 
$uri = "http://files.test.com/zips/weekly/$filename" 

try 
{ 
    #This is to check if the URl exists or not 
    Invoke-WebRequest -Uri $uri -Method Head -Verbose -ErrorAction Stop 

    #if the above call succeeded without errors then download file 
    Invoke-WebRequest -Uri $uri -OutFile (Join-Path C:\temp -ChildPath $filename) -Credential (Get-Credential) -TimeoutSec 60 
} 
Catch 
{ 
    Write-Warning ('Cant find the URL: {0}' -f $uri) 
}