Solved

put result of format-table in variables

Posted on 2014-11-10
3
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 85

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 70

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

630 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