2013-09-24 37 views
1

我正在使用powershell做一些監控,我想檢查一個應用程序的jnlp 是否存在於網站上,並可供下載。 我有鏈接到.jnlp,到目前爲止我正在用.navigate()下載文件。PowerShell測試應用程序鏈接

$ie = new-object -com "InternetExplorer.Application" 
Try { 
    $ie.navigate("http://bla.com/testApp.jnlp") 
} Catch { 
    #$_ 
    $ErrorMessage = $_.Exception.Message 
} 

我試圖通過提供無效的文件名來捕獲異常,但它不起作用。 此外,我想下載的應用程序,並嘗試刪除文件後,以 檢查它確實存在,但它會太慢,因爲我有很多jnlps檢查。

有沒有另一種更簡單和優雅的方式來做到這一點?我想避免下載 我想測試的每個文件。

回答

4

如何使用WebClient類來自.Net?獲取數據非常簡單。像這樣,

$webclient = new-object System.Net.WebClient 
try { 
    # Download data as string and store the result into $data 
    $data = $webclient.DownloadString("http://www.google.com/") 
} catch [Net.WebException] { 
    # A 404 or some other error occured, process the exception here 
    $ex = $_ 
    $ex.Exception 
} 
2

如果您正在使用PowerShell的高於3.0,則可以使用Invoke-WebRequest,看是否存在通過發出HTTP請求HEAD和檢查狀態代碼的頁面。

$Result = Invoke-WebRequest -uri `http://bla.com/testApp.jnlp` -method head 
if ($Result.StatusCode -ne 200){ 
    # Something other than "OK" was returned. 
} 

這也可以用System.Net.WebClient來實現,但這樣做會更加努力。