From e60cd96bc6cec0c3b8360d65ad59a04c79888e79 Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Tue, 25 Jun 2024 10:17:17 -0700 Subject: [PATCH] Added PCMonitor.ps1 to fix last commit. Oopsie! --- Private/PCMonitor.ps1 | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Private/PCMonitor.ps1 diff --git a/Private/PCMonitor.ps1 b/Private/PCMonitor.ps1 new file mode 100644 index 0000000..adf0a5b --- /dev/null +++ b/Private/PCMonitor.ps1 @@ -0,0 +1,46 @@ +function Invoke-Monitor { + param( + [string[]] $comps + ) + # Ask to notify + # Initialize computer sessions + +} + +class WatchDog { + [string]$Hostname + [PSSession]$Session + [string]$LastUser + WatchDog() { $this.Init(@{})} + WatchDog([hashtable]$Properties) { $this.Init($Properties)} + WatchDog([string]$Hostname) { $this.Init(@{Hostname = $Hostname})} + [void] Init([hashtable]$Properties){ + foreach ($prop in $Properties.Keys) { + $this.$prop = $Properties.$prop + } + } + # Returns true if a value was updated + [bool] Update() { + $old = $this + if ((Test-Connection -ComputerName $this.Hostnmae -Count 1)) { + $this.Session = $null + return -not ($old.Session) + } + if (-not ($this.Session)) { + $this.Session = New-PSSession -ComputerName $this.Hostname -SessionOption (New-PSSessionOption -NoMachineProfile) + } + $this.LastUser = $(Invoke-Command -Session $this.Session {Get-ComputerInfo}).CSUserName + if($null -eq $this.LastUser){ + + $this.LastUser = (Invoke-Command -Session $this.Session -ScriptBlock {Get-Process Explorer -IncludeUsername | Where-Object { $_.Username -notlike "*SYSTEM" }} ).Username + + if($null -ne $this.LastUser){ + $this.LastUser = "$($this.LastUser) (RDP/Inactive)" + } + else{ + $this.LastUser = $null + } + } + return (($this.Session -ne $old.Session) -or ($this.LastUser -ne $old.LastUser)) + } +}