Your basic ITPro blog... What's going on at work, what I'm interested in.

Saturday, April 12, 2008

A Little PowerShell Fun

On a challenge from a co-worker, I wrote this little script. I am new to scripting, .NET, COM, etc. so I had to research most of this (which, I guess, was the whole point!) These types of exercises, while not too practical in and of themselves, are a great way for me to learn about the features and functions of these technologies.

Write-Host "Reading your inbox...`n`n"
# Folder IDs
#$olAppointmentItem = 1
#$olFolderDeletedItems = 3
#$olFolderOutbox = 4
#$olFolderSentMail = 5
$olFolderInbox = 6
#$olFolderCalendar = 9
#$olFolderContacts = 10
#$olFolderJournal = 11
#$olFolderNotes = 12
#$olFolderTasks = 13
#$olFolderDrafts = 16
# Connect to Outlook Inbox
$outlook = New-Object -ComObject Outlook.Application
$session = $outlook.Session
$inbox = $outlook.session.GetDefaultFolder($olFolderInbox)
# Create Voice Object
$speaker = New-Object -ComObject sapi.spvoice
# Set voice to something a bit more pleasant
$speaker.Voice = $speaker.GetVoices().Item(1)
# Count number of unread items in INBOX
$unreadCount = (%{$inbox.Items | where {$_.UnRead}}).Count
# Get Mailbox User's First Name
$firstName = $outlook.Session.CurrentUser.Name.Split()[0]
# Say it
Write-Host "Hello $firstName. You have " -NoNewline 
Write-Host $unreadCount -ForegroundColor Green -NoNewline 
Write-Host " unread items in your inbox.`n`n"
$null = $speaker.Speak("Hello $firstName. You have $unreadCount unread items in your inbox.")
$speaker = $inbox = $session = $outlook = $Null

No comments:

Additional Info

My photo
email: support (AT) mangrumtech (DOT) com
mobile: 480-270-4332