Solved

put result of format-table in variables

Posted on 2014-11-10
3
132 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 84

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

Problems using Powershell and Active Directory?

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

This article will help you understand what HashTables are and how to use them in PowerShell.
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 …
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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