Solved

PowerShell Formatting Output

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

IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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