VMware Cloud Community
JimmyDean
Enthusiast
Enthusiast
Jump to solution

Error Getting Datastore Sizes in GB

Hey all,

I'm lost... I followed How to get datastore report in GB When I place the following code in to a test file and call it data.ps1 I get and error.

Connect-VIServer The rest to connect...

<code>$freespaceCalc = @{ Label = "FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } }
$capacityCalc = @{ Label = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } }
Get-Datastore \| ft name, $freespaceCalc, $capacityCalc</code>

out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Format.For matStartData" is not legal or not in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting.

I have searched google but have had not luck in solving my issue. I can cut and paste the lines in the powerCLI and it works just fine. Why will it not work in my script? Anyone?

Thankx!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, everything is possible in PowerShell Smiley Wink

This will produce a table on screen.

$srv = Connect-ViServer -Server <VC-server>

$freespaceCalc = @{ Name = oformat}{code}"FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } } 
$capacityCalc = @{ Name = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } } 
Get-Datastore | Select Name, $freespaceCalc, $capacityCalc | ft -Force | Out-Default

Or you can send it to a CSV file for later use in Excel.

$srv = Connect-ViServer -Server <VC-server>

$freespaceCalc = @{ Name = "FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } } 
$capacityCalc = @{ Name = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } } 
Get-Datastore | Select Name, $freespaceCalc, $capacityCalc | Export-Csv "C:\test.csv" -NoTypeInformation


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

It's caused by the Format-Table cmdlet (alias ft) in the last line.

Try this

Connect-ViServer -Server <VC-server>

$freespaceCalc = @{ Label = "FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } } 
$capacityCalc = @{ Label = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } } 
Get-Datastore | Select name, $freespaceCalc, $capacityCalc


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

0 Kudos
JimmyDean
Enthusiast
Enthusiast
Jump to solution

Luc,

Thanks for the quick responce.

I now get the following error:

-


Select-Object : Illegal Key Label

At Line 5 Char: 23

+ Get-Datastore | Select &lt;&lt;&lt;&lt; name, $freespaceCalc, $capacityCalc

-


Any thoughts?

<!-- BEGIN browse user content -->

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My mistake, I didn't look at the other lines.

It should be

Connect-ViServer -Server <VC-server>

$freespaceCalc = @{ Name = "FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } } 
$capacityCalc = @{ Name = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } } 
Get-Datastore | Select Name, $freespaceCalc, $capacityCalc


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

JimmyDean
Enthusiast
Enthusiast
Jump to solution

Wonderful. One step closer. Is there any way to format the output into a table?

Name FreeSpaceGB CapacityGB

Because the below displays as a list view.

Again thank you for your quick response.

J

0 Kudos
JimmyDean
Enthusiast
Enthusiast
Jump to solution

Fat Finger...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, everything is possible in PowerShell Smiley Wink

This will produce a table on screen.

$srv = Connect-ViServer -Server <VC-server>

$freespaceCalc = @{ Name = oformat}{code}"FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } } 
$capacityCalc = @{ Name = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } } 
Get-Datastore | Select Name, $freespaceCalc, $capacityCalc | ft -Force | Out-Default

Or you can send it to a CSV file for later use in Excel.

$srv = Connect-ViServer -Server <VC-server>

$freespaceCalc = @{ Name = "FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } } 
$capacityCalc = @{ Name = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } } 
Get-Datastore | Select Name, $freespaceCalc, $capacityCalc | Export-Csv "C:\test.csv" -NoTypeInformation


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

0 Kudos
JimmyDean
Enthusiast
Enthusiast
Jump to solution

Lucd,

You are the Master! You gave me everything I needed! Just wanted to be sure that if someone comes by and looks at the below example they will have the corrected code.There was second line had a typo in it.

$freespaceCalc = @{ Name = oformat}"FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } }

The below works great!

$srv = Connect-ViServer -Server <VC-server>

$freespaceCalc = @{ Name = "FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } }

$capacityCalc = @{ Name = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } }

Get-Datastore | Select Name, $freespaceCalc, $capacityCalc | Export-Csv "C:\test.csv" -NoTypeInformation

Thanks!

0 Kudos