VMware Cloud Community
2448Charles
Enthusiast
Enthusiast
Jump to solution

Get List of VMs, Datastores and Folder name per Cluster


Hello!!  I'm hoping someone can assist me with a PowerCLI script that can pull the subject info into a txt or csv file.  In an effort to get our environment cleaned up before our next DR exercise with SRM, I want to get a list that shows each VM, the datastore(s) it resides on, and the Folder name within VMs and Templates view.  We keep our VMs organized by creating folder names that correspond to the app running on the VM.  I mention SRM because last year our Protected Groups matched the Rcopy groups from our 3PAR array.  However, we've added over 200 new VMs since that time and my team members basically placed these new VMs on the first available datastore that had enough free space.  Unfortunately, we're not utilizing Storage Profiles...it's scheduled for next year.

The extracted info would look similiar to the following:

Servername    Datastore   Folder Name

Server1            LUN0           ActiveDirectory

Server2            LUN0           ActiveDirectory

Server3            LUN1           ActiveDirectory

Server4            LUN3           Argo

Server5            LUN7           Argo

Server6            LUN6           Lockbox

Server7            LUN5           Lockbox

Server8            LUN9           Citrix

etc...

Any help with extracting this info from vCenter would be greatly appreciated.

Thanks,

Charles

98 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM -Datastore "*NACL*" |

Select Name,

   @{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}},

   @{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},

   @{N= 'Cluster';E={(Get-Cluster -VM $_).Name}},

   @{N = "Folder"; E = {$_.Folder.Name}} |

Sort-Object -Property Folder


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

0 Kudos
phatq
Contributor
Contributor
Jump to solution

That worked partially! I encountered the error that was posted before again and had to wait an awfull long time before the query ended and not all of the datastores with the phrase *NACL* was shown in the result. So yes, I think our ancient PowerCLI version needs to be updated before we continue 🙂

Thanks again Luc!

0 Kudos
TX_Tundra
Contributor
Contributor
Jump to solution

This works great!  Thank you for all you provide us!

&{foreach($vm in Get-VM){

Get-Datastore -RelatedObject $vm |

Select @{N='Cluster';E={Get-Cluster -VMHost $vm.VMhost | select -ExpandProperty Name}},

Name,

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

@{N='NAA';E={$_.ExtensionData.Info.Vmfs.Extent[0].DiskName}},

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

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

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

 

Using the above script, how can we pull only the VMs that have VMDKs split between two LUNs?  Our SDRS default is to keep VMDKs together but we are finding some VMs are split.  We're putting in some monitoring to determine how this is happening but it would be nice to be able to pull out those with split storage only, especially in an environment with nearly 3,500 VMs on 130+ hosts.

Thank you Sir!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Would something like this work for you?

Get-VM | ForEach-Object -Process {
  $ds = Get-Datastore -RelatedObject $_
  if($ds.Count -gt 1){
    New-Object -TypeName PSObject -Property @{
      VM = $_.Name
      Datastore = $ds.Name -join '|'
    }
  }
}


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

0 Kudos
TX_Tundra
Contributor
Contributor
Jump to solution

That absolutely works!  You are a lifesaver Sir!  

Thank you again.  

0 Kudos
Sathish_K
Contributor
Contributor
Jump to solution


@LucD wrote:

You can pipe the result to a CSV file for example

 

Get-VM |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv C:\report.csv -NoTypeInformation -UseCulture


if my vm disk has three or more datastores and in the join command i am unable to add thrid datastore id in the column value. how to run on multiple computers 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, I don't understand what you mean.

Perhaps an example or a screenshot could clarify.


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

0 Kudos
Sathish_K
Contributor
Contributor
Jump to solution

Sathish_K_0-1698962858453.png

if u see the yellow highlight the third datastore name value (second column) is missing with dots...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is because the PowerShell output engine can't fit the value on the screen.
Pipe the result to Format-List, or export the results to a CSV with Export-Csv.


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

0 Kudos
Sathish_K
Contributor
Contributor
Jump to solution

i tried with passing export excel output but since it is loop varibale it isnot updating for all servers and only one server it is updating in excel sheet. pls advise

foreach($vmlist in (Get-Content -Path C:\Users\SathishKarthikeyan\sanca-vm-on.txt)){

#$vm = Get-VM -Name $vmlist

#echo $vm

 

Get-VM -Name $vmlist |

 

Select Name,

@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},

@{N="Folder";E={$_.Folder.Name}} |

#sleep 30 

 

Export-Csv C:\Users\SathishKarthikeyan\sanca-vm-datastore.csv  -NoTypeInformation -UseCulture

 

}

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

A foreach statement doesn't place anything in the pipeline.
Try like this, no need for a foreach

$vmlist = Get-Content -Path C:\Users\SathishKarthikeyan\sanca-vm-on.txt

Get-VM -Name $vmlist |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv C:\Users\SathishKarthikeyan\sanca-vm-datastore.csv  -NoTypeInformation -UseCulture

 


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

0 Kudos
Sathish_K
Contributor
Contributor
Jump to solution

i tried without loop as well before but Get-VM doesnt work that way for some reason and i dont know why. As said above i ran the above script and the script didnt even execute at all throwing error as below 


PS C:\Users\SathishKarthikeyan> $vmlist = Get-Content -Path C:\Users\SathishKarthikeyan\sanca-vm-on.txt

Get-VM -Name $vmlist |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv C:\Users\SathishKarthikeyan\sanca-vm-datastore.csv -NoTypeInformation -UseCulture
Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty,
and then try the command again.
At line:3 char:14
+ Get-VM -Name $vmlist |
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What do you have in the TXT file?
On each line a name of a VM?

Does the following returns the lines from the TXT file?

Get-Content -Path C:\Users\SathishKarthikeyan\sanca-vm-on.txt

 


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

0 Kudos
Sathish_K
Contributor
Contributor
Jump to solution

i have just a list of server names in the text file thats all sir. it is a bug with Get-VM command as per Microsoft for some reason it is not working or parsing the values to next line of commands 

 

PS C:\Users\SathishKarthikeyan> Get-Content -Path C:\Users\SathishKarthikeyan\sanca-vm-on.txt
tc3bdb-rep
tc7adb
tc6ddb-rep
fielda1db-rep
tc7bdb
tc9bdb-rep
tcfbdb
tc2ddb-rep
tc6bdb
tc8bdb
tc6adb
tc5bdb
tceadb
fddb-rep
tcebdb-rep
tc1bdb

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

"...it is a bug with Get-VM command as per Microsoft"

Are you by any chance using the Get-VM cmdlet from the Hyper-V module and not the PowerCLI module?
What does this show as the Source?

Get-Command -Name Get-VM


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

0 Kudos
Sathish_K
Contributor
Contributor
Jump to solution

Probably ur right it shows vmware mule and not hyper-v

 

 

Get-Command -Name Get-VM

CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-VM 13.0.0.... VMware.VimAutomation.Core

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Very strange.
So the following doesn't work for you?

$vmlist = Get-Content -Path C:\Users\SathishKarthikeyan\sanca-vm-on.txt
Get-VM -Name $vmlist

And which MSFT source are you referring to with regards to the Get-VM cmdlet?


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

0 Kudos
Sathish_K
Contributor
Contributor
Jump to solution

Sathish_K_0-1699039190262.png

yes the below format works for me but i cannot parse it to excel sheet bcz the loop is not working for excel parse output but it works for display at PS IDE for all servers

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm utterly confused now.

This works, but before you showed me an error stating that the Name parameter received a Null value.

And like I explained before, the foreach statement doesn't place anything in the pipeline, hence nothing will be written by Export-Csv.
The previous snippet I posted bypasses the foreach statement by placing everything in a pipeline construct.

I give up.


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

0 Kudos