Ping watchdog with Powershell

We have one unreliable virtual server which sometimes just loses its network connection. Until we have a permanent solution, the temporary fix is to have the computer reboot if it loses network connection.

Note: There is nothing as permanent as a temporary solution. With that warning, let's get to it.

Step 1 - Produce the following Powershell script into c:\bin\pingdog.ps1

$netup = new-object Test-Connection -quiet "goo.gl"
if( $netup -eq $False ) {
  Restart-Computer
}

Step 2 - Fix your security settings

Start Powershell. Enter Set-ExecutionPolicy RemoteSigned or whatever level you're comfortable with that'll still run your script.

Step 3 - Schedule it

If you're using Windows Server 2003 (like we, ungh) schedule a daily task to run %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\bin\pingdog.ps1

From the task's Properties, the Schedule tab, press the Advanced button. Tick Repeat Task, every 12 minutes (or whatever you fancy), and repeat for 24 hours.

If you're using Windows Server 2008 or later, you should be able to set the task to run evey x minutes right from the interface.

Step 4 - Check your logs

Keep an eye on the failing computer's system log to see if it's restarted when you looked away.

Email address policy firstname@domain with Exchange and PowerShell

If you don't know of Exchange (2010) E-mail Address policies, you should. If all your company's people should have an address firstname.lastname@somedomain.com and a secondary address firstname.lastname@somedomain.net and then suddenly a new address firstname@somedomain.com -- you need E-Mail Address Policies.

Not only that, but you also need PowerShell, since the Exchange console Email Address Policy editor does not support arbitrary email address templates. One address template it can't handle is just that, firstname@anywhere, for which you need Powershell.

Fire up your Exchange Management Shell and enter something along this line:

Set-EmailAddressPolicy -Identity 'Default Policy' -EnabledPrimaryAddressTemplate "smtp:%g.%s@somedomain.com" -EnabledEmailAddressTemplates "smtp:%g.%s@somedomain.net", "smtp:%g@somedomain.com"

That last bit was the magic one. Substitute the 'Default Policy' to whatever policy you're editing and the somedomain.com to suite your organization.