Vel_VMware
Enthusiast
Enthusiast

Get the host cluster name with respective datastores name.

Jump to solution

Hi,

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

 

Thanks

0 Kudos
3 Solutions

Accepted Solutions
LucD
Leadership
Leadership

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

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

Hi, 

I tried with your code and getting below error.

Vel_VMware_1-1655377898155.png

 

 

 

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership

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

Getting error as attached.

Vel_VMware_0-1655294946321.png

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

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. 

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

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.

0 Kudos
LucD
Leadership
Leadership

Can you show the code you are using?


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

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

Vel_VMware_0-1655299608193.png

 

 

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

How can I stop overwriting $p in the script.

0 Kudos
LucD
Leadership
Leadership

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

Hi, 

I tried with your code and getting below error.

Vel_VMware_1-1655377898155.png

 

 

 

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

I found the issue and fixed it.. 

It works now.. 

Thank you LucD

0 Kudos