Forked from doyoucompute/Get-MachineResources.ps1
Created
September 21, 2017 20:57
-
-
Save NathanTheGr8/7fe137137de644fa75b3943410d14735 to your computer and use it in GitHub Desktop.
Get-MachineResources
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
############################ | |
### 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