Friday, September 22, 2017

PowerShell: Invoke-WebRequest Phone Arena RSS Feed Emailer

Use this code if you'd like an emailer from Phone Arena:

[xml]$phonearena = iwr "https://www.phonearena.com/feed"
$news = $phonearena.rss.channel.item
foreach ($n in $news){
$link = $n.link
$title = $n.title
$html = New-Object -ComObject "HTMLFile"
$getimglink =  ($n.description.'#cdata-section')
$html.IHTMLDocument2_write($getimglink)
$imglink = $html.body.getelementsbytagname('img')|select -expand src
$text = $html.body.innertext
$n.author = ("<table width='100%'><tr><td style='font-family: Arial, sans-serif;'><a href=$link><img width=200 align=left src=" + $imglink + "></a><center><h3>" + $title + "</h3></center></td></tr></table><table><tr><td style='font-family: Arial, sans-serif;font-size:13;'>" + $text + "</td></tr></table><hr style='border: 2px solid mintcream;'>")
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($html)|out-null
}

$body = $news.author
$body = $body.replace("???",'"')

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "mymail@gmail.com"
$Password = "mypassword"
$to = "recipient@somemail.com"
$subject = ("Phone Arena - " + (get-date).tolongdatestring())
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.from = $username
$message.IsBodyHTML = $true
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)

PowerShell: Invoke-WebRequest SlashDot RSS Feed Emailer

Use the following code if you want an emailer using Slashdot's RSS service:


[xml]$slashdot = iwr http://rss.slashdot.org/Slashdot/slashdotMain
$articles = $slashdot.getelementsbytagname('item')

foreach ($a in $articles){
$link = $a.link
$title = $a.title
$comments = $a.comments
$department = $a.department
$description = $a.description
if ($a.subject -eq "iphone"){$subjecticon = "<img align=left vspace=2 hspace=5 align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/iphone_64.png>"}
if ($a.subject -eq "money"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/money_64.png>"}
if ($a.subject -eq "microsoft"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/microsoft_64100.png>"}
if ($a.subject -eq "privacy"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/privacy_64.png>"}
if ($a.subject -eq "eu"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/eu_64.png>"}
if ($a.subject -eq "encryption"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/encryption_64.png>"}
if ($a.subject -eq "firefox"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/firefox_64.png>"}
if ($a.subject -eq "security"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/security_64.png>"}
if ($a.subject -eq "social"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/social_64.png>"}
if ($a.subject -eq "drm"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/drm_64.png>"}
if ($a.subject -eq "business"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/business_64.png>"}
if ($a.subject -eq "games"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/games_64.png>"}
if ($a.subject -eq "china"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/china_64.png>"}
if ($a.subject -eq "tv"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/tv_64.png>"}
if ($a.subject -eq "medicine"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/medicine_64.png>"}

$html = New-Object -ComObject "HTMLFile"
$html.IHTMLDocument2_write($a.description)
$text = $html.body.innertext
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($html)|out-null
$a.about = ("<table><tr><td><h3>" + $subjecticon + $title + "</h3><b>" + $comments + "</b> comments <b><i>(" + $department + ")</b></i></td></tr></table><table><tr><td style='font-family: Arial, sans-serif;font-size:13;'>" + $text + "</td></tr></table><hr>")
}

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "myemail@gmail.com"
$Password = "mypassword"
$to = "recipient@somemail.com"
$subject = ("Slashdot - " + (get-date).tolongdatestring())
$body = $articles.about
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $articles.about
$message.to.add($to)
$message.from = $username
$message.IsBodyHTML = $true
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)

PowerShell: Invoke-WebRequest Techspot RSS Feed Emailer

Follow the code below to receive emails from Techspot using their RSS feed:

[xml]$techspot = iwr https://www.techspot.com/backend.xml
$articles = $techspot.getelementsbytagname('item')

foreach ($a in $articles){
$link = $a.link
$title = $a.title.'#cdata-section'
$html = New-Object -ComObject "HTMLFile"
$html.IHTMLDocument2_write($a.description.'#cdata-section')
$imglink = $html.body.getelementsbytagname('img')[0].src
$text = $html.body.innertext

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($html)|out-null

$a.pubdate = ("<table width='100%'><tr><td style='font-family: Arial, sans-serif;'><a href=$link><img width=200 align=left src=" + $imglink + "></a><center><h3>" + $title + "</h3></center></td></tr></table><table><tr><td style='font-family: Arial, sans-serif;font-size:13;'>" + $text + "</td></tr></table><hr style='border: 2px solid mintcream;'>")
}

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "myemail@gmail.com"
$Password = "mypassword"
$to = "recipient@somemail.com"
$subject = ("Techspot - " + (get-date).tolongdatestring())
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $articles.pubdate
$message.to.add($to)
$message.from = $username
$message.IsBodyHTML = $true
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)

PowerShell: Invoke-WebRequest Android Police RSS Feed Emailer

I like Android Police and used to have their mailer but it was quirky so I wrote a RSS Feed reader which then sends an email.  I have it kick off daily via a scheduled task from my server and I get my daily dose.

[xml]$ap = iwr http://www.androidpolice.com/feed/
$articles = $ap.getelementsbytagname('item')

foreach ($a in $articles){
$link = $a.link
$comments = ($a.link + "#comments")
$title = $a.title
$title = $title -replace ('\[','"')
$title = $title -replace ('\]','"')
$html = New-Object -ComObject "HTMLFile"
$description =  ($a.description.'#cdata-section')
$html.IHTMLDocument2_write($description)
$imglink = $html.body.getelementsbytagname('img')[0].src
$text = $html.body.innertext
$text = $text -replace ('\[','"')
$text = $text -replace ('\]','"')
$text = $text -replace ('Read More\s+')
$text = $text -replace ($title)
$text = $text.replace(" was written by the awesome team at Android Police.","")
$text = $text.trim()
$text = $text + " <a href=$comments>Comments</a>"
$a.pubdate = ("<table width='100%'><tr><td style='font-family: Arial, sans-serif;'><a href=$link><img width=200 align=left src=" + $imglink + "></a><center><h3>" + $title + "</h3></center></td></tr></table><table><tr><td style='font-family: Arial, sans-serif;font-size:13;'>" + $text + "</td></tr></table><hr style='border: 2px solid mintcream;'>")

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($html)|out-null
}

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "myaccount@gmail.com"
$Password = "mypassword"
$to = "recipient@somemail.com"
$subject = ("Android Police - " + (get-date).tolongdatestring())
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $articles.pubdate
$message.to.add($to)
$message.from = $username
$message.IsBodyHTML = $true
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)

Thursday, August 11, 2016

PowerShell: Gmailing / Emailing Screenshots

I totally scraped the script from AdminAresenal. Kris Powell wrote such a good solution that I lifted it and added a gmailer process. I'm posting this solution in case someone needs to automate emailing of screenshots. The automation component is Task Scheduler.  My scheduled job uses "powershell -executionpolicy unrestricted -windowstyle minimized c:\scripts\screenshots.ps1" so I don't have the PowerShell script window appear in each screenshot.

#Create a timestamp-based BMP filename

$filename = (get-date).tostring().replace('/','.').replace(':','.')

#Create a folder for your outputted screenshots
$File = "c:\scripts\ss\$filename.bmp"

## From the AdminArsenal blog ##
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

# Gather Screen resolution information
$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$Width = $Screen.Width
$Height = $Screen.Height
$Left = $Screen.Left
$Top = $Screen.Top

# Create bitmap using the top-left and bottom-right bounds
$bitmap = New-Object System.Drawing.Bitmap $Width, $Height

# Create Graphics object
$graphic = [System.Drawing.Graphics]::FromImage($bitmap)

# Capture screen
$graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)

# Save to file
$bitmap.Save($File) 

#Now we send that screenshot via GMail
#Enable "Less Secure Apps" in order to use SMTP via script
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "yourgmailaccount@gmail.com"
$Password = "P@$$w0rd"

$to = "addressee@gmail.com"
$cc = "anotherperson@someotherdomain.com"
$subject = "Screenshot taken $filename"
$body = "Screenshot Attachment"
$attachment = $file

$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.cc.add($cc)
$message.from = $username
$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)