Contributor
Contributor

Datastore script problem

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
Leadership
Leadership

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

0 Kudos
2 Replies
Leadership
Leadership

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

0 Kudos
Contributor
Contributor

Works!

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

0 Kudos