VMware Cloud Community
AnkurS11
Contributor
Contributor
Jump to solution

Script to get infomration of datastore per cluster

Hi All,

Good Morning!

I am trying to write a script to get all datastore in the cluster and it should give me the data in table format like datastorename, clustername,capacityGB, freespaceGB,usedspace=freespaceGB/capacityGB *100

#$VCName = Read-Host "Enter the VC name "

Connect-VIServer VCname -WarningAction Continue

clear

$cls = get-cluster

Get-Datastore|where {$_.name -notlike "*local*" -and $_.name -notlike "*template*"}| Format-Table name ,capacityGB, freespaceGB -AutoSize

the above get-datastore cmd is giving me the list of all the datastore and capacityGB, freespaceGB but not the cluster name. I tried for loop as well but not getting the desired result.

Can someone correct my script above.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$VCName = Read-Host "Enter the VC name "

Connect-VIServer VCname -WarningAction Continue

clear

Get-Cluster -PipelineVariable cluster |

Get-Datastore |

where {$_.name -notlike "*local*" -and $_.name -notlike "*template*"} |

Select @{N='Cluster';E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}}

 


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$VCName = Read-Host "Enter the VC name "

Connect-VIServer VCname -WarningAction Continue

clear

Get-Cluster -PipelineVariable cluster |

Get-Datastore |

where {$_.name -notlike "*local*" -and $_.name -notlike "*template*"} |

Select @{N='Cluster';E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}}

 


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

Thanks For reply LucD that gives the below desired data. Can i get this data in table format.

Cluster     : ClusterName

Name        : DataStoreName

CapacityGB  : 8191.75

FreeSpaceGB : 5198.4091796875

UsedSpace   : 63.459079924161503952146977140

Can i get this data in table format. if i pipe it again with format-table as below

Get-Cluster -PipelineVariable cluster |

Get-Datastore |

where {$_.name -notlike "*local*" -and $_.name -notlike "*template*"} |

Select @{N='Cluster';E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} | Format-Table datastoreName, clusterName, CapacityGB,FreespaceGB,UsedSpace -Autosize

Name          clusterName       CapacityGB      FreespaceGB     UsedSpace

DataStoreName    ClusterName      8191.75          5198.40         63.45

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, like this

$VCName = Read-Host "Enter the VC name "

Connect-VIServer VCname -WarningAction Continue

clear

Get-Cluster -PipelineVariable cluster |

Get-Datastore |

where {$_.name -notlike "*local*" -and $_.name -notlike "*template*"} |

Select @{N='Cluster';E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} |

Format-List

 


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

even after |format-list its giving data into rows and i would like to get it per column like below

Name          clusterName       CapacityGB      FreespaceGB     UsedSpace

DataStoreName    ClusterName      8191.75          5198.40         63.45

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you use a Format-Table

$VCName = Read-Host "Enter the VC name "

Connect-VIServer VCname -WarningAction Continue

clear

Get-Cluster -PipelineVariable cluster |

Get-Datastore |

where {$_.name -notlike "*local*" -and $_.name -notlike "*template*"} |

Select @{N='Cluster';E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} |

Format-Table -AutoSize

 


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

AnkurS11
Contributor
Contributor
Jump to solution

Thanks a ton LucD. that exactly i wanted.

0 Kudos
haripadmam
Hot Shot
Hot Shot
Jump to solution

Hi LucD, I see "PipelineVariable" won't be recognized under PowerShell version we use. Is there alternate how we can achieve this under PowerShell v3? I saw your article under below link, however, unable to put it together as usable script!

Its just that we want to export Cluster details (not SDRS) as well when exporting datastore list (Name, capacity, free).

https://communities.vmware.com/thread/595182

Thanks,

Hari.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

$VCName = Read-Host "Enter the VC name "

Connect-VIServer VCname -WarningAction Continue

clear


Get-Cluster | ForEach-Object -Process {

   $cluster = $_

   Get-Datastore -RelatedObject $cluster |

   where { $_.name -notlike "*local*" -and $_.name -notlike "*template*" } |

  Select @{N = 'Cluster'; E = { $cluster.Name } }, Name, CapacityGB, FreespaceGB, @{N = 'UsedSpace'; E = { $_.FreeSpaceGB / $_.CapacityGB * 100 } }

} | Format-Table -AutoSize


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

0 Kudos
haripadmam
Hot Shot
Hot Shot
Jump to solution

that did the magic! Thank you.

 

Output seems muffled, is there way to correct it? I remember using Autofit should do it but not sure whats wrong. First and second is cluster and DS name and then all cut short.

Dev-15   Dev-15_001                  ...75

Thank you!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's probably due to the linelength of the console.
Send it a file (for example with Export-Csv), there you should see all values and complete.


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

0 Kudos
haripadmam
Hot Shot
Hot Shot
Jump to solution

replace format-table with export-csv and it did given desired output!

Thank you very much for the prompt help! 🙂

Thanks,

Hari.

0 Kudos