karthikeyanj
Enthusiast
Enthusiast

script to get vm disk usage from csv and export to csv

Jump to solution

Hi All

I trying to create  script to get the vm disk usage - used space - free space and total disk space .

I have used this script which helps me getting the required result .

but I need to modify the script  for each cluster it should get the vms in that cluster and check the vm disuage . and export to csv file

$vms = Import-CSV "C:\Cluster.csv

$outputfile = "c:\Diskusage.csv"

Connect-viserver VC -user vcadmin -password

foreach ($vm in $vms){

Get-VM $vm.name | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VMName -ExpandProperty Disks | Select VMName, Path, @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},@{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},@{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}} | Export-Csv -NoTypeInformation $outputfile }

Disconnect-viserver

Attached the output file

Kinldy help me in creating the script

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Can you try like this ?

Connect-viserver VC -user vcadmin -password
foreach($cluster in Get-CLuster){
    $outputfile = "c:\" + $cluster.Name + "-Diskusage.csv"
    Get-VM -Location $cluster |     Where { $_.PowerState -eq "PoweredOn" } |     Get-VMGuest |     Select VMName -ExpandProperty Disks |     Select VMName, Path,         @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},            @{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},         @{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}} |     Export-Csv -NoTypeInformation $outputfile
} Disconnect-viserver

The script will create a seperate CSV file for each cluster.


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership

Can you try like this ?

Connect-viserver VC -user vcadmin -password
foreach($cluster in Get-CLuster){
    $outputfile = "c:\" + $cluster.Name + "-Diskusage.csv"
    Get-VM -Location $cluster |     Where { $_.PowerState -eq "PoweredOn" } |     Get-VMGuest |     Select VMName -ExpandProperty Disks |     Select VMName, Path,         @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},            @{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},         @{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}} |     Export-Csv -NoTypeInformation $outputfile
} Disconnect-viserver

The script will create a seperate CSV file for each cluster.


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

0 Kudos
karthikeyanj
Enthusiast
Enthusiast

Hi lucd

This Script works fine .

I need to input the Cluster parameters using import-csv  from a csv file .

For Eg: sample.csv

ClusterName

Cluster1

Cluster2   

Can u also help me with this

Thanks for the valuable inputs .

0 Kudos
LucD
Leadership
Leadership

No, problem, try this version

Connect-viserver VC -user vcadmin -password 

foreach
($cluster in (Get-Cluster -Name (Import-Csv "C:\sample.csv" | %{$_.ClusterName}))){     $outputfile = "c:\" + $cluster.Name + "-Diskusage.csv"
    Get-VM -Location $cluster |     Where { $_.PowerState -eq "PoweredOn" } |     Get-VMGuest |     Select VMName -ExpandProperty Disks |     Select VMName, Path,         @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},            @{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},         @{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}} |     Export-Csv -NoTypeInformation $outputfile
} Disconnect-viserver


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

0 Kudos
RParker
Immortal
Immortal

Simplest method is use a FREE tool:

http://robware.net/

Works with vSphere 5 also.. you can export everything, hosts, VM's, disks, networks, even snapshot usage.. It's very nice, I would check it out.

0 Kudos
karthikeyanj
Enthusiast
Enthusiast

Hi All

Thanks to LUCD

I was able to import from csv and im getting the desired output .

I modified the script as LUCD Advice .

$clusters = Import-csv "C:\Scripts\Cluster.csv


Connect-viserver vcserver -user vcadmin -password


foreach ($cluster in $clusters){
    $outputfile = "c:\scripts\"+ $cluster.Name + "-Diskusage.csv"
   
   Get-cluster $cluster.name  |
   Get-VM  |
    Where { $_.PowerState -eq "PoweredOn" } |
    Get-VMGuest | where { $_.State -eq "Running" } |
    Select VMName -ExpandProperty Disks |
    Select VMName, Path,
        @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},   
        @{Name="DiskUsedGB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1GB),2)}},
        @{Name="DiskFreeGB";Expression={[math]::Round((($_.FreeSpace)/1GB),2)}} |
    Export-Csv -NoTypeInformation $outputfile
}


Disconnect-viserver vcserver -confirm:$false

0 Kudos
Varunraju
Contributor
Contributor

Hi ,

Even Am also looking to find the disk space in the below mentioned format.

VmNamePathDiskCapacityGBDiskUsedMBDiskFreeMB
VCENTER5C:\19.4315940.63958.39
VCENTER5E:\20.47999.2119958.79
COEVSDVCC:\39.936159.694698.3
COEVSDVCF:\40876.6740080.32
COEVSDDCC:\39.912865.6827992.32

Could you help me with any script , also let me know where should i run the script.

Am sorry, Am new to VMware.

0 Kudos
LucD
Leadership
Leadership

I think the script just above you question does exactly that.

And for running PowerCLI scripts have a look at Execute PowerCLi scripts out of PowerCli Console?


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

0 Kudos
AviShimon
Contributor
Contributor

Hi LucD,

I was wandering what is the difference between $VM.HardDisks.CapacityGB  and $VM.ProvisionedSpaceGB.

my VM is provisioned (Thin) with One Disk size 32Gb (No SnapShots) which is exactly tha value shown in $VM.HardDisks.CapacityGB.

the value of $VM.ProvisionedSpaceGB shows 42 Gb.  (shown only in VI client).

datastore browser shows this,

there are No other significant files (large) in the VM folder. VM is powered off.

can you explain the difference between the 2 properties ?

Thanks, Avi

0 Kudos
LucD
Leadership
Leadership

There could be a number of reasons for this.

Have a look with the Datastore Browser from the vSphere client in the VM's folder, are there perhaps any hidden snapshot files in there ?


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

0 Kudos