VMware Cloud Community
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

Get List of VMs, HDD Size, Datastores Size, Folder name, Operating System

Hello!!  I'm hoping someone can assist me with a PowerCLI script that can pull the subject info into a txt or csv file.

Thanks

Naveen Kumar

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There you go

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    Get-HardDisk -VM $vm |

    Select @{N='Host';E={$vm.VMhost.Name}},

        @{N='VMName';E={$vm.Name}},

        @{N='PowerState';E={$vm.PowerState}},

        @{N='OS';E={$vm.Guest.OSFullName}},

        @{N='OSConfigured';E={$vm.ExtensionData.Config.GuestFullName}},

        @{N='HDDName';E={$_.Name}},

        @{N='HDDDS';E={

            $script:ds = Get-Datastore -RelatedObject $_

            $script:ds.Name}},

        @{N='HDDDSFreeGB';E={[math]::Round($script:ds.FreeSpaceGB)}},

        @{N='HDDDSCapacityGB';E={[math]::Round($script:ds.CapacityGB)}},

        @{N='HDDCapacityGB';E={[math]::Round($_.CapacityGB)}},

        StorageFormat

} |

Export-Csv C:\Temp\vm-full-report-2.csv -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
26 Replies
LucD
Leadership
Leadership
Jump to solution

What do you already have?


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

&{foreach($vm in Get-VM){Get-Datastore -RelatedObject $vm | Select @{N='Host';E={$vm.VMhost.Name}}, @{N='VMName';E={$vm.Name}}, Name, @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}}, @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture

i have got above command from a earlier POST, which was replayed by you. Smiley Happy

i want to add HDD and running OS in above output file.

Thanks

Naveen Kumar

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    Get-Datastore -RelatedObject $vm |

    Select @{N='Host';E={$vm.VMhost.Name}},

        @{N='VMName';E={$vm.Name}},

        @{N='OS';E={$vm.Guest.OSFullName}},

        @{N='HDDProvisionedGB';E={[math]::Round($vm.ProvisionedSpaceGB)}},

        @{N='HDDUsedGB';E={[math]::Round($vm.UsedSpaceGB)}},

        Name,

        @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},

        @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}

} |

Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

PS C:\> Get-VM -PipelineVariable vm | ForEach-Object -Process {Get-Datastore -RelatedObject $vm | Select @{N='Host';E={$vm.VMhost.Name}}, @{N='VMName';E={$vm.Name}}, @{N='OS';E={$vm.Guest.OSFullName}}, @{N='HDDProvisionedGB';E={[math]::Round($vm.ProvisionedSpaceGB)}}, @{N='HDDUsedGB';E={[math]::Round($vm.UsedSpaceGB)}}, Name, @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}}, @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}} | Export-Csv C:\vm-full-report-21.csv -NoTypeInformation -UseCulture

Get-Datastore : 2/5/2020 10:18:06 PM    Get-Datastore           The underlying connection was closed: A connection that was

expected to be kept alive was closed by the server.

At line:1 char:56

+ ... m | ForEach-Object -Process {Get-Datastore -RelatedObject $vm | Selec ...

+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Datastore], ViError

    + FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrievePropertiesEx_ViError,VMware.VimAutomation.ViCore.Cmdle

   ts.Commands.GetDatastore

got some error.

Thanks

Naveen Kumar

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

PS C:\WINDOWS\system32> Get-VM -PipelineVariable vm | ForEach-Object -Process {Get-Datastore -RelatedObject $vm | Select @{N='Host';E={$vm.VMhost.Name}}, @{N='VMName';E={$vm.Name}}, @{N='OS';E={$vm.Guest.OSFullName}}, @{N='HDDProvisionedGB';E={[math]::Round($vm.ProvisionedSpaceGB)}}, @{N='HDDUsedGB';E={[math]::Round($vm.UsedSpaceGB)}}, Name, @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}}, @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture

Get-Datastore : 2/5/2020 10:29:32 PM    Get-Datastore           There was no endpoint listening at https://lbc-vcn1/sdk that could

accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for

more details.

At line:1 char:56

+ ... m | ForEach-Object -Process {Get-Datastore -RelatedObject $vm | Selec ...

+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Datastore], ViError

    + FullyQualifiedErrorId : Client20_DatastoreServiceImpl_UploadDatastoreItem_ViError,VMware.VimAutomation.ViCore.Cm

   dlets.Commands.GetDatastore

previous reply was might be due to related to network, i am not sure.

so i ran command again and got this error.

Thanks

Naveen Kumar

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerShell version?
Which PowerCLI version?

What does the PowerCLI configuration look like?

Are you connected to a vCenter or an ESXi node?

Which vSphere version?


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does your Connect-VIServer work without issue?
Does the VCSA show up in $global:DefaultViServers?


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

Thanks, command worked for me. Those errors looks like some network related issue was there.

Get-VM -PipelineVariable vm | ForEach-Object -Process {Get-Datastore -RelatedObject $vm | Select @{N='Host';E={$vm.VMhost.Name}}, @{N='VMName';E={$vm.Name}}, @{N='OS';E={$vm.Guest.OSFullName}}, @{N='HDDProvisionedGB';E={[math]::Round($vm.ProvisionedSpaceGB)}}, @{N='HDDUsedGB';E={[math]::Round($vm.UsedSpaceGB)}}, Name, @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}}, @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture

i need HDDCapacity instead of HDDProvisionedGB and HDDUsedGB, i am sorry it was not cleared by me earlier. i need output like below:

Output should be like this (added Datastore Free CapacityGB): 

pastedImage_0.png

HostVMNameHDD NameStorageFormatHDD CapacityGBRunning OSDatastore NameDatastore CapacityGBDatastore Free CapacityGB
abc-esx01.xyz.comtest01Harddisk1Thin101Microsoft Windows Server 2016 or later (64-bit)test-emc-lun0112060
abc-esx01.xyz.comtest01Harddisk2Thin501Microsoft Windows Server 2016 or later (64-bit)test-emc-lun02600400

Thanks for you help.

Thanks

Naveen Kumar

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is that sum of the capacity of all HDD connected to a VM?


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

HDD name and HDD capacity of a virtual machine.

pastedImage_0.png

Thanks

Naveen Kumar

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean like this?

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    Get-HardDisk -VM $vm |

    Select @{N='Host';E={$vm.VMhost.Name}},

        @{N='VMName';E={$vm.Name}},

        @{N='OS';E={$vm.Guest.OSFullName}},

        @{N='HDDName';E={$_.Name}},

        @{N='HDDDs';E={(Get-Datastore -RelatedObject $_).Name}},

        @{N='HDDCapacityGB';E={[math]::Round($_.CapacityGB)}}

} |

Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

pastedImage_0.png

thanks, yellow field are good (in above pic), please add 2 more things into this, "StorageFormat" and "Datastore Free CapacityGB"

Thanks

Naveen Kumar

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    Get-HardDisk -VM $vm |

    Select @{N='Host';E={$vm.VMhost.Name}},

        @{N='VMName';E={$vm.Name}},

        @{N='OS';E={$vm.Guest.OSFullName}},

        @{N='HDDName';E={$_.Name}},

        @{N='HDDDS';E={

            $script:ds = Get-Datastore -RelatedObject $vm

            $script:ds.Name}},

        @{N='HDDDSFreeGB';E={[math]::Round($script:ds.FreeSpaceGB)}},

        @{N='HDDCapacityGB';E={[math]::Round($_.CapacityGB)}},

        StorageFormat

} |

Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

pastedImage_0.png

by this command i got this:

yellow field good and rest 3 needed.

2 fields are blank in output file:

HDDDS, HDDDSFreeGB

pastedImage_1.png

Thanks

Naveen Kumar

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using?


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was a typo in the code I pasted.
I updated the last code, please run it again.


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

Reply
0 Kudos
naveenbaldwa1
Enthusiast
Enthusiast
Jump to solution

i am sorry, still the same as i posted previously.

Thanks

Naveen Kumar

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works for me.

Are you really using this version?

Get-VM -PipelineVariable vm |

ForEach-Object -Process {

    Get-HardDisk -VM $vm |

    Select @{N='Host';E={$vm.VMhost.Name}},

        @{N='VMName';E={$vm.Name}},

        @{N='OS';E={$vm.Guest.OSFullName}},

        @{N='HDDName';E={$_.Name}},

        @{N='HDDDS';E={

            $script:ds = Get-Datastore -RelatedObject $vm

            $script:ds.Name}},

        @{N='HDDDSFreeGB';E={[math]::Round($script:ds.FreeSpaceGB)}},

        @{N='HDDCapacityGB';E={[math]::Round($_.CapacityGB)}},

        StorageFormat

} |

Export-Csv C:\Temp\vm-full-report-2.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos