?
Solved

PowerShell Formatting Output

Posted on 2014-12-24
2
Medium Priority
?
161 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
[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
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

718 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