none
Script throwing an error.. RRS feed

  • Question

  • hey this script keeps throwing an error goggled solutions but no luck. any suggestions?

    the error says "...the underlying connection was closed: An unexpected error occurred on a send... "$wc.Downloadstring($mainurl)"

    cls
    #<tr valign="top"><td><a href="../deadata/zipweekly/WA131111.zip">WA131111.zip</a> </td><td> 69.1 MB </td><td> 2013/11/11 </td><td align="center"> Weekly </td><td align="center"> 1512416</td>  
     
    $ReportFolder="C:\Data\XXX"
    $DebugPreference = "SilentlyContinue"
    $MainUrl = "https://XXX"
    $ReportBlock=".*\.zip"
    $Username = "XXX"
    $Password = "XXX
    $wc = new-object System.Net.WebClient
    $wc.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
    $wc.DownloadString($MainUrl) 
    $matches=[string] $ReportPage |select-string -pattern  "$ReportBlock" -AllMatches
    
    
    foreach($match in $matches.Matches) {	
       	$txt = ( ($match.Value  -replace "</A><br>", "`r`n") -replace  "<[^>]*?>","")
       	foreach($lines in $txt.Split("`r`n")){
          $line=$lines|select-string  -NotMatch -Pattern "To Parent|^$|&lt;dir&gt;"
          if ($line -ne $null){
    		 if (!(Test-Path "$ReportFolder\$line"))  { 
    		 	Try {	
               		$wc.DownloadFile("$ZipUrl/$line","$ReportFolder\$line" )
           		}
    	    	catch [System.Net.WebException]  {
    				echo "An error occurred: $ZipUrl/$line`r`n$ReportLocalDir\$_"
    				if ($_.Exception.InnerException) {"$($_.Exception.InnerException.Message)"}
    			}
    		 }
          }
       }
    }
    


    QNN

    Friday, February 3, 2017 4:09 PM

All replies

  • Assuming that your hosting server correctly supports the download protocol you are using.

    Something like this should work:

    $source = 'http://foo.bar.com/Media/jar71.zip'
    $destination = "$env:programdata\notRoot\Media.zip"
    $webClient = New-Object System.Net.WebClient
    $webClient.DownloadFile("$source", "$destination")
    Unblock-File $destination 


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.

    Friday, February 3, 2017 4:31 PM
    Moderator
  • thank you sir. what does your "env:programdata" in the $destination represent? "$env:programdata\notRoot\Media.zip" 

    QNN

    Friday, February 3, 2017 4:48 PM