A N

September 21, 2015

Get Dilbert Cartoon with Powershell

Filed under: Tips — donrsh @ 8:17 am

Get Today’s or even yesterdays Dilbert Cartoon with Powershell with Invoke-WebRequest from http://www.dilbert.com and send it as email with embedded picture.

   <#
   .NOTES 
   ===========================================================================

      Created on:   	2015-09-21 09:05
      Created by:   	Arash Nabi
      Email:            arash@nabi.nu

  ===========================================================================

   .Synopsis
      Send Daily Dilbert Cartoon from www.dilbert.com
   .EXAMPLE
      Todays Dilbert: Get-Dilbert
   .EXAMPLE
      Yestardays Dilbert: Get-Dilbert -last -1
   #>
function Get-Dilbert
{
	[CmdletBinding()]
	[OutputType([int])]
	Param
	(
		[Parameter(ValueFromPipelineByPropertyName = $true,
				   HelpMessage = "Choose last date",
				   Position = 0)]
		[ValidateSet ('-1',
		'-2',
		'-3',
		'-4')]
		[int]$Last
		
		
	)
	
	
	Process
	{
		$VerbosePreference = "Continue"
		if ($Last)
		{
			$lastDate = (get-date).AddDays($Last) | get-date -Format "yyyy-MM-dd"
			Write-Verbose 'Invoking website'
			$dil = Invoke-WebRequest -Uri "http://dilbert.com/strip/$LastDate" -UseBasicParsing
			$bild = $dil.Images | Where-Object { $_.class -eq 'img-responsive img-comic' } | Select-Object src
			Invoke-WebRequest $bild.src -OutFile "$PSScriptRoot\$LastDate.gif"
			Write-Verbose 'Sending email'
			Send-MailMessage -to Email.To@domain.com -From Email.From@domain.com -SmtpServer smtp.domain.com -Subject "Dilbert $LastDate" -BodyAsHtml "<br/><img src='$LastDate.gif' />" -Attachments "$PSScriptRoot\$LastDate.gif"
		}
		else
		{
			$today = (get-date -Format "yyyy-MM-dd")
			Write-Verbose 'Invoking website'
			$dil = Invoke-WebRequest -Uri "http://dilbert.com/strip/$today" -UseBasicParsing
			$bild = $dil.Images | Where-Object { $_.class -eq 'img-responsive img-comic' } | Select-Object src
			Invoke-WebRequest $bild.src -OutFile "$PSScriptRoot\$today.gif"
			Write-Verbose 'Sending email'
			Send-MailMessage -to Email.To@domain.com -From Email.From@domain.com -SmtpServer smtp.domain.com -Subject "Dilbert $today" -BodyAsHtml "<br/><img src='$today.gif' />" -Attachments "$PSScriptRoot\$today.gif"
			
		}
		
		
	}
	
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: