Created
December 20, 2014 04:27
-
-
Save vScripter/d6b4d87d7d8fb5f0cd14 to your computer and use it in GitHub Desktop.
PS Custom Object Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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