Powershell: Quick script to email DST status

Described by some as "another Y2K", DST in North America started 3 weeks earlier.  Unfortunately, I never got to post this in timefor the changeover, but DST will end 1 week later, so we’re not done testing.

Here’s a Powershell script that gets DST status and mails it to the address specified in the command line.  It’s great boilerplate code if you need to send a quick status email and much much better than the old way with NT CMD, Blat and VBScript.

 


# Send-DSTStatus
#
# Get current time and DST status of specified computer and email to specified user
# For use in supporting North American DST changes
#
# D. Moisan 2/23/2007
#
param([string]$computer, [string]$email)
#
# Parameters (Required):
#
# -computer <computer to scan for status>
# -email <email address>

if ($email -eq "") {
   write-host "Usage:"
   write-host
   write-host "send-DSTStatus [-computer <computer>] -email <somebody@somewhere.com>"
   write-host
   write-host "`t-computer defaults to local machine if not specified"
   write-host
   exit
   }

#
# Set up email sender address and server name
#
$sender = "Administrator@satvonline.org"  # Change as needed
$mailservername = "salemtv.satv.loc"    # Change to your SMTP server's address

$mailserver = new-object system.net.mail.smtpClient
$mailserver.Host = $mailservername
$mailmessage = new-object system.net.mail.mailmessage($sender,$email)

# If -computer parameter not specified, use localhost

if ($computer -eq "") {
   $computer = $Env:Computername
   }

# Get WMI Objects

$wmios = get-wmiobject "Win32_OperatingSystem" -computer $computer
$wmisys = get-wmiobject "Win32_ComputerSystem" -computer $computer

# Compose message

$maildate = get-date
$mailmessage.Subject = "DST Status Report for $computer on $maildate"
$mailmessage.Sender = $sender
$messagetext = $mailmessage.Subject + "`n`n"
$messagetext += "Local Time for $computer : " + $wmios.ConverttoDateTime($wmios.LocalDateTime) + "`n`n"
$DSTEnabled = $wmisys.EnableDaylightSavingsTime
$DSTInEffect = $wmisys.DaylightInEffect
$TZOffset = $wmisys.CurrentTimeZone
if ($DSTEnabled) {
   $messagetext += "DST is ENABLED`n" }
else {

   # We can't tell if Windows 2000 clients have DST implemented since
   # the WMI property EnableDaylightSavingsTime is not supported there
   # so if the property is null (as is the case in 2000), we skip reporting
   # DST enabled.  IF it is False (as in an XP/2003 client with DST turned off),
   # we report that.

   if ($DSTEnabled -eq $False) {
      $messagetext += "DST is NOT ENABLED`n"
      }
   }

if ($DSTInEffect) {
   $messagetext += "DST is IN EFFECT`n" }
else {
   $messagetext += "DST is NOT IN EFFECT`n" }

$messagetext += "Current timezone offset: $TZOffset `n"
$messagetext += "`n`n"

# Send mail and report it on the console

$mailmessage.Body = $messagetext
write-host "From: $sender"
write-host
write-host "To: $email"
write-host
write-host $messagetext
$mailserver.Send($mailmessage)
Advertisements


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