VMware Cloud Community
pshankland
Contributor
Contributor
Jump to solution

Get-VMDiskUsage

Hi all,

I have just purchased "Managing VMware Infrastructure with Windows PowerShell" and am really impressed with what you can do - first go with PowerShell Smiley Happy

Having gone through some of the book I came across the following script which is meant to pull out the VM name, CapacityMB, FreeSpaceMB and USage % but I can't get it to work by using the command "Get-VM | scripts:Get-VMDiskUsage.ps1"

########################################################################################

    1. VM Tools must be in "green" state and VM must be powered on

Begin {

  1. This hides the errors which appear if the VM Tools are not running or if VM is off

$ErrorActionPreference = "SilentlyContinue"

$VMNameExp = @{

Name = "Name"

Expr = { $VMName }

}

$CapacityExp = @{

Name = "CapacityMB"

Expr = { "{0:n2}" -f ( $_.Capacity / 1MB ) }

}

$FreeSpaceExp = @{

Name = "FreeSpaceMB"

Expr = { "{0:n2}" -f ( $_.FreeSpace / 1MB ) }

}

$UsageExp = @{

Name = "Usage %"

Expr = { "{0:p2}" -f ( $_.FreeSpace / $_.Capacity ) }

}

Process {

if ( $_.GetType().Name -ne 'VirtualMachineImpl' ) {

Throw "This script expects a VirtualMachineImpl object as imput."

}

$VMName = $_.Name

$_ | Get-VMGuest | Select-Object -ExpandProperty Disks |

Select-Object $VMNameExp, $CapacityExp, $FreeSpaceExp, $UsageExp

}

########################################################################################

Any help would be appreciated as would eventually like to get this Emailed as a small, simple daily report.

Thanks.

Pete.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are connected to the vCenter ?

Did you run

Connect-ViServer -Server <your-VC-server>

before you start the script ?

I attached my copy, perhaps try that one.

This is what you should get

You could comment out the $ErrorActionPreference line, that way you will see the error messages.


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

The problem is caused by the fact that you are trying to use a provider (scripts:) which is not on your client.

Hal seems to have defined a provider for his scripts and apparently that provider made it to the printed copy.

If you're in the same directory (do a pwd at the prompt to check) as where you saved the script, you can do

Get-VM | .\Get-VMDiskUsage.ps1

else you can also give the full path to the script.

For example

Get-VM  | C:\Scripts\Get-VMDiskUsage.ps1


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

Reply
0 Kudos
pshankland
Contributor
Contributor
Jump to solution

Hi,

Yeah tried "Get-VM | .\Get-VMDiskUsage.ps1" and that seems to just run the script and then return me to the prompt - no error but no output.

Any other suggestions?

Thanks.

Pete.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are connected to the vCenter ?

Did you run

Connect-ViServer -Server <your-VC-server>

before you start the script ?

I attached my copy, perhaps try that one.

This is what you should get

You could comment out the $ErrorActionPreference line, that way you will see the error messages.


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

Reply
0 Kudos
pshankland
Contributor
Contributor
Jump to solution

Thanks for the help LucD! It was a typo on my part - I had missed of a '}' before the 'Process' Smiley Sad

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

The problem is caused by the fact that you are trying to use a provider (scripts:) which is not on your client.

Hal seems to have defined a provider for his scripts and apparently that provider made it to the printed copy.

Oops! In my defense, Luc was basically the senior technical editor and he didn't catch the bug, so it's his fault. :smileygrin:


[vExpert|http://www.vmware.com/communities/vexpert/], PowerShell MVP, VI Toolkit forum moderator

Author of the book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I humbly bow my head in shame Smiley Wink

I'll try to do a better job when Hal finishes the 2nd edition.


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

Reply
0 Kudos
tjw82
Contributor
Contributor
Jump to solution

I was wondering how I could write this file out to a csv file and make it look right?

This is what I have, but it does not look like what it does when it writes out to the screen

$_ | Get-VMGuest | Select-Object -ExpandProperty Disks | ConvertTo-Csv | Set-Content c:\vms.csv

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could use the Export-Csv cmdlet.

Something like this

$_ | Get-VMGuest | Select-Object -ExpandProperty Disks | Export-Csv "C:\Report.csv" -NoTypeInformation -UseCulture

Note that the -Userculture parameter is a PowerShell v2 RTM feature.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos