PowerCLI

 View Only
Expand all | Collapse all

get VM inventory using get-view

  • 1.  get VM inventory using get-view

    Posted Oct 31, 2024 01:40 PM
    Edited by LucD Oct 31, 2024 02:34 PM

    Hello,

    I've been playing with get-view rather than get-vm and I've been finding it to contain so much more information.  Problem is I'm still struggling to find some fields which I could easily find in get-vm. I'm sure someone (probably LucD :) ) can instantly tell me where these are, but my real question is how do I find this information myself? Up until now I've been filtering on a single VM, then cycling through the various fields until I finally find what I'm looking for. Is there a way to search through all the fields when I know what I'm looking for?

    The fields I'm trying to find are:
    cluster, and VMhost

    Here's have I've gotten so far:

    get-view –viewtype VirtualMachine |
    Select-Object Name,
      @{N='GuestId';E={$_.Config.GuestId}},
      @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}},
      @{N='IP Address';E={$_.Summary.Guest.IpAddress}}, 
      @{N='Template';E={$_.summary.config.Template}}



  • 2.  RE: get VM inventory using get-view
    Best Answer

    Posted Oct 31, 2024 03:14 PM

    The issue might be that some properties are not directly found in the object representing the inventory object.

    The vSphere Web Services API document is your basic reference to consult.

    In your case, start with the VirtualMachine object, since you are looking at VMs.
    It is worth knowing that most objects used in vSphere, are not flat objects.
    They are often nested objects, i.e. a level 1 property points to another object, with it's own properties.

    Back to your question, the VirtualMachine object has a Runtime property that points to a VirtualMachineRuntimeInfo object.
    That object has a Host property, containing a HostSystem object pointer.
    These pointers are called MoRef, which stands for Managed Object Reference.

    With Get-View you can fetch the actual object from such a pointer.
    And in the HostSystem object there is a Name property, which is one of the values your looking for.

    Get-View –ViewType VirtualMachine |
    Select-Object Name,
      @{N='GuestId';E={$_.Config.GuestId}},
      @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}},
      @{N='IP Address';E={$_.Summary.Guest.IpAddress}}, 
      @{N='Template';E={$_.summary.config.Template}},
      @{N='VMHost';E={(Get-View -Id $_.Runtime.Host).Name}}

    To find the cluster, you can use the Parent property of the HostSystem object.

    Get-View –ViewType VirtualMachine -Filter @{Name='TestVM3'}|
    Select-Object Name,
      @{N='GuestId';E={$_.Config.GuestId}},
      @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}},
      @{N='IP Address';E={$_.Summary.Guest.IpAddress}},
      @{N='Template';E={$_.summary.config.Template}},
      @{N='VMHost';E={(Get-View -Id $_.Runtime.Host).Name}},
      @{N='Cluster';E={(Get-View -Id (Get-View -Id $_.Runtime.Host).Parent).Name}}

    Note that the Cluster value will not work when the ESX node is not part of a Cluster.

    Now this code can be optimised.
    Limit the properties returned for an object by Get-View.
    Don't run the same code twice.

    In the end you can end up with something like this

    Get-View -ViewType VirtualMachine -Property Config,Summary,Runtime |
    ForEach-Object -Process {
      $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent
      [PSCustomObject] @{
        GuestId = $_.Config.GuestId
        vCenter = ([uri]$_.Client.ServiceUrl).Host
        'IP Address' = $_.Summary.Guest.IpAddress
        Template = $_.summary.config.Template
        VMHost = $esx.Name
        Cluster = (Get-View -Id $esx.Parent -Property Name).Name
      }
    }
    


    ------------------------------


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


    ------------------------------



  • 3.  RE: get VM inventory using get-view

    Posted Oct 31, 2024 05:24 PM

    Amazing as always LucD, the only field I added was the VM name, but it's tested and working perfectly now. 

    Get-View -ViewType VirtualMachine -Property Config,Summary,Runtime |
    ForEach-Object -Process {
      $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent
      [PSCustomObject] @{
        Name = $_.config.Name
        GuestId = $_.Config.GuestId
        vCenter = ([uri]$_.Client.ServiceUrl).Host
        'IP Address' = $_.Summary.Guest.IpAddress
        Template = $_.summary.config.Template
        VMHost = $esx.Name
        Cluster = (Get-View -Id $esx.Parent -Property Name).Name
      }
    }




  • 4.  RE: get VM inventory using get-view

    Posted Oct 31, 2024 05:24 PM

    What would be the best way to export this to csv? Or would it require a different approach entirely?




  • 5.  RE: get VM inventory using get-view

    Posted Oct 31, 2024 05:25 PM

    Just pipe the output after the end of the Foreach-Object loop to an Export-Csv



    ------------------------------


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


    ------------------------------



  • 6.  RE: get VM inventory using get-view

    Posted Nov 01, 2024 12:01 PM

    Last thing I need to add is folder. Try as I might to do it myself I can't seem to get it. Using James' suggestion I ran this to see if the folder was contained in the virtual machine viewtype:

    get-view -viewtype VirtualMachine -filter @{Name="civ2"} | fc -depth 7

    Could not find it, however I see that I can get folder information from:

    get-view -viewtype Folder which contains the the info I need, but can't seem to figure out how I add this to the script. I've tried:

    Get-View -ViewType VirtualMachine -Property Config,Summary,Runtime |
    ForEach-Object -Process {
      $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent
      $folder = Get-View -ViewType Folder
      [PSCustomObject] @{
        Name = $_.config.Name
        'IP Address' = $_.Summary.Guest.IpAddress
        Folder = $folder.name
        GuestId = $_.Config.GuestId
        Template = $_.summary.config.Template
        vCenter = ([uri]$_.Client.ServiceUrl).Host
        VMHost = $esx.Name
        Cluster = (Get-View -Id $esx.Parent -Property Name).Name
        Notes = $_.Summary.Config.Annotation 
      }
    }|
    Export-Csv -path C:\output\getview_comprehensive_inventory1.csv -NoTypeInformation

    Feel like I'm on the right track, but getting this result for folder:




    Name       : VCNEO
    IP Address : 192.168.0.12
    Folder     : {Datacenters, host, network, datastore...}
    GuestId    : other3xLinux64Guest
    Template   : False
    vCenter    : vcneo.lebrine.local
    VMHost     : grey.lebrine.local
    Cluster    : VSAN
    Notes      : VMware vCenter Server Appliance


    Little help with this last piece?




  • 7.  RE: get VM inventory using get-view

    Posted Nov 01, 2024 01:46 PM

    Try like this

    Get-View -ViewType VirtualMachine -Property Parent, Config, Summary, Runtime |
    ForEach-Object -Process {
      $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent
      [PSCustomObject] @{
        GuestId = $_.Config.GuestId
        vCenter = ([uri]$_.Client.ServiceUrl).Host
        Folder = (Get-View -Id $_.Parent -Property Name).Name
        'IP Address' = $_.Summary.Guest.IpAddress
        Template = $_.summary.config.Template
        VMHost = $esx.Name
        Cluster = (Get-View -Id $esx.Parent -Property Name).Name
      }
    }


    ------------------------------


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


    ------------------------------



  • 8.  RE: get VM inventory using get-view

    Posted Nov 01, 2024 02:39 PM

    Thanks LucD, I think I get it now, I should be able to add all the other fields solo. 




  • 9.  RE: get VM inventory using get-view

    Posted Nov 01, 2024 04:25 PM

    Here's the final product, fields include Name, GuestID, vCenter, Folder, IP Address, Template, VMhost, Cluster, Notes:

    Get-View -ViewType VirtualMachine -Property Parent, Config, Summary, Runtime |
    ForEach-Object -Process {
      $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent
      [PSCustomObject] @{
        Name = $_.config.name
        GuestId = $_.Config.GuestId
        vCenter = ([uri]$_.Client.ServiceUrl).Host
        Folder = (Get-View -Id $_.Parent -Property Name).Name
        'IP Address' = $_.Summary.Guest.IpAddress
        Template = $_.summary.config.Template
        VMHost = $esx.Name
        Cluster = (Get-View -Id $esx.Parent -Property Name).Name
        Notes = $_.summary.config.annotation
      }
    }|
    Export-Csv -Path C:\output\NDH1VMS_v2.csv




  • 10.  RE: get VM inventory using get-view

    Posted Oct 31, 2024 03:30 PM
    Edited by James Dougherty Oct 31, 2024 03:30 PM

    Pretty sure you can use | fc -depth, correct me if I'm wrong.

    get-view -viewtype VirtualMachine -filter @{Name="VMname"} | fc -depth 7
    The greater the depth, the deeper you'll pull info from.  You could then pipe this to a txt file or just copy and paste to notepad and then search for what you are looking for.




  • 11.  RE: get VM inventory using get-view

    Posted Oct 31, 2024 05:24 PM

    Thanks James, I'll play around with this.