-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDailyDilbert.ps1
66 lines (55 loc) · 2.06 KB
/
DailyDilbert.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function displayImage
{
param($file)
# Load in Windows forms assembly
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$image = [System.Drawing.Image]::Fromfile($file)
[void][System.Windows.Forms.Application]::EnableVisualStyles()
# Create a picture box using winforms
$form = New-Object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Width = $image.Size.Width
$form.Height = $image.Size.Height
$pictureBox = New-Object Windows.Forms.PictureBox
$pictureBox.Width = $image.Size.Width
$pictureBox.Height = $image.Size.Height
# Add image and display
$pictureBox.Image = $image
$form.Controls.Add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
}
function getDilbert
{
param($path)
try
{
# Dilbert website stores its daily cartoon strip on "http://dilbert.com/strip/<todays date>"
#
# We parse the url and look for the source image.
$url = "http://dilbert.com/strip/$(Get-Date -UFormat "%Y-%m-%d")"
$dilbert = Invoke-WebRequest -Uri $url -UseBasicParsing
$images = $dilbert.images | Where-Object { $_.class -eq 'img-responsive img-comic' } | Select-Object src
Invoke-WebRequest -uri $("http:" + $images.src) -OutFile $path | Out-Null
}
catch
{
write-host "Ooops! There was a problem with the web request!"
$error.Exception
write-host "Launching previous Dilbert..."
}
}
<#
Dilbert web page has recently enforced higher level of encryption, therefore we need to force
PowerShell to use TLS1.2 for all web requests
#>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Host "Please wait while we retrieve Dilbert..."
$file = "$env:USERPROFILE\DailyDilbert.gif"
getDilbert $file
if (Test-Path -Path $file)
# If we can't get today's dilbert image then it will launch the previous
# one from the root of your user profile.
{
displayImage $file
}