Automation

 View Only
Expand all | Collapse all

Get the host cluster name with respective datastores name.

  • 1.  Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 10:59 AM

    Hi,

    I have list datastores Name, I need to get the respective hosts cluster name in powercli. Please help me.

     

    Thanks



  • 2.  RE: Get the host cluster name with respective datastores name.
    Best Answer

    Posted Jun 15, 2022 11:10 AM

    You didn't specify in which format that "list datastores Nameis provided, but when I assume it is a CSV with a column named DSName, you could do

    Import-Csv -Path .\datastores.csv -UseCulture |
    Foreach-Object -Process {
       Get-Datastore -Name $_.DSName |
       Select Name,
         @{N='Cluster';E={(Get-VMHost -Datastore $_ | Get-Cluster).Name}}
    }

     



  • 3.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 12:09 PM

    Getting error as attached.

    Vel_VMware_0-1655294946321.png

     



  • 4.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 12:33 PM

    Where is that Disconnect-VIServer coming from?
    That was not in the code I posted.
    The code assumes you are connected to the vCenter.



  • 5.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 12:36 PM

    Disconnect-VIServer is added my self. Script is working now. But, When I try to export data in CSV, getting  cluster detail for only one datastore only. otherwise on screen output shows for all datastore. 



  • 6.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 12:41 PM

    That probably means you placed the Export-Csv inside the Forwach-Object loop, hence overwriting the CSV each time.
    You have to place the Export-Csv outside the Foreach-Object code block



  • 7.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 01:03 PM

    Now, I have kept my Export-CSV in out of foreach, but still I am getting cluster detail only for last datastore in the list.



  • 8.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 01:22 PM

    Can you show the code you are using?



  • 9.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 01:27 PM

    Unable to copy and paste the code here due to limited access, just providing the screenshot of the code.

    Vel_VMware_0-1655299608193.png

     

     



  • 10.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 01:31 PM

    You are overwriting the variable $p inside the loop each time.



  • 11.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 15, 2022 01:48 PM

    How can I stop overwriting $p in the script.



  • 12.  RE: Get the host cluster name with respective datastores name.
    Best Answer

    Posted Jun 15, 2022 02:03 PM

    Perhaps you could use the code I give and not change it?

    Import-Csv -Path .\datastores.csv -UseCulture |
    Foreach-Object -Process {
       Get-Datastore -Name $_.DSName |
       Select Name,
         @{N='Cluster';E={(Get-VMHost -Datastore $_ | Get-Cluster).Name}}
    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


  • 13.  RE: Get the host cluster name with respective datastores name.
    Best Answer

    Posted Jun 16, 2022 11:12 AM

    Hi, 

    I tried with your code and getting below error.

    Vel_VMware_1-1655377898155.png

     

     

     



  • 14.  RE: Get the host cluster name with respective datastores name.

    Posted Jun 16, 2022 11:17 AM

    I found the issue and fixed it.. 

    It works now.. 

    Thank you LucD