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
There you go
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
What do you already have?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
&{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.
i want to add HDD and running OS in above output file.
Thanks
Naveen Kumar
Try something like this
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
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
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
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
Does your Connect-VIServer work without issue?
Does the VCSA show up in $global:DefaultViServers?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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):
Host | VMName | HDD Name | StorageFormat | HDD CapacityGB | Running OS | Datastore Name | Datastore CapacityGB | Datastore Free CapacityGB |
abc-esx01.xyz.com | test01 | Harddisk1 | Thin | 101 | Microsoft Windows Server 2016 or later (64-bit) | test-emc-lun01 | 120 | 60 |
abc-esx01.xyz.com | test01 | Harddisk2 | Thin | 501 | Microsoft Windows Server 2016 or later (64-bit) | test-emc-lun02 | 600 | 400 |
Thanks for you help.
Thanks
Naveen Kumar
Is that sum of the capacity of all HDD connected to a VM?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
HDD name and HDD capacity of a virtual machine.
Thanks
Naveen Kumar
You mean like this?
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
thanks, yellow field are good (in above pic), please add 2 more things into this, "StorageFormat" and "Datastore Free CapacityGB"
Thanks
Naveen Kumar
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
by this command i got this:
yellow field good and rest 3 needed.
2 fields are blank in output file:
HDDDS, HDDDSFreeGB
Thanks
Naveen Kumar
Which PowerCLI version are you using?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
i am sorry, still the same as i posted previously.
Thanks
Naveen Kumar
Works for me.
Are you really using this version?
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