Skip to content

Instantly share code, notes, and snippets.

@vScripter
Created December 20, 2014 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vScripter/d6b4d87d7d8fb5f0cd14 to your computer and use it in GitHub Desktop.
Save vScripter/d6b4d87d7d8fb5f0cd14 to your computer and use it in GitHub Desktop.
PS Custom Object Example
[cmdletbinding(PositionalBinding = $true,
DefaultParameterSetName = "Default")]
param (
[parameter(mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[alias('Comp', 'Name', 'DNSHostName')]
[string[]]$ComputerName = 'localhost'
)
BEGIN {
#Requires -Version 3
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
$objResults = @()
$SizeInGB = @{ Name = "SizeGB"; Expression = { "{0:N2}" -f ($_.Size/1GB) } }
$FreespaceInGB = @{ Name = "FreespaceGB"; Expression = { "{0:N2}" -f ($_.Freespace/1GB) } }
$PercentFree = @{ name = "PercentFree"; Expression = { [int](($_.FreeSpace/$_.Size) * 100) } }
} # BEGIN
PROCESS {
foreach ($c in $ComputerName) {
if (Test-Connection -ComputerName $c -Count 2 -Quiet) {
try {
Write-Verbose -Message "Working on $c"
$diskQuery = $null
$diskQuery = Get-WmiObject -ComputerName $c -Query "SELECT SystemName,Caption,VolumeName,Size,Freespace,DriveType FROM win32_logicaldisk WHERE drivetype = 3" |
Select-Object SystemName, Caption, VolumeName, $SizeInGB, $FreespaceInGB, $PercentFree
foreach ($item in $diskQuery) {
$objDiskInfo = @()
$objDiskInfo = [PSCustomObject] @{
SystemName = $item.SystemName
DriveLetter = $item.Caption
VolumeName = $item.VolumeName
SizeGB = $item.SizeGB
FreeSpaceGB = $item.FreeSpaceGB
PercentFree = $item.PercentFree
} # $objDiskInfo
$objResults += $objDiskinfo
} # foreach
} catch {
Write-Warning -Message "$c - $_"
} # try/catch
} else {
Write-Warning -Message "$c - Unreachable via Ping"
} # if/else
} # foreach
} # PROCESS
END {
$objResults
} # END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment