0

Good morning,

So I have been teaching myself PowerShell, and I have come across a nagging question to a problem I am having at work. so to minimize time on system checks I am trying to write something that can be run and return a value of say a system with less then X% free space on a given drive.

I have put the list of hostnames into a file and have done a get-content etc into a $servers variable the below code works and displays lists of the servers and the drive size on freespace etc. so works OK. but i want it better

foreach ($server in $servers) {
Get-WmiObject Win32_LogicalDisk -ComputerName $server  | format-table deviceid, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
}

What I want to do is take the output of the foreach into variables that I can then run some math of to get % then get an output to something say below 20%

but for the life of me I cannot figure out how to get the output of this to variable i can call

deviceid  Size FreeSpace
--------  ---- ---------
C:       69.51      42.5
D:         400    146.73
E:          10      6.67
X:           0         0

This being the output so no server names etc. If I knew how to articulate what it actually is I want I would be able to find it I know. but I can't.

Any help would be amazing

boxdog
  • 7,894
  • 2
  • 18
  • 27
  • As an aside: `Format-*` cmdlets output objects whose sole purpose is to provide _formatting instructions_ to PowerShell's for-display output-formatting system. In short: only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_ - see [this answer](https://stackoverflow.com/a/55174715/45375) for more information. – mklement0 May 11 '22 at 12:42
  • As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 May 11 '22 at 12:42
  • Also, `Get-WmiObject` / `Get-CimInstance` accept an _array_ of server names, so you can simplify your command to: `$diskInfos = Get-CimInstance Win32_LogicalDisk -ComputerName $servers` (no need for a loop). `$diskInfos` then contains an array of objects describing the disks, which you can subsequently work with. – mklement0 May 11 '22 at 13:03

1 Answers1

0

You can assign the output from the entire loop to a variable like so:

$disks = foreach($server in $servers) {
    Get-WmiObject Win32_LogicalDisk -ComputerName $server
}

# do math with $disks

$disks | Format-Table DeviceID, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206