Guest Post — Get-myFreeSpace Revisited
May 4th, 2017 by Charlie Russel and tagged Calculated Column, Comment-Based Help, Disk Free Space, ErrorAction, Format-Table, Formatting output, Pipeline, PowerShell, PowerShell Function, Process{}, WMI
Today’s post comes by way of a co-worker, Robert Carlson, who took my previous post on getting the free disk space of remote computers and offered a very useful suggestion — instead of outputting strings, which is only useful for a display or report, he suggests creating a PSCustomObject and outputting that. Slick! I like it.
So, why a PSCustomObject? Because now he can use it to drive automation, rather than simply reporting. A very handy change, and a good reminder for all of us that we should put off formatting until the last possible moment, because once you pipe something to Format-*, you’re done. All your precious objects and their properties are gone, and you’re left with a simple string.
The other thing Robert has done is change this from a script to a function. This makes it easier to call from other scripts and allows it to be added to your “toolbox” module. (More on Toolbox Modules soon. ) A worthy change. So, without further ado, here’s Robert’s revised Get-myFreeSpace function.
function Get-myFreeSpace { <# .Synopsis Gets the disk utilization of one or more computers .Description Get-myFreeSpace queries an array of remote computers and returns a nicely formatted display of their current disk utilization and free space. The output can be redirected to a file or other output option using standard redirection, or can be piped to further commands. .Parameter ComputerName An array of computer names from which you want the disk utilization .Example (Get-VM -Name “*server*” | Where-Object {$_.State -eq ‘Running’).Name } | Get-myFreeSpace Gets the free disk space of the running virtual machines whose name includes 'server' .Inputs [string[]] .Notes Original Author: Charlie Russel Secondary Author: Robert Carlson Copyright: 2017 by Charlie Russel : Permission to use is granted but attribution is appreciated Initial: 26 Nov, 2014 (cpr) ModHist: 29 Sep, 2016 — Changed default to array of localhost (cpr) : 18 Apr, 2017 — Changed to use Write-Output,accept Pipeline,added man page, (cpr) : 20 Apr, 2017 — Changed output to pscustomobject rather than string, etc.(RC) #> [CmdletBinding()] Param( [Parameter(Mandatory=$False,Position=0,` ValueFromPipeline=$True,` ValueFromPipelineByPropertyName=$True,` ValueFromRemainingArguments=$True)] [alias(“Name”,”Computer”)] [string[]] $ComputerName = @(“localhost”) ) Begin { if ($Input) { $ComputerName = @($Input) } } Process { ForEach ( $Computer in $ComputerName ) { $volumes = Get-WmiObject -ComputerName $Computer -Class Win32_Volume -ErrorAction SilentlyContinue foreach ($volume in $volumes) { $volumeData = [pscustomobject]@{ ComputerName=$Computer Drive=$volume.DriveLetter VolumeLabel=$volume.Label VolumeSize=”{0:N0}” -f ($volume.Capacity / 1GB) FreeSpace=”{0:N0}” -f ($volume.FreeSpace/1GB) } if ($volume.Capacity) { $percentage = “{0:P0}” -f ($volume.FreeSpace / $volume.Capacity) $volumeData | Add-Member -NotePropertyName “PercentageFree” -NotePropertyValue $percentage } else { $volumeData | Add-Member -NotePropertyName “PercentageFree” -NotePropertyValue “n/a” } Write-Output $volumeData } } } }
I really appreciate Robert’s contribution, and I thank him profoundly for his suggestion. I learned something, and I hope you have too. I hope you found this useful, and I’d love to hear comments, suggestions for improvements, or bug reports as appropriate. As always, if you use this script as the basis for your own work, please respect my copyright and provide appropriate attribution.
Posted in IT Admin, PowerShell, PowerShell Functions | Comments Off on Guest Post — Get-myFreeSpace Revisited