Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 137
  • Last Modified:

put result of format-table in variables

Hello,

How can I put the result of format table in variables $logfile, $size_mb, $used_space, max_size :
        $db.get_logfiles() | Format-Table  @{Label="Log File"; Expression={$_.FileName}},`
        @{Label="Size(MB)"; Expression={[math]::round($_.Size/1KB,2)}},`
        @{Label="Used Space(MB)"; Expression={[math]::round($_.UsedSpace/1KB,2)}},`
        @{Label="Max Size(MB)"; Expression={[math]::round($_.MaxSize/1KB,2)}} -a

Thanks

regards
0
bibi92
Asked:
bibi92
1 Solution
 
oBdACommented:
Depends mainly on your preferences; one way would be like this:
$Result = $db.get_logfiles()
$logfile = $Result.FileName
$size_mb = [math]::round($Result.Size/1KB,2)
$used_space = [math]::round($Result.UsedSpace/1KB,2)
$max_size = [math]::round($Result.MaxSize/1KB,2)

$Result | Format-Table -AutoSize -Property `
	@{Label="Log File"; Expression={$logfile}},
	@{Label="Size(MB)"; Expression={$size_mb}},
	@{Label="Used Space(MB)"; Expression={$used_space}},
	@{Label="Max Size(MB)"; Expression={$max_size}}

Open in new window

0
 
QlemoC++ DeveloperCommented:
Does not work if the result consists of more than one row.
Usually it is better to collect info into a single object with custom properties instead of having a set of vars:
$db.get_logfiles() | % { New-Object PsObject -Property @{
  'Log File' = $_.FileName
  'Size (MB)' = [math]::round($_.Size/1KB,2)
  'Used Space (MB)' = [math]::round($_.UsedSpace/1KB,2)
  'Max Size (MB)' = math]::round($_.MaxSize/1KB,2)
} | tee -Variable vals | Format-Table -auto

Open in new window

Note that the properties of $vals are labeled exactly as the format-table result, and will require to be enclosed in double quotes, ticks or curly braces to access them, like in
 $val.{Size (MB)}
0
 
bibi92Author Commented:
Thanks
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now