Automation

 View Only
Expand all | Collapse all

Get-View question

LucD

LucDJan 20, 2021 06:51 PM

  • 1.  Get-View question

    Posted Jan 20, 2021 05:53 PM

    We have an multi datacenter environment with linked vCenters.  I'm trying to build a PowerCLI script to create a csv report listing all VMs with PowerState, ToolsVersion and ToolsStatus.

    I've made it that far (by digging through these forums  ), but I also want to include Datacenter and Cluster for the VMs so the report can be sorted/filtered in a spreadsheet by those values.  I'm a PowerCLI noob so I'm getting a bit lost.

    Here's what I have so far... the script does a connect-viserver to each of the vCenters and then runs this and pipes it to Export-CSV:

     

    Any help in getting the datacenter and cluster info in there would be greatly appreciated!

    Thanks,
    Darryl



  • 2.  RE: Get-View question

    Posted Jan 20, 2021 06:14 PM

    Instead of getting the Datacenter and Cluster from the VM, I would suggest looping through Datacenters and Clusters.
    With the SearchRoot parameter you can restrict where Get-View looks for objects.



  • 3.  RE: Get-View question

    Posted Jan 20, 2021 06:40 PM

    Nice!  So then, if I just add a pipe to Export-csv to the end like so, I should be good to go right?

     



  • 4.  RE: Get-View question

    Posted Jan 20, 2021 06:51 PM

    Correct



  • 5.  RE: Get-View question

    Posted Jan 20, 2021 07:31 PM

    Something went wrong.  The output lists many VMs in the wrong datacenter, ie: VM1 in DatacenterA - Cluster1 is showing as VM1 in DatacenterB - Cluster1.  Seems like some data got mixed up in the pipelining.



  • 6.  RE: Get-View question

    Posted Jan 20, 2021 07:46 PM

    I have no idea how this could happen.
    The SearchRoot parameter normally limits the search, and via the PipelineVariable you should see the correct Datacenter and Cluster.



  • 7.  RE: Get-View question

    Posted Jan 20, 2021 10:08 PM

    Also, I would recommend using the excellent module "ImportExcel" to quickly format your results.
    https://www.powershellgallery.com/packages/ImportExcel

    Replace "export-csv ...." with "Export-Excel -AutoSize -TableStyle Medium10 -AutoFilter -FreezeTopRow -Show -WorksheetName 'My Report'" for example.



  • 8.  RE: Get-View question

    Posted Jan 21, 2021 03:19 PM

    It is strange, there is definitely something wrong with the way the nested ForEach loops are saving the results.



  • 9.  RE: Get-View question

    Posted Jan 21, 2021 03:23 PM

    Can you try this alternative, without the Pipelinevariable.



  • 10.  RE: Get-View question

    Posted Jan 21, 2021 04:44 PM

    Same thing.  It looks like it's getting the clusters right but mixing up some of the datacenters.



  • 11.  RE: Get-View question

    Posted Jan 21, 2021 04:48 PM

    Sorry, works for me.
    I have no clue why it doesn't work for you.



  • 12.  RE: Get-View question

    Posted Jan 21, 2021 05:04 PM

    No problem, it doesn't make sense to me either.  It should work.

    I'll have to do some more experimenting or find another alternate method.

     

    Thanks,
    Darryl



  • 13.  RE: Get-View question

    Posted Jan 21, 2021 05:08 PM

    One more thing you could check, disconnect from all vCenters.
    And then connect to only 1 vCenter



  • 14.  RE: Get-View question

    Posted Jan 21, 2021 05:14 PM

    That just returns the VMs under the vCenter I'm connected to, so only the one datacenter.



  • 15.  RE: Get-View question
    Best Answer

    Posted Jan 21, 2021 05:26 PM

    Ok, then try the following when you are connected to all your vCenters.



  • 16.  RE: Get-View question

    Posted Jan 21, 2021 07:08 PM

    I think we have a winner!!  Did a few spot checks of the results and all looks to be in the right place.

    Thanks so much for the help!

     

    Regards,
    Darryl



  • 17.  RE: Get-View question

    Posted Jan 21, 2021 07:11 PM

    The issue might have been related to your vCenters being in linked-mode.