Server Reboot Script

Running a little low on content this last few months, plus I’ve been busy with other work stuff.

I had the requirement to create a PowerShell script that would get the uptime of a server and then decide whether or not the server needed rebooting.

I also wanted the script to randomize the reboot of the servers, that way if there are multiple servers that need rebooting at once, they don’t cause a power spike or resource issues on the hosts. I did this by creating a random number between 1 and 5 and then if the number equals 5, the server is rebooted. If not then the server isn’t rebooted.

This is the script that I ended up with and what is currently being tested:

$loglocation = "C:\scripts\reboot\log"
$dateforfile = Get-Date

$lastbootuptime = Get-WmiObject win32_operatingsystem
$uptime = (Get-Date) - ($lastbootuptime.converttodatetime($lastbootuptime.lastbootuptime))
$uptimeindays = $uptime.days

$randomnumber = Get-Random -Minimum 1 -Maximum 6

if ($uptimeindays -ge "14"){

 Add-Content -Path "$loglocation\$env:COMPUTERNAME.txt" -Value @"
Server restarted at:
This was an immediate shutdown as the server had been up for $uptimeindays days

 Restart-Computer -Force

}elseif ($uptimeindays -lt "14" -and $uptimeindays -ge "7"){

    if ($randomnumber -eq "5"){

        Add-Content -Path "$loglocation\$env:COMPUTERNAME.txt" -Value @"
Server restarted at :
This was a random restart as uptime was only $uptimeindays days
        Restart-Computer -Force

        Add-Content -Path "$loglocation\$env:COMPUTERNAME.txt" -Value @"
Server NOT restarted
This was not randomly restarted. Uptime is currently $uptimeindays days. Random number was $randomnumber

Add-Content -Path "loglocation\$env:COMPUTERNAME.txt" -Value @"
No restart required
No restart required since uptime is only $uptimeindays days

The first time I created this script and set it up as a scheduled task, nothing happened. Turns out that I needed the -Force parameter in order for the server to be rebooted.

This will later be used in a group policy without the log creating as that is only necessary in the testing stage.



Custom PowerShell Environment and Modules

To add custom PowerShell modules to your PowerShell environment, you first need to find out where you PowerShell profile is. You can do this by typing in:


into a PowerShell prompt. It should look like the following:


Now we need to test if the path actually exists. To do this type:

Test-Path $profile

If the prompt returns $true, your good. If it returns $false then you will need to run the following command:

New-Item -Path $profile -Itemtype file -Force

Now that is done we should be able to open the file in notepad. You can either browse to it following the path in $profile or type in:

notepad $profile

This should open a blank text file:

$profile empty

In here is where you specify PowerShell to look for custom modules and can even add text to the PowerShell prompt. For example, if I had the following to the notepad, it will also be displayed when I open a new PowerShell window:

custom $profile (write-host)

But we can also use this to load custom functions into our PowerShell environment. To do this, got to the $profile location, here you should find a folder called “Scripts”

Scripts folder

Go into the folder and create a new folder, mine is called “autoload”


Here is where you create your custom functions/ scripts. For example, here is what I have:


where each file contains a single function.

Now that were done with the easy part. You want to go back into your $profile notepad and add the following in order for PowerShell to load your customer functions:


get-childitem "${psdir}\*.ps1" | %{.$_}

You can see from my file. I have multiple files to make for easier sorting of my functions. I also have a custom start screen to list all of my current commands so that I don’t forget them. You can see that below:

$profile example

Just so that you can see, this is what my PowerShell prompts look like:

My prompt

For a list on verbs and commands that you can use, visit the Microsoft website and forums. You DON’T want to use verbs or commands that are used else where or that have a unique purpose. Choose verbs that are different. Good luck. Enjoy!

Customized Email Signatures In HTML

Back off holiday, want to do something simple. So email signature it is.

I made mine in the Mimecast administrator center as a way to become more familiar with HTML so that I could create an actual signature for some users.

Before we look at the code – I will put an image below of what the signature would actually look like. Also, I own NONE of the images/sites shown here. (Sorry, just didn’t want legal people sniffing around).


Now that you have seen the signature, you can see that is it in no way a real world example of something that should be used. But I also couldn’t use the actual one for…reasons. I just wanted to show you every feature I used in the real one.

Below is the actual code I used:

  <body><mc type="body">
  <p style="font-family:calibri:">This is a <b>test</b> email signature</p>

  <img src="" alt="TEST LEFT" style="float:left;width:50px;height:20px;" />
  <p style="font-family:courier;">This is the second line of the test email signature</p>
  <p style="font-family:verdana;font-size:80%;">This is the last line of the test email signature</p>

  <img src="" alt="TEST IMAGE" style="width:200px;height:70px;" />

  Click <a href="" target="_blank"><i>here</i></a> to open a link


You may notice that I have linked Krebs blog – Enjoy!

Adding Signatures In Mimecast

Bit different from, the now usual, Powershell guides. Thought I’d do something different. This guide will show you how to add a custom signature to a user, multiple users’ or even globally.

Lets start off. You’ll want to log into your Mimecast administrative console, go to Service-Gateway-Policies. In here you should see “Stationary”. We want to create a new definition, so click on the “Definition” button on the “Stationary” listing.

Here you can click the “New Item” button. For the Description, obviously, enter a brief description of the new definition. The “Unique Identification Text” is used to scan an email that the definition is applied to to determine if the signature has already been added, so you want to add something that is in the signature and something that is unique to this signature. For the short code, enter a unique name. Below is an example of mine:


Now we want to edit the HTML so that there actually is a signature to put onto the emails. Click on the “Edit HTML” button and enter your signature. This is the code I added to mine:

    <mc type="body">

    <font size="2" color="gray">
    This is a test email signature for marketing

    <img src="IMAGE LINK" alt="ALTERNATIVE IMAGE NAME" style="width:214px;height:24px:" />


So in my signature I have the message “This is a test email signature for marketing” and also a company logo.

Now we have that, we need to add it to an actual policy, otherwise it simply won’t be used by anything. Navigate back after saving your definition to the “Policies” page and click on the “Stationary” row. You now want to click “New Policy” which should open up a new policy creation window.

For the “Policy Narrative” enter a name for the policy, for the “Select Stationary”, select “lookup”, find your newly created definition and click “Select”. Since I created mine as a test and my Mimecast access is on my business email address, I configured an individual send and to email address for the policy to apply to. On this page you can also select when the policy will be active or even a range of source IP addresses.

Below are the values that I entered. The emails are removed for obvious reasons:


Finally, below is an example of an email with the newly added signature:


You should now have a custom signature that applies to every user defined in the policy instead of individual users.