Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Created August 23, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/6ebfad97b5c62ee6ad9561f0bd29d5e4 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/6ebfad97b5c62ee6ad9561f0bd29d5e4 to your computer and use it in GitHub Desktop.
A demonstration PowerShell 7 function using $PSStyle as default formatting. Put both files in the same folder. Dot source the ps1 file.
#requires -version 7.2
Function Get-Status {
[cmdletbinding(DefaultParameterSetName = 'name')]
[alias("gst")]
Param(
[Parameter(
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
HelpMessage = 'Enter the name of a computer',
ParameterSetName = 'name')
]
[ValidateNotNullOrEmpty()]
[string]$Computername = $env:computername,
[Parameter(ParameterSetName = 'name')]
[pscredential]$Credential,
[Parameter(ParameterSetName = 'Session', ValueFromPipeline)]
[CimSession]$Cimsession,
[Parameter(HelpMessage = "Format values as [INT]")]
[switch]$AsInt
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay)] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay)] Using parameter set $($pscmdlet.ParameterSetName)"
$sessParams = @{
ErrorAction = 'stop'
computername = $null
}
$cimParams = @{
ErrorAction = 'stop'
classname = $null
}
if ($pscmdlet.ParameterSetName -eq 'name') {
#create a temporary Cimsession
$sessParams.Computername = $Computername
if ($Credential) {
$sessParams.Credential = $credential
}
#if localhost use DCOM - it will be faster to create the session
if ($Computername -eq $env:computername) {
Write-Verbose "[$((Get-Date).TimeofDay)] Creating a local session using DCOM"
$sessParams.Add("SessionOption", (New-CimSessionOption -Protocol DCOM))
}
Try {
Write-Verbose "[$((Get-Date).TimeofDay)] $computername"
$Cimsession = New-CimSession @sessParams
$tempsession = $True
}
catch {
Write-Error $_
#bail out
return
}
}
if ($Cimsession) {
$hash = [ordered]@{
PSTypename = "QuickStatus"
Computername = $cimsession.computername.toUpper()
}
Try {
$cimParams.classname = 'Win32_OperatingSystem'
$cimParams.CimSession = $Cimsession
Write-Verbose "[$((Get-Date).TimeofDay)] Using class $($cimparams.classname)"
$OS = Get-CimInstance @cimParams
$uptime = (Get-Date) - $OS.lastBootUpTime
$hash.Add("Uptime", $uptime)
$pctFreeMem = [math]::Round(($os.FreePhysicalMemory / $os.TotalVisibleMemorySize) * 100, 2)
if ($AsInt) {
$pctFreeMem = $pctFreeMem -as [int]
}
$hash.Add("PctFreeMem", $pctFreeMem)
$cimParams.classname = 'Win32_Logicaldisk'
$cimParams.filter = "deviceid='C:'"
Write-Verbose "[$((Get-Date).TimeofDay)] Using class $($cimparams.classname)"
Get-CimInstance @cimParams | ForEach-Object {
$name = "PctFree{0}" -f $_.deviceid.substring(0, 1)
$pctFree = [math]::Round(($_.FreeSpace / $_.size) * 100, 2)
if ($AsInt) {
$pctFree = $pctFree -as [int]
}
$hash.add($name, $pctfree)
}
New-Object PSObject -Property $hash
}
catch {
Write-Error $_
}
#only remove the cimsession if it was created in this function
if ($tempsession) {
Write-Verbose "[$((Get-Date).TimeofDay)] Removing temporary cimsession"
Remove-CimSession -CimSession $Cimsession
}
} #if cimsession
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay)] Ending $($myinvocation.mycommand)"
} #end
} #close function
Update-FormatData $PSScriptRoot\quickstatus.format.ps1xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Format type data generated 01/28/2021 17:37:17 by PROSPERO\Jeff
This file was created using the New-PSFormatXML command that is part
of the PSScriptTools module.
https://github.com/jdhitsolutions/PSScriptTools
-->
<Configuration>
<ViewDefinitions>
<View>
<!--Created 01/28/2021 17:37:17 by PROSPERO\Jeff-->
<Name>default</Name>
<ViewSelectedBy>
<TypeName>QuickStatus</TypeName>
</ViewSelectedBy>
<TableControl>
<!--Delete the AutoSize node if you want to use the defined widths.-->
<AutoSize />
<TableHeaders>
<TableColumnHeader>
<Label>Computername</Label>
<Width>15</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Uptime</Label>
<Width>21</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>%FreeMem</Label>
<Width>13</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>PctFreeC</Label>
<Width>11</Width>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<!--
By default the entries use property names, but you can replace them with scriptblocks.
<ScriptBlock>$_.foo /1mb -as [int]</ScriptBlock>
-->
<TableColumnItem>
<ScriptBlock>
if ($host.name -match "console|code") {
"$($psstyle.foreground.brightblue+$psstyle.reverse)$($_.Computername)$($psstyle.reset)"
}
else {
$_.Computername
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>"{0:dd\.hh\:mm\:ss}" -f $_.uptime</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
if ($host.name -match "console|code") {
if ($_.pctFreemem -le 25) {
"$($psstyle.foreground.brightred)$($_.pctFreemem)$($psstyle.reset)"
}
elseif ($_.pctFreemem -le 50) {
"$($psstyle.foreground.brightyellow)$($_.pctFreemem)$($psstyle.reset)"
}
else {
$_.pctFreeMem
}
}
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>PctFreeC</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment