First steps to tackle VM sprawl (…with a little, well a lot of help!)

I was recently looking into tackling VM sprawl (with no budget!) one thing I wanted to discover was when the virtual machine was created to allow for the  discovery of the count created in a certain time period, and while I was at it who created the VM.

I then found an excellent script compiled by Alan Renouf (@alanrenouf) at http://www.virtu-al.net/2010/02/23/who-created-that-vm. The script has a dependancy on vSphere PowerCLI (obviously!) and ActiveRoles Management Shell for Active Directory and creates two custom virtual machine custom fields named ‘CreatedBy’ and ‘CreatedOn’ and retrieves the values for $Event.Username and $Event.CreatedTime attributes using the Get-VIEvent cmdlet and filtering the discover to the following types;

VmBeingDeployedEvent
VmCreatedEvent
VmRegisteredEvent
VmClonedEvent

On the initial run of the above script using the WhatIfPreference I noticed that the $Event.CreatedTime would be output the date string in the following format ‘MM/DD/YYYY HH:MM:SS’ when used in a variable. Interesting enough the $Event.CreatedTime value was in the following format ‘DD/MM/YYYY HH:MM:SS’.

So how was this occurring? My conclusion was that it was due to the default culture format used in Powershell by our cousins over the pond ‘en-US’, to provide evidence to the case I added a couple of lines to the script provided to convert the date format using the CultureInfo Class as below and this resolved my issue, providing the date the VM was created in the correct format.

$Culture = New-Object System.Globalization.CultureInfo(“en-GB”)
$Created= $Date.ToString(“G”, $Culture)

So that now gives me the building blocks to see how many and when the virtual machines were created and by whom. Obviously for those in the US the script is good to go, however those of us in the UK will need to initialise the en-GB culture setting to convert the date to the correct format.

Therefore the only modification I made to the script was the final script block to include the below:

$Culture = New-Object System.Globalization.CultureInfo(“en-GB”)
$Created = $Date.ToString(“G”, $Culture)
Write “Adding info to $($VM.Name)”
Write-Host -ForegroundColor Yellow “CreatedBy $User”
$VM | Set-CustomField -Name “CreatedBy” -Value $User | Out-Null
Write-Host -ForegroundColor Yellow “CreatedOn $Date”
$VM | Set-CustomField -Name “CreatedOn” -Value $Date | Out-Null
}

Information on the CultureInfo  Class can be found here http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx and the specifiers for the string format here http://technet.microsoft.com/en-us/library/ee692801.


One thought on “First steps to tackle VM sprawl (…with a little, well a lot of help!)

  1. Hi, thanks a lot, just was exactly what I was looking for!

    I just modified your script because

    1. $Created = $Date.ToString(“G”, $Culture) was giving error since $Date is not defined before
    2. Need to manage cases when creation date is unknown.

    So I use this variant, which is working nicely:

    # Date format
    if ($Created -ne “Unknown”) {
    $Culture = New-Object System.Globalization.CultureInfo(“it-IT”)
    $Created = $Created.ToString(“G”, $Culture)
    }

    Anyway thanks a lot for your suggestion!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s