Skip to content

Instantly share code, notes, and snippets.

@doyoucompute
Created May 30, 2017 13:34
Show Gist options
  • Save doyoucompute/93e9a12c102484e1acee87bdb40c7db9 to your computer and use it in GitHub Desktop.
Save doyoucompute/93e9a12c102484e1acee87bdb40c7db9 to your computer and use it in GitHub Desktop.
Get-MachineResources
############################
### CDOLLA CDOLLA CDOLLA ###
### CDOLLA CDOLLA CDOLLA ###
### CDOLLA CDOLLA CDOLLA ###
### CDOLLA CDOLLA CDOLLA ###
############################
###
###
### This function returns cpu/mem/C drive utilization.
### Formatting is weird - sorry.
###
###
#Create function.
Function Get-MachineResources {
<#
.SYNOPSIS
This function takes a specified machine(s) and checks resources.
.DESCRIPTION
This function will return the CPU Usage, Memory Usage, and C:\ free disk space.
.EXAMPLE
Get-MachineResources -Machines TEST-MACHINE-01
.EXAMPLE
Get-MachineResources -Machines TEST-MACHINE-01,TEST-MACHINE-02,TEST-MACHINE-03
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage='Provide full machine name(s) separated by commas.')]
[string[]]$Machines
)
#Begin to try.
Try {
Foreach ($Comp in $Machines) {
#Get average of cpus - otherwise will display each core.
$cpusage = Get-WmiObject win32_processor -ComputerName $Comp |
Measure-Object -property LoadPercentage -Average |
Select @{Name = "CPU Average %" ; Expression = {$_.Average}} | Format-Table
#Get memory usage
$memusage = Get-WmiObject -Class win32_operatingsystem -ComputerName $Comp |
Select @{Name = "Memory Usage %" ; Expression = {{0:N2} -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }} | Format-Table
#Get c: disk space usage
$diskspace = Invoke-Command -ComputerName $Comp -ScriptBlock {Get-PSDrive C | Select @{Expression={[math]::Round($_.Free/1GB,2)};Label="Free Space(GB)"} | Format-Table}
#Write it to screen. I like the hostname in red.
(Write-Host -ForegroundColor Red $comp),$cpusage,$memusage,$diskspace
}
}
#Catchem.
Catch {
Write-Warning "Failed to get resources of $Comp. Check your syntax."
}
#Geez, about time.
Finally {
Write-Verbose "Complete."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment