Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PowerShell Formatting Output

Posted on 2014-12-24
2
Medium Priority
?
164 Views
Last Modified: 2014-12-24
Hey Experts,

I have a PowerShell script that pulls WMI information for a server to get the specs of the machine.  When I query win32_logicaldisk I get back the information on disk space, it comes back as:

DriveSpace: @{DeviceID=C:; Size=64422408192} | @{DeviceID=E:; Size=32209104896}

But I'd like for it to show up as:
C: 64422408192 | E: 32209104896


If at all possible I'd rather have it in GBs but when I try to use any sort of calculation on it, it doesn't bring back anything for the drive space.

Below is the script I'm using:

cls
$serverlist = @("Server1")
foreach ($server in $serverList ){ 
$CPU = gwmi win32_processor -ComputerName $server -Property "systemname","deviceID","maxclockspeed","addressWidth","numberOfCores","NumberOfLogicalProcessors" | Select "systemname","deviceID","maxclockspeed","addressWidth","numberOfCores","NumberOfLogicalProcessors" 
$Disk = gwmi win32_logicaldisk -ComputerName $server -Property "DeviceID","Size","FreeSpace","VolumeName" | Select "DeviceID","Size","FreeSpace","VolumeName" | Where-Object {$_.Size -ne $null}
$ServerSpecs = "" | Select Computer,NumCPUs,CPUSpeed,Bit,DriveSpace
$ServerSpecs.Computer = ($CPU | Select systemname -Unique).systemname
$ServerSpecs.NumCPUs = (($CPU.NumberofLogicalProcessors) | Measure-Object -Sum).Sum
$ServerSpecs.CPUSpeed = ((($CPU.maxclockspeed | Measure-Object -Average).average)/1000.0).ToString() + " GHz"
$ServerSpecs.Bit = ($CPU.addressWidth | Measure-Object -Average).average
$ServerSpecs.DriveSpace = ([string]::Join(' | ',($Disk | Select "DeviceID","Size")))
$ServerSpecs
}

Open in new window


Which brings back the following information:

Computer   : Server1
NumCPUs    : 2
CPUSpeed   : 3.456 GHz
Bit        : 64
DriveSpace : @{DeviceID=C:; Size=64422408192} | @{DeviceID=E:; Size=32209104896}
0
Comment
Question by:Hankinater
2 Comments
 
LVL 41

Accepted Solution

by:
footech earned 2000 total points
ID: 40516690
The reason you're getting that output is because you're joining (complex) objects (i.e. with properties), rather than just simple string objects.

Try substituting this for line 11.
$ServerSpecs.DriveSpace = ($Disk | % { "{0} {1:N2} GB" -f $_.DeviceID,($_.Size / 1GB) }) -join " | "

Open in new window

0
 
LVL 2

Author Closing Comment

by:Hankinater
ID: 40516702
That fixed it for me, thanks very much!
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
Loops Section Overview

772 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