Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active November 26, 2019 21:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/b1cd5a88e804ea8b1770702f64a40b3a to your computer and use it in GitHub Desktop.
Save jdhitsolutions/b1cd5a88e804ea8b1770702f64a40b3a to your computer and use it in GitHub Desktop.
An enhanced PowerShell prompt function that incorporates some of my other PowerShell modules to provide a daily management experience.
#requires -version 5.1
<#
The complete version of the function also requires additional modules
which can be downloaded from the PowerShell Gallery.
https://github.com/jdhitsolutions/PSCalendar
https://github.com/jdhitsolutions/myTickle
If you prefer to
skip some of the features in this function you'll need to remove those lines
and modify the following requires statements.
The function requires a windows platform.
#>
#requires -module PSCalendar
#requires -module myTickle
Function prompt {
$charHash = @{
Up = 0x25b2 -as [char]
Down = 0x25bc -as [char]
}
Try {
#verify there is a global hashtable variable
Get-Variable -Name rsHash -Scope global -ErrorAction Stop | Out-Null
}
Catch {
#create the runspace and synchronized hashtable
$global:rsHash = [hashtable]::Synchronized(@{Computername = $env:computername; results = ""; date = (Get-Date); computers = @()})
$newRunspace = [runspacefactory]::CreateRunspace()
#set apartment state if available
if ($newRunspace.ApartmentState) {
$newRunspace.ApartmentState = "STA"
}
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("rsHash", $rsHash)
$pscmd = [PowerShell]::Create().AddScript( {
#define scriptblock to run remotely
$sb = {
Try {
$mem = Get-CimInstance -ClassName win32_operatingsystem -Property TotalVisibleMemorySize, FreePhysicalMemory -ErrorAction Stop -OperationTimeoutSec 2
$memUsage = ($mem.FreePhysicalMemory / $mem.TotalVisibleMemorySize) * 100 -as [int]
$disk = Get-CimInstance -Classname win32_logicaldisk -filter "deviceID = 'c:'" -Property DeviceID, Size, FreeSpace -ErrorAction Stop -OperationTimeoutSec 2
$diskFree = ($disk.FreeSpace / $disk.Size) * 100 -as [int]
#get a count of all processes except idle and system and subtract 1 for the remote connection
$proc = (Get-Process).Where( {$_.name -notmatch 'Idle|system'}).count - 1
$os = Get-CimInstance -ClassName win32_operatingsystem -Property lastbootupTime
$isUp = $True
}
catch {
#this will probably never be used
$memUsage = 0
$diskFree = 0
$isUp = $False
Processes = 0
OS = ""
}
[pscustomobject]@{
Computername = $env:computername
MemoryUsage = $memUsage
DiskFree = $diskFree
Processes = $proc
IsUp = $IsUp
Uptime = ((Get-Date) - $os.LastBootUpTime).toString("d\.hh\:mm\:ss")
}
} #end scriptblock
#define a pssession option to speed up connections
$opt = New-PSSessionOption -OpenTimeout 1000 -MaxConnectionRetryCount 1
do {
$computers = $env:computername
<#
23 January 2019
This version of the prompt is only querying the local computer so I have
commented out code that you would use to get information from remote computers.
#define a pssession option to speed up connections.
$opt = New-PSSessionOption -OpenTimeout 1000 -MaxConnectionRetryCount 1
$computers | Where-Object {(get-pssession).where( {$_.state -eq 'opened'}).computername -notcontains $_} |
ForEach-Object {
#make sure there are sessions for all computers in the list
New-PSSession -ComputerName $_ -SessionOption $opt
}
#remove broken sessions
Get-PSSession | Where-Object {$_.state -eq 'broken'} | Remove-PSSession
#>
$global:rsHash.date = Get-Date
$global:rshash.computers = $computers
#uncomment the other parameters if querying remote computers
$results = Invoke-Command -ScriptBlock $sb #-HideComputerName -Session (Get-PSSession)
#set a sleep interval between tests
$global:rsHash.results = $results
Start-Sleep -Seconds 10
} While ($True)
}) # script
}) # script
$pscmd.runspace = $newrunspace
[void]$psCmd.BeginInvoke()
} #catch
#guess at a line length
#Note that this code is copied from other prompts that were displaying information
#from multiple computers. This version is only querying the local host but I'm
#leaving this since it doesn't break anything and you may want to go back to a version
#that queries other computers.
$names = ($global:rshash.computers | Measure-Object -Property length -sum).sum
#account for a : after each name + character + value
#you may have to tweak this value to get everything to line up
#and it might depend on whether you are using the console or ISE
$len = $names + (($global:rshash.computers.count) * 20) + 1
######### PSCALENDAR CODE #########
#display calendar
#check if y is greater than 3
if ($host.ui.RawUI.CursorPosition.y -le 10) {
$Y = $host.ui.RawUI.CursorPosition.Y
}
else {
$Y = $host.ui.RawUI.CursorPosition.Y - 3
}
$X = 75
Show-Calendar -position ([System.Management.Automation.Host.Coordinates]::new($X, $Y))
######### PSCALENDAR CODE #########
#display local computername and datetime
$dt = "{2} {0} {1} " -f (Get-Date).toshortdatestring(), (Get-Date).TolongTimeString(), $env:computername
Write-Host "`n " -NoNewline
Write-Host $dt -ForegroundColor black -BackgroundColor gray
######### TICKLE CODE #########
#get tickle events
$tickle = Get-TickleEvent -days 10
if ($tickle) {
#get length of longest event for padding purposes
$evtlen = ($tickle | sort-object {$_.event.length} -Descending)[0].event.length
foreach ($item in $tickle) {
#date time is formatted for North America
$evtString = " {0} {1:MM/dd/yyyy hh:mm:ss} [{2}]" -f $item.event.padright($evtlen), $item.date, $item.countdown.tostring("dd\.hh\:mm\:ss")
if ($item.countdown.totalhours -le 24) {
$fg = "red"
}
elseif ($item.countdown.TotalHours -le 48) {
$fg = "yellow"
}
else {
$fg = "green"
}
Write-Host $evtString -ForegroundColor $fg
}
}
######### TICKLE CODE #########
Write-Host "$([char]0x250c)" -NoNewline
Write-Host $(([char]0x2500).ToString() * $len ) -NoNewline
Write-Host $([char]0x2510)
Write-Host " " -NoNewline
if ($global:rsHash.results) {
#create a hashtable from the results
$h = $global:rshash.results | Group-Object -Property Computername -AsHashTable -AsString
#sort the results by computername
foreach ($item in $global:rshash.computers) {
if ($h[$item]) {
$obj = $h[$item]
}
else {
#create a temporary object for computer that is not running but in the list
$obj = [pscustomobject]@{
Computername = $item.ToUpper()
Uptime = (New-TimeSpan).toString()
DiskFree = "00"
MemoryUsage = "00"
Processes = "00"
IsUp = $false
}
}
if ($obj.DiskFree -le 20) {
$diskfg = "red"
}
elseif ($obj.DiskFree -le 75) {
$diskfg = "yellow"
}
else {
$diskfg = "green"
}
if ($obj.MemoryUsage -le 30) {
$memfg = "red"
}
elseif ($obj.MemoryUsage -le 60) {
$memfg = "yellow"
}
else {
$memfg = "green"
}
if ($obj.IsUp) {
Write-Host " $($charHash.Up)" -ForegroundColor green -NoNewline
$upfg = "green"
}
else {
Write-Host " $($charHash.down)" -ForegroundColor red -NoNewline
$upfg = "red"
}
Write-Host $obj.Uptime -ForegroundColor $upfg -NoNewline
Write-Host " $([char]0x058d)$($obj.diskfree)%" -NoNewline -ForegroundColor $diskfg
Write-Host " $([char]0x3bc)$($obj.MemoryUsage)%"-NoNewline -ForegroundColor $memfg
Write-Host " p$($obj.Processes)" -NoNewline
} #foreach
}
else {
Write-Host "Working..." -ForegroundColor yellow -NoNewline
}
Write-Host " "
Write-Host $([char]0x2514) -NoNewline
Write-Host $(([char]0x2500).ToString() * $len ) -NoNewline
Write-Host $([char]0x2518)
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
}
@jdhitsolutions
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment