Solved

PowerShell Formatting Output

Posted on 2014-12-24
2
150 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 40

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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