David's profileDavid Moisan's ITPhotosBlogLists Tools Help
    March 19

    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)

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://dmoisan.spaces.live.com/blog/cns!95CB015E3E4A702A!202.trak
    Weblogs that reference this entry
    • None