Solved

PowerShell Formatting Output

Posted on 2014-12-24
2
143 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 39

Accepted Solution

by:
footech earned 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

707 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

12 Experts available now in Live!

Get 1:1 Help Now