PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

Restart stopped services which are set to automatic

This script is handy for your Exchange 2010 deployments, it will find any automatic service which is stopped and start it (optional) and let you know that it happened. This is similar to a previous scripts I have posted.

As usual it uses my usual template.

Read More
PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

Why you reboot?

I wanted to know when and by what/who/why servers rebooted. This is just a simple script, and you configure the it to run at start up in Windows task scheduler.

Read More
PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

XML + PowerShell = Really neat config files

There are often times where you need to store data within a file to make your script work, examples include:

Complex configuration information
Historical information
Complex data storage
There are a number of ways you can address this when writing a normal .Net application, but the majority of these can easily be used within Powershell. These options include simple text files, SQL databases and what I want to talk about today, XML files!

Using XML files is very simple in PowerShell, most of the hard work will be performed by the Powershell engine and the .Net libraries it is built upon.

Example XML File

Reading data within an XML file
To read an XML file into a variable, simply

Now if we wanted to access all of the XML elements under the "Folders" element, all we would have to do is:

But if there are no "folder" elements below "folders", you will have a nice error message when you go to run that foreach loop! You can resolve this by testing if there are elements under the "folders" element by:

if the above returns false, then there are no elements sitting under "folders". Note that this command is CASE SENSITIVE!!! folders and Folders are not the same. BEWARE!

This is enough for today, and should allow you to understand some of the scripts I will be posting up soon.

 

 

Read More
PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

Server Up Time Monitoring

These days, servers, even ones running Windows, can be up for a very long time. I recently had a request where they wanted an alert generated if a server had be up for longer than a time period.

I modify the script that can be found here, http://pshscripts.blogspot.com.au/2009/01/get-uptimeps1.html, to fit my standard template and just my preferred style.

Read More
PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

Volume Health Checks in PowerShell

I was asked to write a script to monitor the health of the volumes for our web application farm. The original request was to monitor how much disk space was free on each volume, however I wanted the ability to monitor the integrity and fragmentation of the volume as well.

The first issue was we wanted to exclude the 100mb partition that Windows creates for the boot up process. This doesn't generally change and we didn't want it causing too much noise.

Checking the fragmentation status of a volume is available through WMI, through WMI you can easily ask Windows if that particular volume isfragment and get a True or False response.

File system integrity is a little different. NTFS has a dirty flag, which is used by check disk during boot to determine if it should perform a check on that volume. One annoying issue is there are two methods to check this flag, WMI or the CHKNTFS.EXE, WMI is considered to be more reliable, but there are many discussions on the Internet in regards to how reliable it is. CHKNTFS cannot run on a drive without a drive letter, so you need to be careful when scripting with it. In the end, my script will perform both tests where possible.

Using my usual template, the code below is for the body.

 If you want to skip the 100mb system drives, set $thresholdexcludesystem to $true

Read More
PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

Exporting the registry

When it comes to manipulating the registry, PowerShell gives you a truckload of options, personally, if its something as simple as backing up and restoring entries, nothing beats the proven old fashioned methods.

I needed to backup some registry keys recently, this will be running in my usual template as a scheduled task. I wanted to know if there was every an issue when trying the export.

My code looked like:

$key is the registry path you want to export, and $destfile is the resulting .reg file you want to export to.

Read More
PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

Physical Disks, SMART and PowerShell

It is always nice to know when a physical hard disk is about to fail. The SMART status of a drive in Windows is the best way to know that there is potentially a problem comming up, SMART will often be the first sign of a drive failure, but Windows is yet to provide a great way of telling you what SMART is trying to tell it.

Another issue, is through Disk Manager and Device Manager, it can be difficult to get the serial number of a disk, leading to difficulty in identifying the disk that is failing when you do go to replace it.

I discovered that WMI actually can provide a very good interface with the SMART status, as well as a great mechanism for getting the serial number of a disk as well!

I created a script using my usual template, with the following in the body. I have scheduled this script to run hourly.

 

Read More
PowerShell Kieran Jacobsen PowerShell Kieran Jacobsen

Web Application Warming

Original Issue: Web application is a bit unstable, restarting IIS seems to resolve issues for a few days.

Solution to original issue: Powershell script to restart application components including IIS in early hours of morning.

... A few weeks go by ...Users happy that application is significantly more stable during the day, however a small group of users complain about the performance of the application. Why is the performance suddenly dropping for those users?

First look at users issues: These users are always the first in the office, some appear not to sleep and decide to go to work at very early hours.

Root Cause: Now that we are restarting IIS, ASP.NET is recompiling the dynamic code when it is accessed for the first time, thus causing performance issues for first user hitting each part of the web site.

Solution 1: Use the Microsoft Web Application Warm up tool

Problem with Solution 1: Microsoft pulled the tool from their site

Solution 2: Write a script to handle this issue!

 

Writing a script for this solution was pretty easy:

  1. Get listing of all web pages which form application, put in file of format:
    index.aspx
    contact/contact_us.aspx

    The reason to do this is the application resides in multiple SDLC environments with different base URLs but the same relative paths
  2. Put the list from step 1 into a file
  3. Get the wget.exe from the gnuwin32 project. Why this and not the .net framework? Simply, put, from my experience the wget.exe was better at forcing all of the content to be loaded correctly in a manner a browser would. Go here for more info on the gnuwin32 project http://gnuwin32.sourceforge.net
  4. Based on the usual template:

Read More