{"id":1079,"date":"2016-05-26T11:02:39","date_gmt":"2016-05-26T01:02:39","guid":{"rendered":"https:\/\/girl-germs.com\/?p=1079"},"modified":"2018-11-29T09:22:30","modified_gmt":"2018-11-28T23:22:30","slug":"how-to-send-an-email-when-a-server-reboots-including-who-and-why","status":"publish","type":"post","link":"https:\/\/girl-germs.com\/?p=1079","title":{"rendered":"How to: send an email when a server reboots&#8230;including who and why!"},"content":{"rendered":"<p>So, I was told that this might make a good blog post, so thought I&#8217;d be a good person and share (because that&#8217;s the kind of person I am).<\/p>\n<p>A bit of background:&nbsp;If you follow me on Twitter (<a href=\"http:\/\/www.twitter.com\/girlgerms\">and you totally should<\/a>), over the last few days you may have seen me wearing my ranty pants due to some issues at work. Specifically, regarding a coworker rebooting production domain controllers during the middle of the day with no warning. No IM to the rest of the team to let us know. No email to inform us. Nothing.<\/p>\n<p>Now, I can hear what you want to yell at me: &#8220;But if you&#8217;ve got your domain set up properly, it shouldn&#8217;t be an issue! With multiple domain controllers, there&#8217;s redundancy!&#8221; You&#8217;d be partially right.&nbsp;There is redundancy&nbsp;<strong>for the domain<\/strong>. But not for the apps that connect to the domain&#8230;and prefer to speak to a specific server (e.g. for LDAP binds). For the sake of getting past this, let&#8217;s say that in my environment I know what I&#8217;m talking about and rebooting a DC without any warning is bad juju&#8230;forgetting, for a moment, that rebooting&nbsp;<strong>any<\/strong> production server during business hours without warning is bad juju.<\/p>\n<p>So, moving on. Because this particular coworker, even&nbsp;<strong>after<\/strong> being told to let people know, went and did the same thing again the following day &#8211; I decided that action needed to be taken. We needed something that would tell us when these servers were going down &#8211; sure, monitoring helps, but if the server goes down and comes back up so quickly that your monitoring doesn&#8217;t catch it, it makes it hard.<\/p>\n<p>So I went hunting for something that would do what I was after&#8230;and I was able to find something that was close, and then kind of&nbsp;<em>smooshed it<\/em> into what I needed it to do.<\/p>\n<p>So, here goes:<\/p>\n<p>This is a fairly&nbsp;basic script, that I pared down <a href=\"https:\/\/thwack.solarwinds.com\/thread\/76831\">from this one<\/a>, from when I went hunting. The comments really tell the story &#8211; it gets the last EventiD 1074 entry in the System event log, parses that and turns it into individual variables. You put in your email address for To\/From, punch in an SMTP server, and it spits out an email with the details of the EventID 1074. Pretty simple.<\/p>\n<p>The following is the script I used. You can also download it <a href=\"https:\/\/github.com\/girlgerms\/ServerReboot\/blob\/master\/RebootEmail.ps1\">here<\/a>.<\/p>\n<pre># Look for the last event with EventID 1074 in the System log.\n$EventInfo = Get-WinEvent -FilterHashtable @{logname='System'; id=1074} -MaxEvents 1\n$EventInfo | ForEach-Object {\n$rv = New-Object PSObject | Select-Object Date, User, Action, Process, Reason, ReasonCode, Comment\n$rv.Date = $_.TimeCreated\n$rv.User = $_.Properties[6].Value\n$rv.Process = $_.Properties[0].Value\n$rv.Action = $_.Properties[4].Value\n$rv.Reason = $_.Properties[2].Value\n$rv.ReasonCode = $_.Properties[3].Value\n$rv.Comment = $_.Properties[5].Value\n$rv\n}\n \n \n# Set your email settings\n$From = &lt;INSERT EMAIL ADDRESS HERE&gt;\n$To = &lt;INSERT EMAIL ADDRESS HERE&gt;\n#$Cc = \"\"\n$Subject = $env:COMPUTERNAME + \" has Rebooted\"\n#My Email body contains custom properties that will differ from your system.\n$Body = \"$env:COMPUTERNAME has rebooted at $($rv.Date) by $($rv.User) `r`nReason: $($rv.Reason) ($($rv.ReasonCode)) `r`nComment:$($rv.Comment) \"\n$SMTPServer = &lt;INSERT SMTP SERVER HERE&gt;\n$SMTPPort = \"25\"\n \n \n# Send the email!\nSend-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer<\/pre>\n<p>Now, all we need to do is put it into a scheduled task. The scheduled task I have set runs this at startup using the local SYSTEM account. Not sure if this is the best way (and if there is a better way, please share, remembering this was a quick throw-together I did in about half an hour!)<\/p>\n<p>The following is the XML for the scheduled task, or you can download the .xml file <a href=\"https:\/\/github.com\/girlgerms\/ServerReboot\/blob\/master\/Reboot%20Information-cleaned.xml\">here<\/a>.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-16\"?&gt;\n&lt;Task version=\"1.4\" xmlns=\"http:\/\/schemas.microsoft.com\/windows\/2004\/02\/mit\/task\"&gt;\n &lt;RegistrationInfo&gt;\n &lt;Date&gt;2016-05-26T08:09:56.24079&lt;\/Date&gt;\n &lt;Author&gt;DOMAIN\\USERNAME&lt;\/Author&gt;\n &lt;\/RegistrationInfo&gt;\n &lt;Triggers&gt;\n &lt;BootTrigger&gt;\n &lt;Enabled&gt;true&lt;\/Enabled&gt;\n &lt;\/BootTrigger&gt;\n &lt;\/Triggers&gt;\n &lt;Principals&gt;\n &lt;Principal id=\"Author\"&gt;\n &lt;UserId&gt;S-1-5-18&lt;\/UserId&gt;\n &lt;RunLevel&gt;HighestAvailable&lt;\/RunLevel&gt;\n &lt;\/Principal&gt;\n &lt;\/Principals&gt;\n &lt;Settings&gt;\n &lt;MultipleInstancesPolicy&gt;IgnoreNew&lt;\/MultipleInstancesPolicy&gt;\n &lt;DisallowStartIfOnBatteries&gt;true&lt;\/DisallowStartIfOnBatteries&gt;\n &lt;StopIfGoingOnBatteries&gt;true&lt;\/StopIfGoingOnBatteries&gt;\n &lt;AllowHardTerminate&gt;true&lt;\/AllowHardTerminate&gt;\n &lt;StartWhenAvailable&gt;false&lt;\/StartWhenAvailable&gt;\n &lt;RunOnlyIfNetworkAvailable&gt;false&lt;\/RunOnlyIfNetworkAvailable&gt;\n &lt;IdleSettings&gt;\n &lt;StopOnIdleEnd&gt;true&lt;\/StopOnIdleEnd&gt;\n &lt;RestartOnIdle&gt;false&lt;\/RestartOnIdle&gt;\n &lt;\/IdleSettings&gt;\n &lt;AllowStartOnDemand&gt;true&lt;\/AllowStartOnDemand&gt;\n &lt;Enabled&gt;true&lt;\/Enabled&gt;\n &lt;Hidden&gt;false&lt;\/Hidden&gt;\n &lt;RunOnlyIfIdle&gt;false&lt;\/RunOnlyIfIdle&gt;\n &lt;DisallowStartOnRemoteAppSession&gt;false&lt;\/DisallowStartOnRemoteAppSession&gt;\n &lt;UseUnifiedSchedulingEngine&gt;false&lt;\/UseUnifiedSchedulingEngine&gt;\n &lt;WakeToRun&gt;false&lt;\/WakeToRun&gt;\n &lt;ExecutionTimeLimit&gt;PT10M&lt;\/ExecutionTimeLimit&gt;\n &lt;Priority&gt;7&lt;\/Priority&gt;\n &lt;\/Settings&gt;\n &lt;Actions Context=\"Author\"&gt;\n &lt;Exec&gt;\n &lt;Command&gt;powershell.exe&lt;\/Command&gt;\n &lt;Arguments&gt;&lt;LOCATION OF SCRIPT&gt;&lt;\/Arguments&gt;\n &lt;\/Exec&gt;\n &lt;\/Actions&gt;\n&lt;\/Task&gt;<\/pre>\n<p>When it runs at startup (or manually, if you want it to!) the following is what you&#8217;ll get:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/girl-germs.com\/wp-content\/uploads\/2016\/05\/RebootEmail.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1080\" data-permalink=\"https:\/\/girl-germs.com\/?attachment_id=1080\" data-orig-file=\"https:\/\/i0.wp.com\/girl-germs.com\/wp-content\/uploads\/2016\/05\/RebootEmail.jpg?fit=445%2C252&amp;ssl=1\" data-orig-size=\"445,252\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"RebootEmail\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/girl-germs.com\/wp-content\/uploads\/2016\/05\/RebootEmail.jpg?fit=445%2C252&amp;ssl=1\" class=\"alignnone size-full wp-image-1080\" src=\"https:\/\/i0.wp.com\/girl-germs.com\/wp-content\/uploads\/2016\/05\/RebootEmail.jpg?resize=445%2C252&#038;ssl=1\" alt=\"RebootEmail\" width=\"445\" height=\"252\"><\/a><\/p>\n<p>So that&#8217;s it &#8211; how to make sure you get an email when a critical server reboots and get the information you need to work out who and why!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I was told that this might make a good blog post, so thought I&#8217;d be a good person and share (because that&#8217;s the kind of person I am). A bit of background:&nbsp;If you follow me on Twitter (and you totally should), over the last few days you may have seen me wearing my ranty pants due to some issues&#8230; <a href=\"https:\/\/girl-germs.com\/?p=1079\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8,38,5,328,2,7],"tags":[],"class_list":["post-1079","post","type-post","status-publish","format-standard","hentry","category-documentation","category-frustrations","category-sys-admin","category-techstuff","category-technology","category-work"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2Tmk1-hp","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/girl-germs.com\/index.php?rest_route=\/wp\/v2\/posts\/1079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/girl-germs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/girl-germs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/girl-germs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/girl-germs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1079"}],"version-history":[{"count":6,"href":"https:\/\/girl-germs.com\/index.php?rest_route=\/wp\/v2\/posts\/1079\/revisions"}],"predecessor-version":[{"id":1974,"href":"https:\/\/girl-germs.com\/index.php?rest_route=\/wp\/v2\/posts\/1079\/revisions\/1974"}],"wp:attachment":[{"href":"https:\/\/girl-germs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/girl-germs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/girl-germs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}