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!
Sure, everything is possible in PowerShell
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
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
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 <<<< name, $freespaceCalc, $capacityCalc
-
Any thoughts?
<!-- BEGIN browse user content -->
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
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
Fat Finger...
Sure, everything is possible in PowerShell
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
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!