Highlighted

How to check how much disk space left inside Guest OS?

Jump to solution

Based on the PowerCLI on-line help, we can create a new virtual disk then format it with NTFS.

But seems like we can't get inside the Guest OS and check how much save left in C:\ drive or D:\ drive. We can do this by using Get-WMIObject (Good example here --> http://jtruher3.wordpress.com/2006/09/08/getting-disk-usage-information/).

But using WMI  means the Guest OS needs to be visible on the network, firewall ports opened, WMI services must be up, and users must be authorised.

Is there is a method via VMware Tools?

Many thanks in advance

e1

e1
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

Hi Iwan,

the next PowerCLI script returns the capacity and freespace of all the guest disks of all your virtual machines via the VMware Tools.

Get-VM | ForEach-Object {
  $VM = $_
  $_.Guest.Disks | ForEach-Object {
    $Report = "" | Select-Object -Property VM,Path,Capacity,FreeSpace
    $Report.VM = $VM.Name
    $Report.Path = $_.Path
    $Report.Capacity = $_.Capacity
    $Report.FreeSpace = $_.FreeSpace
    $Report
  }
}


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
7 Replies
Highlighted
User Moderator
User Moderator

Did you already try the Invoke-VMScript cmdlet ?

It allows you to launch a script (bat or PS) inside the OS of the guest.

And the output of that script is returned to your PowerCLI script.

A simple example that should work on most Windows guest OS

$vmName = "MyVM"
$vm = Get-VM -Name $vmName 
$result
= Invoke-VMScript -VM $vm -ScriptType bat -ScriptText 'dir c: | find "bytes free"' `
    -HostUser $hostUser -HostPassword $hostPswd `
    -GuestUser $guestUser -GuestPassword $guestPswd
$result

You have to provide credentials for ESX(i) host and the guest when you're connected to a vCenter.

When you're connected to an ESX(i) server you can leave out the host credentials.

You can manipulate the result with simple string functions like Split or you can use a [regex] expression to interprete the result.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Highlighted
User Moderator
User Moderator

Hi Iwan,

the next PowerCLI script returns the capacity and freespace of all the guest disks of all your virtual machines via the VMware Tools.

Get-VM | ForEach-Object {
  $VM = $_
  $_.Guest.Disks | ForEach-Object {
    $Report = "" | Select-Object -Property VM,Path,Capacity,FreeSpace
    $Report.VM = $VM.Name
    $Report.Path = $_.Path
    $Report.Capacity = $_.Capacity
    $Report.FreeSpace = $_.FreeSpace
    $Report
  }
}


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
Highlighted

Thank you Rob!

For others who might be reading this, I ran Rob's script againts both Windows and Linux and pretty quickly it starts printing the results. The .Path property shows you the name of the mount point, which is nice. This means in Windows, it shows the drive name (e.g. C drive, D drive).

Cheers!

e1

e1
0 Kudos
Highlighted
Contributor
Contributor

Hello Rob,

Thanks for the script, can we add a line to the script to also get how much free space is available in percentage value %. means that this much percentage % is free in a new column.

Thanks & Regards,

BabuRaju.

0 Kudos
Highlighted
User Moderator
User Moderator

Hi BabuRaju,

the following modification of the script also shows the percentage of free space for each disk:

Get-VM | ForEach-Object {

  $VM = $_

  $_.Guest.Disks | ForEach-Object {

    $Report = "" | Select-Object -Property VM,Path,Capacity,FreeSpace,PercentageFreeSpace

    $Report.VM = $VM.Name

    $Report.Path = $_.Path

    $Report.Capacity = $_.Capacity

    $Report.FreeSpace = $_.FreeSpace

    if ($_.Capacity) {$Report.PercentageFreeSpace = [math]::Round(100*($_.FreeSpace/$_.Capacity))}

    $Report

  }

}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Highlighted
Contributor
Contributor

Hi

Thanks for the script, can we add a line to the script to also get Used Space ?

Thanks

0 Kudos
Highlighted
User Moderator
User Moderator

Isn't that just substracting FreeSpace from Capacity?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference