Some clues:
I want to load a csv list of vms
Look for more information regarding “Import-Csv"
I want to check for each if the vm guest has more then 2gb free space
The "diskinfo" will provide you this information
(Get-vm “VMname”).guest.disks
(The “path” and “FreeSpaceGB” properties will be relevant for your needs
However it will work only if the VMware tools are installed and properly working.
Based on the “FreeSpaceGB” you can create a “if” statement to check if it is above 2GB.
if not I would like to extend the os drive by 2gb?
Then it will be necessary to extend the disks:
First at the Virtual machine level
Look for more information regarding “set-hardisk” powercli Cmdlet
Note: It will be critical to identify all prerequisite - http://kb.vmware.com/kb/1004071 - and create conditions to prevent a disaster.
Example with a TEST VM 2012R2 on how to extend a secondary data virtual disk from 3 to 5GB
Get-vm “VMname” | get-harddisk | where {$_.name -eq "hard disk 2"} | Set-HardDisk -CapacityGB 5
Then at the OS level…
A different script will be needed for each kind of OS.
So first step get the OS type and create a “switch” statement based on it.
Then use the “Invoke-VMscript” to execute a script in the OS to extend the disk partition.
The script will be different for each OS…and it requires admin access to every OS
So to summarise this is challenging and very risky if not done properly.
Better option:
Third party monitoring solution with agents installed in all guest OS that will report these kind of issues. - Remove dependency on the VMware tools
Automatic notification to relevant IT personnel.
Let people knowing this VM and how to handle the OS perform these kind of operations.