YanSi
Enthusiast
Enthusiast

About PowerCLI - Disk Space Utilization

Jump to solution

I use the following script to execute

# Establishing the connection to our vSphere object
$vm = Get-VM -Name "vm01-a"
$vmView = $vm | Get-View
# Creating a new PowerShell custom object to easily output properties
$output = New-Object -TypeName PSCustomObject
# Adding desired properties to the new PowerShell object
$diskCommit = $vmView.Storage.PerDatastoreUsage.Committed
$diskUncommit = $vmView.Storage.PerDatastoreUsage.Uncommitted
Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceGB -Value ($diskCommit /1GB)
Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceGB -Value (($diskCommit + $diskUncommit) /1GB)
$diskKey = $vmView.LayoutEx.Disk.Key
$diskCapacity = $vmView.Config.Hardware.Device | Where-Object {$_.key -eq $diskKey} | Select-Object -ExpandProperty capacityInBytes
Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityGB -Value ($diskCapacity /1GB)
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vmView.Guest.Disk.DiskPath
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityGB -Value ($vmView.Guest.Disk.Capacity)
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceGB -Value ($vmView.Guest.Disk.FreeSpace)
# Viewing the output
$output | FL

get the following results:

PS C:\Users\Administrator> $output | FL

UsedSpaceGB        : 4.27435741666704
ProvisionedSpaceGB : 5.57904542703182
CapacityGB         : 5
GuestPath          : {/, /boot}
GuestCapacityGB    : {3743416320, 1063256064}
GuestFreeSpaceGB   : {1517223936, 887902208}

I don't know how to get an integer result(GuestCapacityGB \ GuestFreeSpaceGB etc.....),For example, press GB or MB

If use this code

# Establishing the connection to our vSphere object
$vm = Get-VM -Name "vm01-a"
$vmView = $vm | Get-View
# Creating a new PowerShell custom object to easily output properties
$output = New-Object -TypeName PSCustomObject
# Adding desired properties to the new PowerShell object
$diskCommit = $vmView.Storage.PerDatastoreUsage.Committed
$diskUncommit = $vmView.Storage.PerDatastoreUsage.Uncommitted
Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceGB -Value ($diskCommit /1GB)
Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceGB -Value (($diskCommit + $diskUncommit) /1GB)
$diskKey = $vmView.LayoutEx.Disk.Key
$diskCapacity = $vmView.Config.Hardware.Device | Where-Object {$_.key -eq $diskKey} | Select-Object -ExpandProperty capacityInBytes
Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityGB -Value ($diskCapacity /1GB)
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vmView.Guest.Disk.DiskPath
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityGB -Value ($vmView.Guest.Disk.Capacity /1GB)
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceGB -Value ($vmView.Guest.Disk.FreeSpace /1GB)
# Viewing the output
$output | FL

will get the following results:

PS C:\Users\Administrator> # Establishing the connection to our vSphere object
PS C:\Users\Administrator> $vm = Get-VM -Name "vm01-a"
PS C:\Users\Administrator> $vmView = $vm | Get-View
PS C:\Users\Administrator> # Creating a new PowerShell custom object to easily output properties
PS C:\Users\Administrator> $output = New-Object -TypeName PSCustomObject
PS C:\Users\Administrator> # Adding desired properties to the new PowerShell object
PS C:\Users\Administrator> $diskCommit = $vmView.Storage.PerDatastoreUsage.Committed
PS C:\Users\Administrator> $diskUncommit = $vmView.Storage.PerDatastoreUsage.Uncommitted
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceGB -Value ($diskCommit /1GB)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceGB -Value (($diskCommit + $diskUncommit) /1GB)
PS C:\Users\Administrator> $diskKey = $vmView.LayoutEx.Disk.Key
PS C:\Users\Administrator> $diskCapacity = $vmView.Config.Hardware.Device | Where-Object {$_.key -eq $diskKey} | Select-Object -ExpandProperty capacityInBytes
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityGB -Value ($diskCapacity /1GB)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vmView.Guest.Disk.DiskPath
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityGB -Value ($vmView.Guest.Disk.Capacity /1GB)
InvalidOperation: Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceGB -Value ($vmView.Guest.Disk.FreeSpace /1GB)
InvalidOperation: Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
PS C:\Users\Administrator> # Viewing the output
PS C:\Users\Administrator> $output | FL

UsedSpaceGB        : 4.27435741666704
ProvisionedSpaceGB : 5.57904542703182
CapacityGB         : 5
GuestPath          : {/, /boot}


PS C:\Users\Administrator>

I don't know how to solve this problem

0 Kudos
1 Solution

Accepted Solutions
YanSi
Enthusiast
Enthusiast

Thanks you very Much !

I saw this post solved my doubts

https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/get-vmguest-C-drive/td-p/527634

PS C:\Users\Administrator> # Establishing the connection to our vSphere object
PS C:\Users\Administrator> $vm = Get-VM -Name "vm01-a"
PS C:\Users\Administrator> $vmView = $vm | Get-View
PS C:\Users\Administrator> # Creating a new PowerShell custom object to easily output properties
PS C:\Users\Administrator> $output = New-Object -TypeName PSCustomObject
PS C:\Users\Administrator> # Adding desired properties to the new PowerShell object
PS C:\Users\Administrator> $diskCommit = $vmView.Storage.PerDatastoreUsage.Committed
PS C:\Users\Administrator> $diskUncommit = $vmView.Storage.PerDatastoreUsage.Uncommitted
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceMB -Value ([math]::Round($diskCommit /1MB,0))
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceMB -Value ([math]::Round(($diskCommit + $diskUncommit) /1MB,0))
PS C:\Users\Administrator> $diskKey = $vmView.LayoutEx.Disk.Key
PS C:\Users\Administrator> $diskCapacity = $vmView.Config.Hardware.Device | Where-Object {$_.key -eq $diskKey} | Select-Object -ExpandProperty capacityInBytes
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityMB -Value ($diskCapacity /1MB)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vmView.Guest.Disk.DiskPath
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityMB -Value ($vmView.Guest.Disk.Capacity)
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceMB -Value ($vmView.Guest.Disk.FreeSpace)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityMB -Value ($vmView.Guest.Disk.Capacity | %{[math]::Round($_/1MB)})
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceMB -Value ($vmView.Guest.Disk.FreeSpace | %{[math]::Round($_/1MB)})
PS C:\Users\Administrator> # Viewing the output
PS C:\Users\Administrator> $output | FL

UsedSpaceMB        : 3340
ProvisionedSpaceMB : 8459
CapacityMB         : 5120
GuestPath          : {/, /boot}
GuestCapacityMB    : {3570, 1014}
GuestFreeSpaceMB   : {2179, 847}


PS C:\Users\Administrator>

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership

You can use the .NET Round method

-Value ([math]::Round($diskCommit /1GB,0))


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

0 Kudos
YanSi
Enthusiast
Enthusiast

Thank you for your answer

PS C:\Users\Administrator> # Establishing the connection to our vSphere object
PS C:\Users\Administrator> $vm = Get-VM -Name "vm01-a"
PS C:\Users\Administrator> $vmView = $vm | Get-View
PS C:\Users\Administrator> # Creating a new PowerShell custom object to easily output properties
PS C:\Users\Administrator> $output = New-Object -TypeName PSCustomObject
PS C:\Users\Administrator> # Adding desired properties to the new PowerShell object
PS C:\Users\Administrator> $diskCommit = $vmView.Storage.PerDatastoreUsage.Committed
PS C:\Users\Administrator> $diskUncommit = $vmView.Storage.PerDatastoreUsage.Uncommitted
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceGB -Value ([math]::Round($diskCommit /1MB,0))
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceGB -Value ([math]::Round(($diskCommit + $diskUncommit) /1MB,0))
PS C:\Users\Administrator> $diskKey = $vmView.LayoutEx.Disk.Key
PS C:\Users\Administrator> $diskCapacity = $vmView.Config.Hardware.Device | Where-Object {$_.key -eq $diskKey} | Select-Object -ExpandProperty capacityInBytes
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityGB -Value ($diskCapacity /1GB)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vmView.Guest.Disk.DiskPath
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityGB -Value ($vmView.Guest.Disk.Capacity)
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceGB -Value ($vmView.Guest.Disk.FreeSpace)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityGB -Value ([math]::Round($vmView.Guest.Disk.Capacity) /1MB,0))
ParserError:
Line |
   1 |  … apacityGB -Value ([math]::Round($vmView.Guest.Disk.Capacity) /1MB,0))
     |                                                                        ~
     | At line:1 char:138 + … apacityGB -Value ([math]::Round($vmView.Guest.Disk.Capacity) /1MB,0)) +                                                                       ~
Unexpected token ')' in expression or statement.

PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceGB -Value ([math]::Round($vmView.Guest.Disk.FreeSpace) /1MB,0))
ParserError:
Line |
   1 |  … eSpaceGB -Value ([math]::Round($vmView.Guest.Disk.FreeSpace) /1MB,0))
     |                                                                        ~
     | At line:1 char:140 + … eSpaceGB -Value ([math]::Round($vmView.Guest.Disk.FreeSpace) /1MB,0)) +                                                                       ~
Unexpected token ')' in expression or statement.

PS C:\Users\Administrator> # Viewing the output
PS C:\Users\Administrator> $output | FL

UsedSpaceGB        : 2810
ProvisionedSpaceGB : 5712
CapacityGB         : 5
GuestPath          : {/, /boot}


PS C:\Users\Administrator>

 But can't make "GuestCapacityGB" and "GuestFreeSpaceGB" Value Work.I think ($vmView.Guest.Disk.Capacity)and($vmView.Guest.Disk.FreeSpace) maybe can not use the .NET Round method.

0 Kudos
LucD
Leadership
Leadership

You are calling a .NET mehod, that is independent of the property.
Your parenthesis are placed incorrectly.

Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceGB -Value ([math]::Round($vmView.Guest.Disk.FreeSpace/1MB,0))

.
 


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

0 Kudos
YanSi
Enthusiast
Enthusiast
PS C:\Users\Administrator> # Establishing the connection to our vSphere object
PS C:\Users\Administrator> $vm = Get-VM -Name "vm01-a"
PS C:\Users\Administrator> $vmView = $vm | Get-View
PS C:\Users\Administrator> # Creating a new PowerShell custom object to easily output properties
PS C:\Users\Administrator> $output = New-Object -TypeName PSCustomObject
PS C:\Users\Administrator> # Adding desired properties to the new PowerShell object
PS C:\Users\Administrator> $diskCommit = $vmView.Storage.PerDatastoreUsage.Committed
PS C:\Users\Administrator> $diskUncommit = $vmView.Storage.PerDatastoreUsage.Uncommitted
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceMB -Value ([math]::Round($diskCommit /1MB,0))
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceMB -Value ([math]::Round(($diskCommit + $diskUncommit) /1MB,0))
PS C:\Users\Administrator> $diskKey = $vmView.LayoutEx.Disk.Key
PS C:\Users\Administrator> $diskCapacity = $vmView.Config.Hardware.Device | Where-Object {$_.key -eq $diskKey} | Select-Object -ExpandProperty capacityInBytes
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityMB -Value ($diskCapacity /1MB)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vmView.Guest.Disk.DiskPath
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityMB -Value ($vmView.Guest.Disk.Capacity)
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceMB -Value ($vmView.Guest.Disk.FreeSpace)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityMB -Value ([math]::Round($vmView.Guest.Disk.Capacity/1MB,0))
InvalidOperation: Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceMB -Value ([math]::Round($vmView.Guest.Disk.FreeSpace/1MB,0))
InvalidOperation: Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
PS C:\Users\Administrator> # Viewing the output
PS C:\Users\Administrator> $output | FL

UsedSpaceMB        : 3340
ProvisionedSpaceMB : 8459
CapacityMB         : 5120
GuestPath          : {/, /boot}


PS C:\Users\Administrator>

After modification, the prompt is as follows:

InvalidOperation: Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.

I think it has something to do with $vmView.Guest.Disk.Capacity

0 Kudos
LucD
Leadership
Leadership

The $vmView.Guest.Disk.FreeSpace property is an array.
You can't to a division to an array, you will have to do this separately for each element in the array

 


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

0 Kudos
YanSi
Enthusiast
Enthusiast

Thanks you very Much !

I saw this post solved my doubts

https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/get-vmguest-C-drive/td-p/527634

PS C:\Users\Administrator> # Establishing the connection to our vSphere object
PS C:\Users\Administrator> $vm = Get-VM -Name "vm01-a"
PS C:\Users\Administrator> $vmView = $vm | Get-View
PS C:\Users\Administrator> # Creating a new PowerShell custom object to easily output properties
PS C:\Users\Administrator> $output = New-Object -TypeName PSCustomObject
PS C:\Users\Administrator> # Adding desired properties to the new PowerShell object
PS C:\Users\Administrator> $diskCommit = $vmView.Storage.PerDatastoreUsage.Committed
PS C:\Users\Administrator> $diskUncommit = $vmView.Storage.PerDatastoreUsage.Uncommitted
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceMB -Value ([math]::Round($diskCommit /1MB,0))
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceMB -Value ([math]::Round(($diskCommit + $diskUncommit) /1MB,0))
PS C:\Users\Administrator> $diskKey = $vmView.LayoutEx.Disk.Key
PS C:\Users\Administrator> $diskCapacity = $vmView.Config.Hardware.Device | Where-Object {$_.key -eq $diskKey} | Select-Object -ExpandProperty capacityInBytes
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityMB -Value ($diskCapacity /1MB)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vmView.Guest.Disk.DiskPath
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityMB -Value ($vmView.Guest.Disk.Capacity)
PS C:\Users\Administrator> # Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceMB -Value ($vmView.Guest.Disk.FreeSpace)
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityMB -Value ($vmView.Guest.Disk.Capacity | %{[math]::Round($_/1MB)})
PS C:\Users\Administrator> Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceMB -Value ($vmView.Guest.Disk.FreeSpace | %{[math]::Round($_/1MB)})
PS C:\Users\Administrator> # Viewing the output
PS C:\Users\Administrator> $output | FL

UsedSpaceMB        : 3340
ProvisionedSpaceMB : 8459
CapacityMB         : 5120
GuestPath          : {/, /boot}
GuestCapacityMB    : {3570, 1014}
GuestFreeSpaceMB   : {2179, 847}


PS C:\Users\Administrator>

View solution in original post

0 Kudos