Hi,
Looking for a script to expand the Hard disk on SCSI 0:0 on given VM's based on following logic,
1. Take a input of CSV with vmnames
2. Check each VM's given in csv file whether Hard Disk connected on SCSI node 0:0 matches 15GB.
3. Grab the datastore which have the hard disk and check whether the free space is greater than 46GB
4. If Hard disk equals 15GB and the datastore it hosts have free space greater than 46GB, then set a new size of 20GB for the hard disk.(i.e. increase the size by 5GB)
Thanks in advance!
Try something like this
$hdSizeGB = 15
$dsFreeGB = 46
$hdNewSizeGB = 20
Import-Csv "C:\vmnames.csv" | %{ $hd = Get-VM -Name $_.vmName | Get-HardDisk | where {$_.Extensiondata.Unitnumber -eq 0 -and
(Get-ScsiController -HardDisk $_).Extensiondata.BusNumber -eq 0} if($hd){ if($hd.CapacityKb -eq ($hdSizeGB * 1MB)){ $dsName = $hd.Filename.Split(']')[0].TrimStart('[') $ds = Get-Datastore -Name $dsName
if($ds.FreeSpaceMB -ge ($dsFreeGB * 1KB)){ Set-HardDisk -HardDisk $hd -CapacityKB ($hdNewSizeGB * 1MB) -Confirm:$false
} } } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
$hdSizeGB = 15
$dsFreeGB = 46
$hdNewSizeGB = 20
Import-Csv "C:\vmnames.csv" | %{ $hd = Get-VM -Name $_.vmName | Get-HardDisk | where {$_.Extensiondata.Unitnumber -eq 0 -and
(Get-ScsiController -HardDisk $_).Extensiondata.BusNumber -eq 0} if($hd){ if($hd.CapacityKb -eq ($hdSizeGB * 1MB)){ $dsName = $hd.Filename.Split(']')[0].TrimStart('[') $ds = Get-Datastore -Name $dsName
if($ds.FreeSpaceMB -ge ($dsFreeGB * 1KB)){ Set-HardDisk -HardDisk $hd -CapacityKB ($hdNewSizeGB * 1MB) -Confirm:$false
} } } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Works perfect. Thanks Luc, so helpful as always!
This is very similar to what I am after ....How would this work with percentages?
For example:
Select VM, select the disk, grab the datastore which host the hard disk and check whether the free space is greater than 10%
If the datastore it hosts on has free space greater than 10%, then increase the VM hard drive by x amount...
Since the script has the Datastore object in $ds, it would be a matter of calculating the percentage and then comparing it against your threshold.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$hd = Get-VM -Name xyz | Get-HardDisk
if($hd){
$ds = Get-Datastore -Name
if($ds.FreeSpaceMB -ge ($dsFreeGB * 1KB))
How does the above $ds = Get-Datastore -Name know to get the datastore of the VM specified above?
The script extracts the datastorename from the VMDK path.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference