VMware Cloud Community
eaphilipp
Contributor
Contributor
Jump to solution

Datastore script problem

I am wondering if you can look at this script and tell me what I am doing wrong. Basically I am trying to write a simple script that asks for a minimum free space and searches all data stores and reports back…

Connect-VIServer -Server "vcenter1" -Credential(Get-Credential)

$a = Read-Host "Enter minimum datastore size in GB's to search all stores by: "

Get-vmhost | Get-Datastore | ? { $_.FreespaceMB -lt ( "$a"GB/1MB) }

I am new to powershell and really trying to learn it because I definately see the benefit.

Thanks in advance!

eric

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try it like this

$a = Read-Host "Enter minimum datastore size in GB's to search all stores by: "
Get-vmhost | Get-Datastore | ? { $_.FreespaceMB -lt ( ([int]$a * 1KB)) } 

The Read-Host cmdlet returns a string, use the explicit conversion to integer to do the calculation.

The FreespaceMB is in MB, so you only have to multiple the input (in GB) by 1024 (or 1KB) to get to MB

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try it like this

$a = Read-Host "Enter minimum datastore size in GB's to search all stores by: "
Get-vmhost | Get-Datastore | ? { $_.FreespaceMB -lt ( ([int]$a * 1KB)) } 

The Read-Host cmdlet returns a string, use the explicit conversion to integer to do the calculation.

The FreespaceMB is in MB, so you only have to multiple the input (in GB) by 1024 (or 1KB) to get to MB

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
eaphilipp
Contributor
Contributor
Jump to solution

Works!

Thanks for your help, someday I will get this powershell thing.

Reply
0 Kudos