Outputting to table format?

Author Message
ginolard

  • Total Posts : 1315
  • Scores: 23
  • Reward points : 0
  • Joined: 8/11/2005
  • Status: offline
Outputting to table format? - Tuesday, February 02, 2010 8:36 PM ( #1 )
God, I feel like a newbie again with Powershell.

I have the following script to get disk data

 function GetData($drive) {
 #This function receives a drive object from the Win32_LogicalDisk WMI class and calculates the total size, freespace, amount used and 
 #the associated percentages.
     PROCESS {
     $obj = New-Object PSObject
     $total = [Math]::Round($drive.size/1gb,2)
     $free = [Math]::Round($drive.Freespace/1gb,2)
     $used = [Math]::Round(($drive.size-$drive.Freespace)/1gb,2)
 
     $PercentUsed = ($used/$Total)
     $PercentUsed = "{0:P2}" -f ($used/$Total) 
 
     $PercentFree = ($free/$Total)
     $PercentFree = "{0:P2}" -f $PercentFree
     $obj | Add-Member NoteProperty Server($drive.systemname) -force
     $obj | Add-Member NoteProperty Drive($drive.name) -force
     $obj | Add-Member NoteProperty Total($total) -force
     $obj | Add-Member NoteProperty Used($used) -force
     $obj | Add-Member NoteProperty Free($free) -force
     $obj | Add-Member NoteProperty "% Used"($PercentUsed) -force
     $obj | Add-Member NoteProperty "% Free"($PercentFree) -force
     $obj | Add-Member NoteProperty Volume($drive.volumename) -force
     Write-Output $obj 
 
 }
 }
 
 $currentdate = get-date # Get the current date
 Write-Output "SERVER DISK USAGE AS OF" $currentdate   #Write the title
 
 $servers = gc c:\temp\test.txt
 
 
 foreach ($server in $servers) { 
 $disks = Get-WmiObject Win32_LogicalDisk -ComputerName $server | Where-Object {$_.DriveType -eq 3} 
 
 foreach ($d in $disks) {
     GetData $d
     }
 }
 


The output, however, comes out like this

Server : D02DI0908216R
Drive  : E:
Total  : 6.17
Used   : 0.05
Free   : 6.11
% Used : 0.81 %
% Free : 99.03 %
Volume : SDMCACHE

From what I've read on MSDN it SHOULD output in a table format (rows and columns) with column headings but I can't see to get it to do that.  Any idea what I'm doing wrong?
Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
ginolard

  • Total Posts : 1315
  • Scores: 23
  • Reward points : 0
  • Joined: 8/11/2005
  • Status: offline
Re:Outputting to table format? - Tuesday, February 02, 2010 9:33 PM ( #2 )
Well, I've managed to do it like this now

 $drives = gwmi win32_logicaldisk -filter "drivetype=3" -ComputerName (gc c:\temp\test.txt) 
 
 $total = @{n="Total";e={"{0:N2}" -f ($_.Size/1GB)}} 
 $free = @{n="Free";e={"{0:N2}" -f ($_.FreeSpace/1GB)}} 
 $used = @{n="Used";e={"{0:N2}" -f (($_.Size-$_.FreeSpace)/1GB)}} 
 $server = @{n="Server";e={$_.__SERVER}} 
 $drive = @{n="Drive";e={$_.NAME}} 
 $volume = @{n="Volume";e={$_.VOLUMENAME}} 
 $PercentUsed=@{Name=”% Used”;Expression={‘{0:P2}’ -f(($_.Size/1gb-$_.freespace/1gb) / ($_.size/1gb))}}
 $PercentFree=@{Name=”% Free”;Expression={‘{0:P2}’ -f(($_.freespace/1gb) / ($_.size/1gb))}}
 
 $drives | Select $server,$drive,$total,$used,$free,$PercentUsed,$PercentFree,$volume | ft | Out-File c:\temp\diskspace.txt
 


That works just fine but it seems a bit messier than the object-oriented method above (which I can't get to work).
Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
ginolard

  • Total Posts : 1315
  • Scores: 23
  • Reward points : 0
  • Joined: 8/11/2005
  • Status: offline
Re:Outputting to table format? - Wednesday, February 03, 2010 3:38 AM ( #3 )
OK, here's the final version I've settled on but there's ONE issue.

 Get-WMIObject Win32_LogicalDisk -filter “DriveType=3" -computer (Get-Content c:\temp\serverlist.txt) | ` 
 Select @{Name="Server";Expression={$_.SystemName}},` 
 @{Name="Drive";Expression={$_.DeviceID }},`
 @{Name=”Total”;Expression={([string][Math]::Round($_.size/1gb,2))+ " Gb" }},`
 @{Name=”Used”;Expression={[string][Math]::Round(($_.size-$_.freespace)/1gb,2)+ " Gb"}},`
 @{Name=”Free”;Expression={[string]([Math]::Round($_.freespace/1gb,2)) + " Gb"}},`
 @{Name=”% Used”;Expression={“{0:P2}” -f(($_.size/1gb - $_.freespace/1gb)/($_.size/1gb))}},`
 @{Name=”% Free”;Expression={“{0:P2}” -f(($_.size/1gb - $_.freespace/1gb)/($_.size/1gb))}},VolumeName| Format-Table -auto 
 


This produces output like this

Server Drive Total Used Free % Used % Free Volume
------ ----- ----- ---- ---- ------ ------ ------
SERVER1 C: 14.66 8.49 6.18 57.88 % 42.12 % SYSTEM
SERVER1 D: 53.67 0.36 53.31 0.67 % 99.33 % DATA
SERVER1 E: 6.17 0.05 6.11 0.88 % 99.12 % SDMCACHE
SERVER2 C: 14.66 7.95 6.72 54.19 % 45.81 % SYSTEM
SERVER2 D: 53.67 0.08 53.59 0.14 % 99.86 % DATA
SERVER2 E: 6.17 0.03 6.13 0.54 % 99.46 % SDMCACHE

And I would prefer a blank line separating the servers, so like this

Server Drive Total Used Free % Used % Free Volume
------ ----- ----- ---- ---- ------ ------ ------
SERVER1 C: 14.66 8.49 6.18 57.88 % 42.12 % SYSTEM
SERVER1 D: 53.67 0.36 53.31 0.67 % 99.33 % DATA
SERVER1 E: 6.17 0.05 6.11 0.88 % 99.12 % SDMCACHE

SERVER2 C: 14.66 7.95 6.72 54.19 % 45.81 % SYSTEM
SERVER2 D: 53.67 0.08 53.59 0.14 % 99.86 % DATA
SERVER2 E: 6.17 0.03 6.13 0.54 % 99.46 % SDMCACHE

Any ideas how to do that?
Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
ebgreen

  • Total Posts : 7236
  • Scores: 72
  • Reward points : 0
  • Joined: 7/12/2005
  • Status: offline
Re:Outputting to table format? - Friday, February 05, 2010 3:13 AM ( #4 )
I think to get a space between each server, I would do it like this (untested):

Get-Content c:\temp\serverlist.txt | %{ gwmi Win32_LogicalDisk -Filter "DriveType=3" -Computer $_ | INSERT MASSIVE SELECT STATEMENT HERE | Format-Table -auto; ""}

That "" on the end should output a blank line after each server has been processed.
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.
Jump to:

Current active users
There are 0 members and 1 guests.
Icon Legend and Permission
  • New Messages
  • No New Messages
  • Hot Topic w/ New Messages
  • Hot Topic w/o New Messages
  • Locked w/ New Messages
  • Locked w/o New Messages
  • Read Message
  • Post New Thread
  • Reply to message
  • Post New Poll
  • Submit Vote
  • Post reward post
  • Delete my own posts
  • Delete my own threads
  • Rate post

© 2000-2009 ASPPlayground.NET Forum Version 3.6