PowerCLI

 View Only
Expand all | Collapse all

Get List of VMs, Datastores and Folder name per Cluster

bernz

bernzJun 01, 2016 06:03 AM

Virtualduty

VirtualdutyFeb 15, 2018 07:16 AM

  • 1.  Get List of VMs, Datastores and Folder name per Cluster

    Posted Jul 30, 2013 09:00 PM


    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



  • 2.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jul 30, 2013 09:34 PM

    Try something like this

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


  • 3.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jul 31, 2013 01:12 PM

    Thanks LucD!!!  That appears to provide me with what I'm looking for.  However, the above command is outputting the info screen.....what can be added to pipe it to a TXT file?

    Thanks,

    Charles



  • 4.  RE: Get List of VMs, Datastores and Folder name per Cluster
    Best Answer

    Posted Jul 31, 2013 01:18 PM

    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


  • 5.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jul 31, 2013 02:19 PM

    LucD, I've marked this as "correct answer".  Is there anything else I need to do to get it closed?

    Thanks,

    Charles



  • 6.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jul 31, 2013 02:23 PM

    No, when you mark a reply as Correct, the thread is marked Answered automatically.



  • 7.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jul 31, 2013 06:13 PM

    LucD, I have 2 additonal columns of info I forgot to mention.  I need to know how much stoarge has been provisioned and how much storage is being used.

    Example:

    Servername    Datastore     Provisioned     Used        Folder Name

    Server1            LUN0            80GB                 45GB       ActiveDirectory

    Server2            LUN0            1.2TB                730GB     ActiveDirectory

    Thanks,

    Charles



  • 8.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jul 31, 2013 06:23 PM

    No problem, try this

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


  • 9.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Aug 04, 2015 12:35 AM

    Hi how's it going?

    I haven't done any powershell for years and I'm trying to remember how to do this stuff...

    The example script is pretty good bit it's missing something for me.

    I'm looking for the following in a list form if possible;

    HOST     VM     DATASTORE NAME     DATASTORE CAPACITY      DATASTORE FREE SPACE

    I'd really appreciate some help :smileyhappy:

    Thanks in advance.



  • 10.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Aug 04, 2015 06:55 AM

    You mean like this ?

    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)}}

    }



  • 11.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Aug 04, 2015 11:20 PM

    Champion thank you.



  • 12.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Oct 23, 2015 07:05 PM

    How do you pipe that to a .csv like your earlier commands?  I keep getting this error:

    PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> 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

    At line:1 char:235

    + ... eSpaceGB,1)}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCultu ...

    +                    ~

    An empty pipe element is not allowed.

        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

        + FullyQualifiedErrorId : EmptyPipeElement

    PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

    And is there a way to also list the RDMs for each VM if any?



  • 13.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Oct 23, 2015 08:33 PM

    Try like this, a ForEach doesn't place anything in the pipeline.

    Use the call operator (&) to force that.

    &{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

    You can get the RDM info through the Get-Harddisk cmdlet, but that would require a somewhat different organisation of the script.

    Perhaps you better make a new thread for that.



  • 14.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 02, 2016 05:24 AM

    Hello,

    I am very new to powercli and I think that it is a great tool to manage vmware infrastructure.

    however, I do not know where to start.

    Could someone tell me what the following code does

    Get-ResourcePool | Where {$_.Name -ne Resources }| Select Name, @{N=NumVM;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\res-numvm.csv

    Globally I know that it list ressource pools etc, but what I want to know is

    1. Why do we use "$_.xxx"

    2. Why and where do we use the "@"

    3. What the "N" means

    4. What the "E" means

    5. Why the codes "$_ | Get-VM" is like it is and not like "$_Get-VM"

    6. What the use of "|"

    Nicolas



  • 15.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jun 02, 2016 09:48 PM

    To nmedard

    Could someone tell me what the following code does

    Get-ResourcePool | Where {$_.Name -ne Resources }| Select Name, @{N=NumVM;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\res-numvm.csv




    1. $_ represents the objects in the pipeline, so:

    Get-ResourcePool | Where {$_.Name -ne Resources }


    if you had 3 resource pools, A, B, C when you do a 'get-resourcepool | where{$_.name}


    It will call 'get-resourcepool' and go through them individually, each one represented by the $_ object. When you are looking at $_.xxx those are properties of that object, so $_.name calls the name of the object.


    get-resourcepool | foreach{$_.name} -this example will go through each resourcepool and just display their name.


    2. @ is used for different reasons, such as creating a hash @{} or an @() etc


    3. N means name, this is creating an expression that has a column header represented by N (NumVM) and then the value that NumVM is represented by the E, or the expression, ($_ | get-vm) -this is running the get-VM command on the resource pool (represented by $_) and then doing a .count on it to count the number of vm objects inside of the $_ object



    The pipe, "|", passes the information of a command that can further be manipulated. Such as the example below:


    get-resourcepool - this will spit out a lot of information, but if we want to manipulate it further we can pipe it...


    get-resourcepool | where {$_.name -eq "A"} - This command will only return information for a resource pool that has the name of "A".



  • 16.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 05, 2016 03:57 PM

    Hi Luc,

    Good to see that script you have provided. its worked for entire VM in a cluster/ VCenter. 

    I'm looking for the script that can provide the report, only list of the VMs.

    Eg :

    VM1

    VM3

    VM10

    VM14 ....

    Could you please help me out on this ?



  • 17.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 05, 2016 04:56 PM

    On the Select-Object, only keep the VMName property.



  • 18.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 05, 2016 05:39 PM

    Could you please gimme the correct syntax ?



  • 19.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 10, 2017 08:21 AM

    Hi ,

    just run this command and you will get only vm names 

    Get-VM | Select Name


    to export to csv use this   - Get-VM | Select Name | Export-Csv C:\data .csv



  • 20.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Apr 24, 2017 09:03 PM

    You can store list of VMs in a text file. and call it using

    $VMs = Get-Content "Text file complete path"

    foreach ($VM in $VMs)

    {

    Code goes in here.

    }

    Hope that helps.



  • 21.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 24, 2016 09:45 AM

    Hi LucD

    How about if I want it to be granular?

    Need to extract this by Cluster.



  • 22.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 24, 2016 10:18 AM

    Can you give a sample layout of the desired output ?



  • 23.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 25, 2016 02:31 AM

    Hi LucD

    Here is the sample output needed:

    Cluster NameDatastore NameNAAVMCapacityFree


  • 24.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 25, 2016 05:25 AM

    Try like this

    &{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:\vm-full-report-2.csv -NoTypeInformation -UseCulture



  • 25.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 30, 2016 06:48 AM

    LucD

    Get-VM |

    Select Name,

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

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

    Can we pull resource pool the vm is part of in this as well ?

    thanks



  • 26.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 30, 2016 06:59 AM

    Try like this

    Get-VM |

    Select Name,

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

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

    @{N='ResourcePool';E={Get-ResourcePool -VM $_ | Select -ExpandProperty Name}}



  • 27.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jun 01, 2016 06:03 AM

    Hi LucD

    Getting this error >>>



  • 28.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jun 02, 2016 08:08 PM

    Can you attach the code you are using ?



  • 29.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jun 03, 2016 02:01 PM

    Hi LucD

    It's now working!

    #####

    &{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:\vm-full-report-2.csv -NoTypeInformation-UseCulture


    #####

    How about if I will run the script on a per cluster basis? Like selecting which cluster.

    Also, can we add "VM folder path"?



  • 30.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jun 04, 2016 06:14 AM

    You can add an outer ForEach loop, that runs through all your clusters.

    And then use the Location parameter on the Get-VM cmdlet.

    For the folder path, which path are you referring to ?

    The path on the datastore where the VM's VMX is located, the "yellow" folderpath (under Clusters & Hosts) or the "blue" folderpath (under VMs & Templates) ?

    Perhaps it would be handier if you created a new thread for these new questions ?



  • 31.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 15, 2018 04:19 AM

    Hi,

    This script was very helpful , thank you !

    Is there any parameter that can get the storage vendor details. I am looking for list of VMs, corresponding datastores for all the virtual disks, naa id and storage vendor name. We are using Hitachi, EMC and NetApp.



  • 32.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 15, 2018 05:39 AM

    One way of doing that would be through this calculated property.

      @{N='Vendor';E={(

        Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

        Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},



  • 33.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 15, 2018 07:16 AM

    Works like a charm ! Thank you !



  • 34.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 20, 2018 05:28 AM

    I am also looking for provisioned space on all the VMs in these clusters. I ran the below script but the provisionspacegb column in the output file was all blank.

    &{foreach($vm in Get-VM){&{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="ProvisionedSpaceGB" ;E={(Get-VM -ProvisionedSpaceGB)}},

      @{N='Vendor';E={(

        Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

        Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},

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

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

    Could you help me understand what's wrong in my script ?



  • 35.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 20, 2018 05:36 AM

    There are a few too many curly braces at the end

    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="ProvisionedSpaceGB" ;E={(Get-VM -ProvisionedSpaceGB)}},

      @{N='Vendor';E={(

        Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

        Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},

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

    Export-Csv  C:\Users\CA-VISM-ADM-P003\vm-full-report-2.csv -NoTypeInformation -UseCulture



  • 36.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 20, 2018 06:39 AM

    I ran below

    &{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="ProvisionedSpaceGB" ;E={(Get-VM -ProvisionedSpaceGB)}},

      @{N='Vendor';E={(

        Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

        Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},

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

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

    I get below error

    Missing closing '}' in statement block or type definition.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEndCurlyBrace



  • 37.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 20, 2018 07:01 AM

    You changed the script, now you need an extra { at the end of the Select (to close the &{)



  • 38.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 08, 2019 02:38 PM

    Thank you Luc! As usual: your work is amazing!

    I know it's been a long time since your last answer but if I need to also get the OS version?

    I can get it this way, but I can't put it in the precedant script:

    Get-VM | Get-View -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") | Select -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}},  @{N="Running OS";E={$_.Guest.GuestFullName}} | Format-Table -AutoSize



  • 39.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 08, 2019 03:03 PM

    You mean like this?

    Get-VM |

      Select Name,

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

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

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



  • 40.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 08, 2019 03:34 PM

    Thank you.

    But I also need the Provisioned Space and the used space.

    Could you help me out?



  • 41.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 08, 2019 03:49 PM

    Which of the scripts in this thread are you using?



  • 42.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 08, 2019 03:52 PM

    this one:

    Get-VM |

    Select Name,

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

    @{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,1)}},

    @{N="ProvisionedSpaceGB";E={[math]::Round($_.ProvisionedSpaceGB,1)}},

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



  • 43.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 08, 2019 04:00 PM

    Ok, just add that new calculated property.

    Get-VM |

    Select Name,

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

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

    @{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},

    @{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},

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



  • 44.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 08, 2019 04:25 PM

    Thank you!

    It works perfectly! I've made a mistake earlier ;-) That's why it didn't work right away



  • 45.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 09, 2019 10:29 AM

    I would also like to sort the VMs per folder, as we are manipulating objects, how could I insert "Sort-Object -Descending" for the folders?



  • 46.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 09, 2019 10:34 AM

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



  • 47.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Jan 09, 2019 10:43 AM



  • 48.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 13, 2019 03:53 PM

    Hi Luc

    I'd like to improve the script: orderding the output by folder name.

    I tried to add this " | Sort-Object -descending " at the end of " @{N="Folder";E={$_.Folder.Name}} "

    But I didn't worked out

    Any idea?



  • 49.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 13, 2019 04:00 PM

    Try like this

    Get-VM |

      Select Name,

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

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

    @{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},

    @{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},

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

      Sort-Object -Property Folder



  • 50.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 13, 2019 04:59 PM

    It works! I was almost there!

    Thank you Luc



  • 51.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Mar 14, 2019 09:11 AM

    Hello LucD

    I know this work: Get-VM vmname | Get-NetworkAdapter | Select -ExpandProperty MacAddress

    But, do you know how to put it to the script to also get the MacAddress of each vm?



  • 52.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Mar 14, 2019 09:16 AM

    Create an additional calculated property for that.

    I added the -join in case you have VMs with more than 1 vNIC.

    Get-VM |

    Select Name,

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

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

       @{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},

       @{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},

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

       @{N = 'MAC';E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}} |

    Sort-Object -Property Folder



  • 53.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Mar 14, 2019 11:14 AM

    Thank you!

    It works fine

    I was able to notice that someone cloned a vm and I had 2 identicals mac addresses - -' and to see several mac addresses for a single vm...

    In fact I had alarm on duplicated MacAddresses but this KB VMware Knowledge Base couldn't help me because my vCenters do not have the same ID..



  • 54.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Apr 06, 2022 03:41 PM

    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!



  • 55.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Apr 06, 2022 04:08 PM

    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 '|'
        }
      }
    }
    


  • 56.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Apr 06, 2022 04:15 PM

    That absolutely works!  You are a lifesaver Sir!  

    Thank you again.  



  • 57.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 17, 2017 03:04 PM

    Hi,

    Thanks for the script. Its working fine to me.

    Can you please help me in downloading the output in csv format. Also, I ma very new to the scripting world. Please give me the fake path for the storing the data.

    Regards,

    Yuvaraj



  • 58.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 06, 2017 10:45 PM

    @LucD, Can you please help me how to add "data store folder --> data store  in the script?

    &{foreach($Cluster in Get-Cluster){

    Get-Datastore -RelatedObject $Cluster |

    Select @{N='Cluster';E={$Cluster.Name}},

      @{N='ESXi Host';E={Get-VMHost -Datastore $_}},

      @{N='Datastore Cluster';E={Get-Datastorecluster -Datastore $_}},

      @{N='Datastore Folder';E={Get-Folder -Type Datastore $_.Datastore.Name}},

    Name,

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

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

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

    Export-Csv "C:\Datastore_info1.csv" -NoTypeInformation -UseCulture



  • 59.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 07, 2017 05:52 AM

    Try like this

    Get-Cluster -PipelineVariable cluster |

    Get-Datastore |

    Select @{N='Cluster';E={$Cluster.Name}},

      @{N='ESXi Host';E={Get-VMHost -Datastore $_ | Select -ExpandProperty Name}},

      @{N='Datastore Cluster';E={Get-Datastorecluster -Datastore $_ | select -ExpandProperty Name}},

      @{N='Datastore Folder';E={

        $parent = Get-View -Id $_.ExtensionData.Parent

        while($parent -isnot [VMware.Vim.Folder] -or $parent.MoRef.Type -eq 'StoragePod'){

            $parent = Get-View -Id $parent.Parent

        }

        $parent.Name

      }},

      Name,

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

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

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

    Export-Csv "C:\Datastore_info1.csv" -NoTypeInformation -UseCulture



  • 60.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 07, 2017 02:42 PM

    It worked perfectly. Thanks LucD.



  • 61.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 02, 2023 08:13 PM

     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 



  • 62.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 02, 2023 08:19 PM

    Sorry, I don't understand what you mean.

    Perhaps an example or a screenshot could clarify.



  • 63.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 02, 2023 10:08 PM

    Sathish_K_0-1698962858453.png

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



  • 64.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 02, 2023 10:19 PM

    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.



  • 65.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 02, 2023 10:22 PM

    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

     

    }

     



  • 66.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 02, 2023 10:52 PM

    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

     



  • 67.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 01:35 PM

    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

     



  • 68.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 02:01 PM

    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

     



  • 69.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 02:38 PM

    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

     



  • 70.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 02:51 PM

    "...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


  • 71.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 04:22 PM

    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



  • 72.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 05:06 PM

    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?



  • 73.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 07:21 PM

    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



  • 74.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Nov 03, 2023 07:23 PM

    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.



  • 75.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 08, 2017 08:45 AM

    Hi,

    How can i get the same using pyvmomi in python.



  • 76.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 08, 2017 08:52 AM


  • 77.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 13, 2017 10:29 AM

    HI,

    How to get the following?

    VM  Datastore   LUN  Vserver(SVM)   Volume  Aggregate

    I could get till the Datastore (using PowerCLI). But the other parameters Iam not sure how to go about.

    Get-View -ViewType virtualmachine |  ForEach-Object {

    # Capture the name of the virtual machine as a variable

    $vmname=$_.Name

    # Capture the details of the VMDK file(s) for the virtual machine, including path(s)

    $vmdkname=$_.layoutex.file.name

    # Capture the name of the datastore for the virtual machine, as reported by the vm

    $dsname=$_.config.DatastoreUrl.name

    # For each virtual machine, capture datastore details, using the

    # name of the datastore AS REPORTED BY THE VIRTUAL MACHINE for the filter,

    # capture the NAA ID of the LUN for the datastore,

    # then export select properties to the CSV file named previously

    Get-View -ViewType datastore -Filter @{"Name"= "^$dsname$"} |Select-Object @{n="VM Name";e={$vmname}}, @{n="VMDK Name";e={$vmdkname}}, @{n="VM Datastore Name";e={$dsname}}, name,@{n='NAA';e={$_.info.vmfs.extent.diskname}}

    }

    how to proceed to get corresponding lun id , vserver, volume, aggregate and node



  • 78.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Oct 26, 2017 11:11 AM

    Is there a way to Get a exported List in vsphere client of all virtual machines attached to LUNS Storage. I need to see what Virtual machines are connected to what LUN?



  • 79.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 02, 2018 06:28 AM

    Hello,

    As mentioned below, the result returns list of VMS and exact Folder name per Cluster. In my scenerio I have to produce list of DatastoreClusters and their exact folder names.

    I tried the same script,  but instead of "Get-VM" have put Get-DataStoreCluster.. But didnt worked.

    List of VMS and Folder name.

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

    List of DatastoreClusters and Folder Names. (It is not working)

    Get-DataStoreCluster |    
    Select Name,
    @{N="Folder";E={$_.Folder.Name}}

    Could you please give some suggestions on it?



  • 80.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 02, 2018 07:01 AM

    Which Foldername is that, the Folder in which the Datastorecluster is located ( a Storage folder)?



  • 81.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 02, 2018 07:21 AM

    yes.



  • 82.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 02, 2018 07:24 AM

    The structure in Vcenter:

    DataCenter

         Folder1   

                   Cluster1    

                        Datastore1

                        Datastore2

                   Cluster2

                        Datastore3

                        Datastore4

         Folder2

              Cluster2

                        Datastore5

                        Datastore6

    Here I want to get Folder1 and Folder2 names.



  • 83.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 02, 2018 07:29 AM

    Try like this

    Get-DatastoreCluster |

    Select Name,

        @{N='Folder';E={

            $parent = Get-View -Id $_.ExtensionData.Parent

            while($parent -isnot [VMware.Vim.Folder]){

                $parent = Get-View -Id $parent.Parent

            }

            $parent.Name       

        }}



  • 84.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Feb 02, 2018 08:24 AM

    Yes Great, Its working.

    Thank you so much :smileyhappy:



  • 85.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 02, 2019 04:28 PM

    Can you elaborate on the [string]::Join syntax? I get that you are calling a .net string.join method but what are you joining here and why?



  • 86.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 03, 2019 06:41 AM

    The reason is because we want to have a single value in this calculated property.
    So if the VM is located on several datastores, we join the names of the datastores (the 2nd operand to the Join method) with the 1 st operand character.



  • 87.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted May 03, 2019 04:59 PM

    oh gotcha, so if more than one item is returned it joins them into a comma delineated list. Thanks man!



  • 88.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 12:03 PM

    Is it also possible to list the VM's on certain datastores. I want to look for those VM's who resides on datastores with for instance "NACL" in de datastore name, and to also show me the corresponding VM folder where the VM's resides in. Thank you in advance!



  • 89.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 12:31 PM

    Use the Datastore parameter and OBN.

    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 = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},

       @{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},

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

       @{N = 'MAC';E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}} |

    Sort-Object -Property Folder



  • 90.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 12:52 PM

    I get this after using the script:



  • 91.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 12:57 PM

    Do you get the same error when you just do Get-VM?



  • 92.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 01:07 PM

    No I don't, I then get a list of all the VM's. But most of the mentioned criteria in the query I don't need, like UsedSpace/MAC/ProvisionedSPace. I only need the VM name, Datastore name and the VM folder it resides in. If we were te exclude this, is the below query then valid. Also to view this in a comma delimited fashion with three columns next to eachother:

    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 = "Folder"; E = {$_.Folder.Name}} |

    Sort-Object -Property Folder



  • 93.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 01:11 PM

    I forgot to mention to also add ClusterName to the query, this would also be of good use.



  • 94.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 01:31 PM

    Which PowerCLI version are you using?
    There is no point in adapting the selection if you can't get the Get-VM working.



  • 95.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 01:47 PM

    Version 6.3

    Get-VM does work, I get a list of the VM's.



  • 96.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 01:49 PM

    I would suggest you upgrade your PowerCLI installation.



  • 97.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 01:53 PM

    Thank you Luc, will do!



  • 98.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 11, 2019 02:06 PM

    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



  • 99.  RE: Get List of VMs, Datastores and Folder name per Cluster

    Posted Sep 12, 2019 07:44 AM

    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!