Solved

put result of format-table in variables

Posted on 2014-11-10
3
129 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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

23 Experts available now in Live!

Get 1:1 Help Now