I am just starting to learn powershell so be patient! I'm looking for a way to connect to our VC or one of the hosts and list all of the virtual servers and their individual drive capacities and also the freespace for each drive. I just started playing around with powershell yesterday and found that if I do "get-vm|fc" it will list out alot of detail about each virtual machine including some lines like below. As far as getting the drive listings for each vm with capacity and freespaceinto a nice formatted report, I'm at a loss. Anyone wanna help? I'm looking through some other scripts to get examples but thought I'd through this out there and see if anyone else (who has better powershell skills!) could help out some.
class VirtualMachineImpl
{
PowerState = PoweredOn
Description =
Guest =
class VMGuestImpl
{
OSFullName = Microsoft Windows Server 2003, Standard Edition (32-bit
IPAddress =
[
REMOVED
]
State = Running
Disks =
[
class DiskInfoImpl
{
Path = C:\
Capacity = 8578932736
FreeSpace = 1092395008
}
class DiskInfoImpl
{
Path = D:\
Capacity = 10733219840
FreeSpace = 10666061824
}
]
After connecting to Virtual Center or an ESX server (Connect-VIServer SERVERNAME), you can use the following one-liner:
Get-VM | Get-VMGuest | Select VmName -ExpandProperty Disks
If you want to have it as a csv file (to import in Excel), pipe it to Export-Csv D:\scripts\vmdisks.csv
If you'd rather have it saved in html, pipe it to ConvertTo-Html | Out-File D:\scripts\vmdisks.html
You might want to set $errorActionPreference = SilentlyContinue first, because powered off vm's will cause an error. Or use:
Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VmName -ExpandProperty Disks
Hope that helps.
Hugo Peeters
Or, if you want to get really fancy, try this one-liner:
Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VmName -ExpandProperty Disks | Select VmName, Path, @{ N="PercFree"; E={ [math]::Round( ( 100 * ( $_.FreeSpace / $_.Capacity ) ),0 ) } } | Sort PercFree
You'll get an overview sorted by Percentage of free space.
Excellent! Thanks to all of you. This definitely gives me a place to start and will certainly help in my quest for more powershell scripting abilities.
Ok .. but i've some vm with Linux like SO... and i cannot find freespace disk property ... .. there's a solution?
Same here, for Linux RHEL5 with LVM, the command reports only the /boot partition infomation which is useless, instead of the / which is the one that we need.
What is reported by Select VmName -ExpandProperty Disks
Path Capacity FreeSpace
/boot 103512064 79201280
What df reports (in bold what is missing from the powershell output):
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
28G 13G 14G 48% /*
/dev/sda1 99M 24M 71M 25% /boot
So there's no way to get freedisk space on VM Linux machine ????
There is a way to get free space on Linux guests (or any guest in any power-state for that matter). I outline the method in a post on my blog at VMtoday. My method is not a VI Toolkit for Windows solution, although it does use PowerShell to run a SQL Query against the VirtualCenter database. Check out the post for more info....
Josh
I'm looking for an update from VMware: is there a way to get a reliable free space via this toolkit for Linux VMs? For Windows this works, for Linux nos so much...
$disks = Get-VMGuest -VM | select VmName -ExpandProperty Disks
#it works well if a VM has only one vDisk
#If a VM has more than one disk, necessary this:
$report = @()
foreach($vmd in $disks){
$row = "" | select Name, Capacity, FreeSpace
$row.name = $vmd.VmName
$row.FreeSpace = $vmd.FreeSpace
$row.capacity = $vmd.Capacity
$report += $row
}