Skip to content

Instantly share code, notes, and snippets.

@camusicjunkie
Last active January 23, 2018 07:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save camusicjunkie/9ec753db21d474df49f64b910b9dc88b to your computer and use it in GitHub Desktop.
function Get-ComputerStats {
<#
.SYNOPSIS
Gets computer and disk information
.DESCRIPTION
Gets computer information and disk information for all
logical disks. Each disk is added as part of the other
properties for as many type 3 disks that exist
.PARAMETER ComputerName
Specifies a remote computer. The default is the local computer
.EXAMPLE
Get-ComputerStats -ComputerName localhost
.EXAMPLE
'localhost' | Get-ComputerStats
.INPUTS
[System.String]
.OUTPUTS
[Custom.IronScripter.ComputerStats]
#>
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[Alias('computer', 'cn', 'workstation')]
[string[]] $ComputerName = $env:COMPUTERNAME
)
begin {
Write-Verbose "[BEGIN ] $(Get-Date) Starting: $($MyInvocation.MyCommand)"
$PSDefaultParameterValues = @{
'Get-CimInstance:Verbose' = $false
'New-CimSession:Verbose' = $false
}
}
process {
foreach ($computer in $ComputerName) {
Write-Verbose "[PROCESS] $(Get-Date) Processing $computer"
try {
$osParams = @{'ClassName' = 'win32_OperatingSystem'}
$diskParams = @{'ClassName' = 'win32_LogicalDisk'
'Filter' = 'DriveType = 3'}
if ($PSBoundParameters.ContainsKey('ComputerName')) {
Write-Verbose "Trying to create session to $computer"
$params = @{'ComputerName' = $computer
'ErrorAction' = 'Stop'
}
$session = New-CimSession @params
Write-Verbose "Session was created to $computer"
# Add cimSession parameter to all param hashtables
$osParams.Add('CimSession', $session)
$diskParams.Add('CimSession', $session)
}
Write-Verbose "Collecting info from $computer"
$os = Get-CimInstance @osParams
$props = @{
'PSTypename' = 'Custom.IronScripter.ComputerStats'
'ComputerName' = $computer
'OperatingSystemName' = $os.Caption
'OperatingSystemVersion' = $os.Version
'ServicePackMajorVersion' = $os.ServicePackMajorVersion
'OperatingSystemManufacturer' = $os.Manufacturer
'WindowsDirectory' = $os.WindowsDirectory
'Locale' = [CultureInfo]([Convert]::ToInt32($os.locale,16))
'FreePhysicalMemory' = $os.FreePhysicalMemory
'TotalVirtualMemorySize' = $os.TotalVirtualMemorySize
'FreeVirtualMemory' = $os.FreeVirtualMemory
}
$disks = Get-CimInstance @diskParams
foreach ($disk in $disks) {
$driveLetter = $disk.DeviceID
$used = (100 - ($disk.FreeSpace/$disk.Size * 100))
$props.Add("DriveType($driveLetter)", $disk.Description)
$props.Add("Size($driveLetter)", $disk.Size)
$props.Add("FreeSpace($driveLetter)", $disk.FreeSpace)
$props.Add("Compressed($driveLetter)", $disk.Compressed)
$props.Add("UsedPct($driveLetter)", "{0:N0}" -f $used)
}
Write-Verbose "Creating custom PSObject from collected info"
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
catch [Microsoft.Management.Infrastructure.CimException] {
Write-Warning "Could not create a session to $computer"
}
catch {
Write-Warning "Something went wrong"
}
finally {
if ($session) {
Write-Verbose "Removing session from $computer"
$session | Remove-CimSession
}
}
}
}
end {
Write-Verbose "[END ] $(Get-Date) Ending: $($MyInvocation.MyCommand)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment