VMware Cloud Community
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Get the host cluster name with respective datastores name.

Hi,

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

 

Thanks

Reply
0 Kudos
3 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

 


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

View solution in original post

LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Hi, 

I tried with your code and getting below error.

Vel_VMware_1-1655377898155.png

 

 

 

View solution in original post

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

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

 


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

Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Getting error as attached.

Vel_VMware_0-1655294946321.png

 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

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. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you show the code you are using?


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

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

Vel_VMware_0-1655299608193.png

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

How can I stop overwriting $p in the script.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Hi, 

I tried with your code and getting below error.

Vel_VMware_1-1655377898155.png

 

 

 

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

I found the issue and fixed it.. 

It works now.. 

Thank you LucD

Reply
0 Kudos