Friday, February 6, 2015

Discovering Windows Version on EC2 Instances

Windows Server 2003 end of life is less than six months away. As I start to think about upgrading, I was looking for an easy way to identify what version of Windows is running on each EC2 instance.  I would like to do this without having to log into each instance.

One solution is to use the System log.  If the instance has the EC2 Config service running on it it will report the OS version (along with a few key driver versions to the console).  You can access the System Log from the console by right clicking on an instance and choosing "View System Log".  For example, the output below is from a Windows 2003 R2 instance I just launched.  Notice the OSVersion on line three.

2015/02/06 11:50:30Z: AMI Origin Version: 2015.01.14
2015/02/06 11:50:30Z: AMI Origin Name: Windows_Server-2003-R2_SP2-English-64Bit-Base
2015/02/06 11:50:30Z: OsVersion: 5.2
2015/02/06 11:50:30Z: OsServicePack: Service Pack 2
2015/02/06 11:50:30Z: OsProductName: Microsoft Windows Server 2003 R2
2015/02/06 11:50:30Z: OsBuildLabEx: NotFound
2015/02/06 11:50:30Z: Language: en-US
2015/02/06 11:50:30Z: EC2 Agent: Ec2Config service v2.3.313.0
2015/02/06 11:50:30Z: EC2 Agent: Ec2Config service fileversion v2.3.313
2015/02/06 11:50:32Z: Driver: Citrix PV Ethernet Adapter v5.9.960.49119
2015/02/06 11:50:32Z: Driver: Citrix PV SCSI Host Adapter v6.0.2.56921
2015/02/06 11:50:33Z: Message: Waiting for meta-data accessibility...
2015/02/06 11:50:34Z: Message: Meta-data is now available.
2015/02/06 11:50:47Z: AMI-ID: ami-529be93a
2015/02/06 11:50:47Z: Instance-ID: i-01bcc4f0
2015/02/06 11:50:47Z: Ec2SetPassword: Enabled
2015/02/06 11:50:48Z: RDPCERTIFICATE-SUBJECTNAME: O=Amazon.com, OU=EC2, CN=i-01bcc4f0
2015/02/06 11:50:48Z: RDPCERTIFICATE-THUMBPRINT: 965A5E6BF3DD3A31C66B4BE78D2B41735A10B540
2015/02/06 11:50:50Z: Username: Administrator
2015/02/06 11:50:50Z: Password: 
2015/02/06 11:50:50Z: Message: Windows is Ready to use

I created a script that will query the system log (also called the console) from every Windows instance in every region using PowerShell.  It then applies a regular expression to parse the OS version number.

Get-EC2Region | % {
    $Region = $_.RegionName

    (Get-EC2Instance -Region $Region).Instances | Where-Object {$_.Platform -eq "Windows"} | % {
        $Instance = $_
        #Get the console output for each instance
        $Console = (Get-EC2ConsoleOutput -Region $Region -InstanceId $Instance.InstanceId).Output
        $Console = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Console));

        #Parse the console output looking for OS and Driver versions.  Note that different EC2Config versions use different formats.
        $OSVersion1 = If($Console -match "OS: Microsoft Windows NT (?[\d.]*)"){ $Matches['OSLongVersion'] };
        $OSVersion2 = If($Console -match "OsVersion: (?[\d.]*)"){ $Matches['OSShortVersion'] };
        $OSVersion = if($OSVersion1 -ne $null) { $OSVersion1 } Else { $OSVersion2 }
        $NICDriver1 = If($Console -match "Driver: AWS PV Network Device v(?[\d.]*)"){ $Matches['NICDriver'] };
        $NICDriver2 = If($Console -match "Driver: Citrix PV Ethernet Adapter v(?[\d.]*)"){ $Matches['NICDriver'] };
        $NICDriver = if($NICDriver1 -ne $null) { $NICDriver1 } Else { $NICDriver2 } 
        $HBADriver1 = If($Console -match "Driver: AWS PV Storage Host Adapter v(?[\d.]*)"){ $Matches['HBADriver'] }
        $HBADriver2 = If($Console -match "Driver: Citrix PV SCSI Host Adapter v(?[\d.]*)"){ $Matches['HBADriver'] }
        $HBADriver = if($HBADriver1 -ne $null) { $HBADriver1 } Else { $HBADriver2 } 

        #Add a new row to the output
        New-Object PSObject -Property @{
            Region = $Region
            InstanceId = $Instance.InstanceId
            InstanceName = ($Instance.Tags | Where-Object {$_.Key -eq "Name"}).Value
            ImageId = $Instance.ImageId
            OSVersion = $OSVersion
            NICDriver = $NICDriver
            HBADriver = $HBADriver
        }
  
    }
 } | Format-Table -AutoSize -Property Region, InstanceName, InstanceId, ImageId, OSVersion, NICDriver, HBADriver                

You can see the sample output below.  The last instance is Windows 2003 indicated by the version number 5.2.  You can find a list of version numbers on Microsoft's Web Site.

Region    InstanceName       InstanceId ImageId      OSVersion  NICDriver     HBADriver  
------    ------------       ---------- -------      ---------  ---------     ---------  
us-east-1 SERVER001          i-583480b4 ami-e49a0b8c 6.2        5.9.960.49119 6.0.2.56921
us-east-1 SERVER002          i-da8c5236 ami-c4fe9aac 6.3        7.2.4.1       7.2.4.1    
us-east-1 SERVER003          i-b316c842 ami-c4fe9aac 6.3        7.2.4.1       7.2.4.1    
us-east-1 SERVER004          i-bee1715f ami-7614ac1e 6.1.7601   5.9.960.49119 6.0.2.56921
us-east-1 SERVER005          i-01bcc8f0 ami-529be93a 5.2        5.9.960.49119 6.0.2.56921
Now a few warnings.  First, you must have EC2 Config installed in the instance to output information to the console.  Second, depending on the version of EC2 config the output format is different.  I have discovered two versions in my testing, but there may be others.  Third, I have noticed that you cannot always distinguish the R2 version of an OS.

Blogger is messing with the script a bit.  You can download a copy here.  Just rename it from .txt to .ps1.

No comments:

Post a Comment