Hello, I’m unsure how to do exactly what I’m thinking and I think the answer should be pretty plainly in front of me…
$x = get-datastore | ?{$_.name -notlike "*local*"}
Name FreeSpaceGB CapacityGB
---- ----------- ----------
fake1 172.830 1,023.750
fake2 226.777 1,023.750
fake3 95.774 1,023.750
fake4 152.063 1,023.750
fake5 196.396 1,023.750
I want it to be listed like…
Number Name FreeSpaceGB CapacityGB
----- ---- ----------- ----------
0 fake1 172.830 1,023.750
1 fake2 226.777 1,023.750
2 fake3 95.774 1,023.750
3 fake4 152.063 1,023.750
4 fake5 196.396 1,023.750
$x[0] would equal fake1
What would be the easiest way to build this table?
get-datastore | ?{$_.name -notlike "*local*"} |
select-object @{n='Number';exp={[regex]::Match($_.Name,'\d').value - 1}},
FreeSpaceGB, CapacityGB
Another solution is to increment (add 1 to number using ++ operator)
That produced some interesting results (took out the other columns)
Number
------
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
6
6
how would I go about doing an increment for the value of the first expression?
Got it doing
get-datastore | ?{$_.name -notlike "*local*"} |
select-object @{n='Number';exp={$global:counter; $global:counter++} },
FreeSpaceGB, CapacityGB
Thanks for your help!
This will be more than a one liner, but here you go.
$DataStores = Get-Datastore | where {$_.Name -notlike "*local*"} | select @{N='Name'; E={$_.Name}}, @{N = 'FreeSpaceGB'; E={"{0:N0}" -f$_.FreeSpaceGB}}, @{N='CapacityGB'; E={"{0:N0}" -f $_.CapacityGB}}
$Count = 0
foreach($DStore in $DataStores)
{
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name "Number" –Value "$($Count)"
$object | Add-Member –MemberType NoteProperty –Name "Name" –Value "$($DStore.Name)"
$object | Add-Member –MemberType NoteProperty –Name "FreeSpaceGB" –Value "$($DStore.FreeSpaceGB)"
$object | Add-Member –MemberType NoteProperty –Name "CapacityGB" –Value "$($DStore.CapacityGB)"
$Count ++
Write-Output $object
}