Solved

put result of format-table in variables

Posted on 2014-11-10
3
127 Views
Last Modified: 2014-11-12
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
Comment
Question by:bibi92
3 Comments
 
LVL 82

Expert Comment

by:oBdA
ID: 40432784
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
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40433362
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
 

Author Closing Comment

by:bibi92
ID: 40438162
Thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now