Hi everyone,
As a part of the clean-up of my first post,
there was a requirement to move the VMDKs from the 3rd Parties
datastores, to our own datastore cluster.
Because of wanting to stagger the VMDK migrations I
decided to write a separate script rather than tacking it on this on to the end
of the previous script.
The first portion is producing some nice reporting. I used the Convertto-html cmdlet for this and
works quite well I think!
After the report is produced the decision is there to be
made by the user as to whether there is the space to migrate the requesteded amount
of VMDKs.
The script is designed to be run peace-meal as
personally I didn’t like the idea of terabytes of data flooding the storage
network!
Here it is:
#######################################################################
# Script Name:
VMDK_Location_Check.ps1
# Purpose: To migrate VMDKs from
non-compliant datastores
#
#
# Created by: Chris Lawrence
http://hoipoiloi.blogspot.com
# Date: 01/08/2013
#######################################################################
#Variables. Please amend as required:
#How many VMDKs you want to move
at once
$X = 5
#Datastore you want to move the
VMDKs to
$Datastore = Get-datastore
-Name "Your Datastore"
#------------
#Sets some manadatory variable
$Administrator = [Environment]::UserName
$Path = "C:\Users\$Administrator\Documents"
$VMDKLocation = Get-vm | Get-HardDisk | Where {$_.filename -notmatch "DATASTORE"}
$TotalGB = $VMDKLocation | Measure-Object CapacityGB -Sum
$GBsInX = $VMDKLocation |Select -First "$X" | Measure-Object CapacityGB -Sum
$FreeSpace = $Datastore.freespaceGB - $gbsinX.sum
$FreeSpace = "{0:N0}" -f $Freespace
#------------
#Report creation
#Pre-define some table and header
styles using HTML
$Header = @"
<style>TABLE {border-width:
1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding:
3px;border-style: solid;border-color: black;background-color: #A74AC7;}
TD {border-width: 1px;padding:
3px;border-style: solid;border-color: black;}
</style>
"@
#Create some HTML code using the
Convertto-html cmdlet.
$Frag1 = $totalgb | ConvertTo-HTML -Property Count, Sum -As Table -Body "<H2>Total
VMs and Total GBs non-compliant</H2>"
$Frag2 = $GBSinX | ConvertTo-HTML -Property Count, Sum -As Table -Body "<H2>Total
specified VMs and GBs to be moved</H2>"
$Frag3 = ConvertTo-HTML -Body "<H2>Free
space is $Freespace(GBs) in $datastore once $X machines have been made
compliant</H2>"
$Frag4 = $VMDKLocation | ConvertTo-HTML -Body "<H2>Complete
list of the non-compliant VMs</H2>" -Property Parent,
CapacityGB, FileName -As Table -Head $Header
#Combine these html fragments to
one report:
ConvertTo-Html -Body "$Frag1
$Frag2 $Frag3 $Frag4" -Title "VMDK Location Report" | Out-File $path\Report.html
Invoke-Expression -Command $path\Report.html
Write-host -ForegroundColor Green "
If
the amount of free space is acceptable, hit ENTER to
run
the SvMotion portion of the script
If
not press Control-C to quit the script"
Read-Host
$Datastores = $vmdklocation | Select -First "$X"
Foreach ($VM in $Datastores)
{
Move-VM -VM $VM.parent -Datastore "Datastore" -RunAsync
}
You can put as many of
the Where {$_.filename -notmatch "DATASTORE"} lines in
as you want, depending on how many datastores you have that are within your “compliant”
list. Just repeat the comment separated
by a | . So:
Where {$_.filename -notmatch "DATASTORE"} | Where {$_.filename -notmatch "DATASTORE"}
Other
than that, happy scripting!
Chris.