PowerShell WebClient example to download a password secured file and email the completion status.

I seem to have got a lot of comments and questions on this from an earlier post so I promised to update this to include email status alerts. This is mainly for those enterprise folks that that haven’t yet move to PS v3 (though this works too on v3).

Automating a status email can be done with a simple function which takes two parameters as text strings.

  1. A subject line
  2. A body or detail message
## Define a simple function to send email alerts ##
function sendEmail ($subject, $detail) {
    $mail = New-Object System.Net.Mail.MailMessage
    $mail.From = "sender@yourdomain.com";
    $mail.To.Add("recipient@theirdomain.org");
    $mail.Subject = $subject;
    $mail.Body = $detail;
    $smtp = New-Object System.Net.Mail.SmtpClient -ArgumentList "your.smtpserver.com";
    $smtp.Credentials = New-Object System.Net.NetworkCredential("yourdomain\yourusername", "yourpassword");
    $smtp.UseDefaultCredentials = $true
    $smtp.Send($mail);
}

Next, the PowerShell code to perform the web download. I’m using a “Try, Catch”
block so we can get any error message into the body of an email to report failure status.


$date = Get-Date -format yyMMdd;
$mypath = "C:\tmp";

Try{
    ## Define the target URL ##
	$url = "http://colinwhite.net/foo.csv";

    ## Instantiate a WebClient Object from the .Net classes ##
	$request = New-Object System.Net.Webclient

    ## credentials for the proxy
    $request.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

    ## Define the website login password ##
	$passwd	= ConvertTo-SecureString "YourPassword" -AsPlainText -Force;

    ## Define the website login username ##
	$request.Credentials = New-Object System.Management.Automation.PSCredential ("YourUserName", $passwd);

    ## Make the Get request and pipe output to the destination file ##
	$request.Downloadstring($url) | Out-File "$($mypath)\foo $($mydate).csv" -force;

    ## Send an email confirming the download completed ##
	sendEmail ("Downloaded $($date)", "Downloaded successfully to $($mypath\foo $($mydate).csv")	

    ### Send a success message to the console. Uncomment this for testing ##
    #Write-Host("Download Finished")

}

Catch [System.Net.WebException], [System.IO.IOException] {
    ## If there's an error send a message to the console ##
	#Write-Host "Nothing to download: (404)";

    ## Send an email indicating a download error ##
	sendEmail ("Failed Download on $($date)", "Nothing to download - $_error")

    ## Exit gracefully ##
	Exit

	}

Throw this all into one long PowerShell script (download.ps1) then add it to the Windows Scheduler as a Scheduled Task and you should be good to go.

Advertisements

One thought on “PowerShell WebClient example to download a password secured file and email the completion status.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s