Review one PowerShell script like an engineer
Documentation: Microsoft Learn
The goal is not to crown the cleverest one-liner. It is to make a useful script easier to trust, reuse and maintain.
Overview
SCRIPT OF THE WEEK
Inventory disk space without hiding failures
1PS> param([string[]]$ComputerName)
2PS>
3PS> foreach ($computer in $ComputerName) {
4PS> try {
5PS> $diskParams = @{
6PS> ComputerName = $computer
7PS> Filter = 'DriveType=3'
8PS> ErrorAction = 'Stop'
9PS> }
10PS> Get-CimInstance Win32_LogicalDisk @diskParams |
11PS> Select-Object @{ Name = 'ComputerName'; Expression = {$computer} },
12PS> DeviceID, Size, FreeSpace
13PS> }
14PS> catch {
15PS> [pscustomobject]@{
16PS> ComputerName = $computer
17PS> DeviceID = $null
18PS> Size = $null
19PS> FreeSpace = $null
20PS> Error = $_.Exception.Message
21PS> }
22PS> }
23PS> }