{"id":78,"date":"2016-04-21T03:10:04","date_gmt":"2016-04-21T03:10:04","guid":{"rendered":"http:\/\/cirwordpress.azurewebsites.net\/?p=78"},"modified":"2016-04-28T12:14:17","modified_gmt":"2016-04-28T12:14:17","slug":"safe-restart-script","status":"publish","type":"post","link":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/","title":{"rendered":"Safe Restart Script"},"content":{"rendered":"<h5>This is a script that reboots if day and time criteria are met and if the uptime is less than a given number of days.<img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-90 alignleft\" src=\"http:\/\/www.ciraltos.com\/wp-content\/uploads\/2016\/04\/PoSh-300x300.jpg\" alt=\"PoSh\" width=\"300\" height=\"300\" \/><\/h5>\n<p>I had, what I thought, was a simple request.\u00a0\u00a0 I wanted any servers that had not rebooted as part of the monthly SCCM patch cycle to reboot at the end of the maintenance window.\u00a0 This way I know that the servers don&#8217;t have any unfinished patching and the servers have a reboot in the last 30 days. \u00a0These are\u00a0Windows servers and I&#8217;m old school, I like to routinely reboot\u00a0them.<!--more--><\/p>\n<p>What I ended up with was a command file that ran a shutdown \/r.\u00a0 This was added to each patch collection as a required deployment at the end of the maintenance windows.\u00a0 It worked, but had a couple issues.\u00a0 First and most significant; under certain, easily repeatable circumstances, this deployment would run outside the maintenance window.\u00a0 This caused some unexpected reboots at inopportune times.\u00a0 It also ran even if the server had a clean restart after patches were applied.\u00a0 This was just unnecessary.<\/p>\n<p>I created a script that would use logic before it rebooted to prevent these issues.\u00a0 My goal is to prevent reboots outside a defined time (2 to 4 AM), I also want it to reboot only on a given day of the month.\u00a0 The first two lines of the script below\u00a0 gets the current day of the month and hour when the script runs.<\/p>\n<pre>[int]$day = get-date -format dd\r\n[int]$hour = get-date -format HH<\/pre>\n<p>Next, I don&#8217;t want to reboot if it already has as part of the patching process.\u00a0 To do that, I first need to identify how long it has been running.\u00a0 The next two lines of code gets the uptime by number of days.\u00a0 If the server has been running for less than 24 hours it returns 0.<\/p>\n<pre>$os=Get-WmiObject win32_operatingsystem\r\n$uptime = ((get-date) - ($os.ConvertToDateTime($os.lastbootuptime))).Days<\/pre>\n<p>The next two lines set the day the server can reboot ($rebootDay).\u00a0 In this example it will only reboot on the 19th of the month.\u00a0 The reboot age ($rebootAge) sets the minimum number of days the server has to be running to get rebooted.\u00a0 With the reboot age of 1, the server uptime needs to be longer than a day to qualify for a reboot.<\/p>\n<pre>[int]$rebootDay = 19\r\n[int]$rebootAge = 1<\/pre>\n<p>Lastly, the script will check three conditions and if none of them are met, it will reboot the server.\u00a0\u00a0 I added the parameters for the reboot hour into the script, but that could easily be a variable.\u00a0\u00a0 For testing I changed the -force parameter on the restart-computer command with -whatif.<\/p>\n<p>Future plans are to write the output to the event log for better diagnostics.\u00a0 I will publish once finished.<\/p>\n<p>Full Script:<\/p>\n<pre>#get the current day of the month and hour\r\n [int]$day = get-date -format dd\r\n [int]$hour = get-date -format HH\r\n#get the number of days the server has been running. Returns \"0\" if less than 24 hours\r\n $os=Get-WmiObject win32_operatingsystem\r\n $uptime = ((get-date) - ($os.ConvertToDateTime($os.lastbootuptime))).Days\r\n#declare the day of the month for a reboot\r\n [int]$rebootDay = 19\r\n#set the minimum days before reboot\r\n [int]$rebootAge = 1\r\nIf($hour -lt 2 -or $hour -gt 4){ Write-Host \"Will not reboot, outside of maintenance hours\" }\r\n ElseIf($day -ne $rebootDay) { Write-Host \"Will not reboot, outside of maintenance day\" }\r\n ElseIf($uptime -lt $rebootAge) { Write-Host \"Will not reboot, rebooted within 24 hours\" }\r\n Else{\r\n(restart-computer -force)\r\n}<\/pre>\n<p>*This script is &#8220;as is&#8221; with no warranties.\u00a0 Test it before you trust it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a script that reboots if day and time criteria are met and if the uptime is less than a given number of days. I had, what I thought, was a simple request.\u00a0\u00a0 I wanted any servers that had not rebooted as part of the monthly SCCM patch cycle to reboot at the end &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\"> <span class=\"screen-reader-text\">Safe Restart Script<\/span> 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":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[63],"tags":[71,9,73,74,76,69,64,20,65,70,66,68,75,67,72],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Safe Restart Script - ciraltos<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Safe Restart Script - ciraltos\" \/>\n<meta property=\"og:description\" content=\"This is a script that reboots if day and time criteria are met and if the uptime is less than a given number of days. I had, what I thought, was a simple request.\u00a0\u00a0 I wanted any servers that had not rebooted as part of the monthly SCCM patch cycle to reboot at the end &hellip; Safe Restart Script Read More &raquo;\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\" \/>\n<meta property=\"og:site_name\" content=\"ciraltos\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-21T03:10:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-04-28T12:14:17+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.ciraltos.com\/wp-content\/uploads\/2016\/04\/PoSh-300x300.jpg\" \/>\n<meta name=\"author\" content=\"Travis Roberts\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ciraltos\" \/>\n<meta name=\"twitter:site\" content=\"@ciraltos\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Travis Roberts\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\"},\"author\":{\"name\":\"Travis Roberts\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\"},\"headline\":\"Safe Restart Script\",\"datePublished\":\"2016-04-21T03:10:04+00:00\",\"dateModified\":\"2016-04-28T12:14:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\"},\"wordCount\":450,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\"},\"keywords\":[\"age\",\"Azure\",\"date\",\"day\",\"managment\",\"posh\",\"power shell\",\"Powershell\",\"reboot\",\"restart\",\"sccm\",\"script\",\"server\",\"system center\",\"time\"],\"articleSection\":[\"System Center\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\",\"url\":\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\",\"name\":\"Safe Restart Script - ciraltos\",\"isPartOf\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#website\"},\"datePublished\":\"2016-04-21T03:10:04+00:00\",\"dateModified\":\"2016-04-28T12:14:17+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.ciraltos.com\/staging2\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Safe Restart Script\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#website\",\"url\":\"http:\/\/www.ciraltos.com\/staging2\/\",\"name\":\"ciraltos\",\"description\":\"cloud, technology and trends\",\"publisher\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.ciraltos.com\/staging2\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\",\"name\":\"Travis Roberts\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png\",\"contentUrl\":\"http:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png\",\"width\":5657,\"height\":3563,\"caption\":\"Travis Roberts\"},\"logo\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.ciraltos.com\",\"https:\/\/twitter.com\/ciraltos\"],\"url\":\"http:\/\/www.ciraltos.com\/staging2\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Safe Restart Script - ciraltos","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/","og_locale":"en_US","og_type":"article","og_title":"Safe Restart Script - ciraltos","og_description":"This is a script that reboots if day and time criteria are met and if the uptime is less than a given number of days. I had, what I thought, was a simple request.\u00a0\u00a0 I wanted any servers that had not rebooted as part of the monthly SCCM patch cycle to reboot at the end &hellip; Safe Restart Script Read More &raquo;","og_url":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/","og_site_name":"ciraltos","article_published_time":"2016-04-21T03:10:04+00:00","article_modified_time":"2016-04-28T12:14:17+00:00","og_image":[{"url":"http:\/\/www.ciraltos.com\/wp-content\/uploads\/2016\/04\/PoSh-300x300.jpg"}],"author":"Travis Roberts","twitter_card":"summary_large_image","twitter_creator":"@ciraltos","twitter_site":"@ciraltos","twitter_misc":{"Written by":"Travis Roberts","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#article","isPartOf":{"@id":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/"},"author":{"name":"Travis Roberts","@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a"},"headline":"Safe Restart Script","datePublished":"2016-04-21T03:10:04+00:00","dateModified":"2016-04-28T12:14:17+00:00","mainEntityOfPage":{"@id":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/"},"wordCount":450,"commentCount":0,"publisher":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a"},"keywords":["age","Azure","date","day","managment","posh","power shell","Powershell","reboot","restart","sccm","script","server","system center","time"],"articleSection":["System Center"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/","url":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/","name":"Safe Restart Script - ciraltos","isPartOf":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#website"},"datePublished":"2016-04-21T03:10:04+00:00","dateModified":"2016-04-28T12:14:17+00:00","breadcrumb":{"@id":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.ciraltos.com\/staging2\/safe-restart-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.ciraltos.com\/staging2\/"},{"@type":"ListItem","position":2,"name":"Safe Restart Script"}]},{"@type":"WebSite","@id":"http:\/\/www.ciraltos.com\/staging2\/#website","url":"http:\/\/www.ciraltos.com\/staging2\/","name":"ciraltos","description":"cloud, technology and trends","publisher":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.ciraltos.com\/staging2\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a","name":"Travis Roberts","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/","url":"http:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png","contentUrl":"http:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png","width":5657,"height":3563,"caption":"Travis Roberts"},"logo":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.ciraltos.com","https:\/\/twitter.com\/ciraltos"],"url":"http:\/\/www.ciraltos.com\/staging2\/author\/admin\/"}]}},"_links":{"self":[{"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":5,"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":110,"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts\/78\/revisions\/110"}],"wp:attachment":[{"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}