Monitor can now monitor printers

This commit is contained in:
Zachary Gorman 2024-07-11 14:12:06 -07:00
parent ac2f3fa6ca
commit 9a1230c004
2 changed files with 43 additions and 27 deletions

View file

@ -27,7 +27,7 @@ function Invoke-PCMonitor {
if ($notify) { if ($notify) {
$null = [System.Windows.MessageBox]::Show($msg) $null = [System.Windows.MessageBox]::Show($msg)
} }
Write-Host "$msg" Write-Host "$msg" -NoNewline
} }
} }
# Update date and time so user knows script hasn't frozen # Update date and time so user knows script hasn't frozen
@ -42,6 +42,7 @@ function Invoke-PCMonitor {
class WatchDog { class WatchDog {
[string]$Hostname [string]$Hostname
[System.Management.Automation.Runspaces.PSSession]$Session [System.Management.Automation.Runspaces.PSSession]$Session
[bool]$Online
[string]$LastUser [string]$LastUser
WatchDog() { $this.Init(@{})} WatchDog() { $this.Init(@{})}
WatchDog([hashtable]$Properties) { $this.Init($Properties)} WatchDog([hashtable]$Properties) { $this.Init($Properties)}
@ -54,7 +55,8 @@ class WatchDog {
[string] ToString() { [string] ToString() {
return ` return `
"Hostname: $($this.Hostname) `n" + "Hostname: $($this.Hostname) `n" +
"Status: $(if ($this.Session) { 'Online' } else { 'Offline' }) `n" + "Status: $(if ($this.Online) { 'Online' } else { 'Offline' }) `n" +
"Session: $(if ($this.Session) { 'True' } else { 'False' }) `n"
"User: $($this.LastUser) `n" "User: $($this.LastUser) `n"
} }
[WatchDog] Copy() { [WatchDog] Copy() {
@ -67,13 +69,18 @@ class WatchDog {
# Returns true if a value was updated # Returns true if a value was updated
[bool] Update() { [bool] Update() {
$old = $this.Copy() $old = $this.Copy()
if (-not (Test-Connection -ComputerName $this.Hostname -Count 1)) { if (-not (Test-Connection -ComputerName $this.Hostname -Count 3)) {
$this.Session = $null $this.Session = $null
return ($old.Session) $this.Online = $false
$this.LastUser = ""
return ($old.Online)
} else {
$this.Online = $true
} }
if (-not ($this.Session)) { if (-not ($this.Session)) {
$this.Session = New-PSSession -ComputerName $this.Hostname -SessionOption (New-PSSessionOption -NoMachineProfile) $this.Session = New-PSSession -ComputerName $this.Hostname -SessionOption (New-PSSessionOption -NoMachineProfile)
} }
if ($this.Session) {
$this.LastUser = $(Invoke-Command -Session $this.Session {Get-ComputerInfo}).CSUserName $this.LastUser = $(Invoke-Command -Session $this.Session {Get-ComputerInfo}).CSUserName
if($null -eq $this.LastUser){ if($null -eq $this.LastUser){
@ -82,34 +89,40 @@ class WatchDog {
if($null -ne $this.LastUser){ if($null -ne $this.LastUser){
$this.LastUser = "$($this.LastUser) (RDP/Inactive)" $this.LastUser = "$($this.LastUser) (RDP/Inactive)"
} }
else{
$this.LastUser = $null
} }
} }
return (($this.Session -ne $old.Session) -or ($this.LastUser -ne $old.LastUser)) if ($null -eq $this.LastUser) {
$this.LastUser = ""
}
return (($this.Online -ne $old.Online) -or ($this.Session -ne $old.Session) -or ($this.LastUser -ne $old.LastUser))
} }
# Returns a message describing any changes # Returns a message describing any changes
[string] ReportChange() { [string] ReportChange() {
$old = $this.Copy() $old = $this.Copy()
if (-not ($this.Update())) { return $null } if (-not ($this.Update())) { return $null }
$msg = ""
$date = Get-Date -Format "[MM/dd/yy HH:MM:ss]" $date = Get-Date -Format "[MM/dd/yy HH:MM:ss]"
if ($old.Session -ne $this.Session) { if ($old.Session -ne $this.Session) {
if ($old.Session -and $this.Session) { if ($old.Session -and $this.Session) {
return "$date $($this.Hostname) | Session re-established" $msg += "$date $($this.Hostname) | Session re-established`r`n"
} elseif ($this.Session) {
return "$date $($this.Hostname) | Online"
} else {
return "$date $($this.Hostname) | Offline"
} }
} elseif ($old.LastUser -ne $this.LastUser) { }
if ($old.Online -ne $this.Online) {
if ($this.Online) {
$msg += "$date $($this.Hostname) | Online`r`n"
} else {
$msg += "$date $($this.Hostname) | Offline`r`n"
}
}
if ($old.LastUser -ne $this.LastUser) {
if ($old.LastUser -and $this.LastUser) { if ($old.LastUser -and $this.LastUser) {
return "$date $($this.Hostname) | $($old.LastUser) logged off and $($this.LastUser) logged on" $msg += "$date $($this.Hostname) | $($old.LastUser) logged off and $($this.LastUser) logged on`r`n"
} elseif ($old.LastUser) { } elseif ($old.LastUser -ne "") {
return "$date $($this.Hostname) | $($old.LastUser) logged off" $msg += "$date $($this.Hostname) | $($old.LastUser) logged off`r`n"
} else { } elseif ($this.LastUser -ne "") {
return "$date $($this.Hostname) | $($this.LastUser) logged on" $msg += "$date $($this.Hostname) | $($this.LastUser) logged on`r`n"
} }
} }
return $null return $msg
} }
} }

View file

@ -1,3 +1,6 @@
Patch: 2024-07-11
-Update Monitor to detect when printers go online and offline
Patch: 2024-07-08 Patch: 2024-07-08
-Add PrinterPurge to purge a printer from all online computers -Add PrinterPurge to purge a printer from all online computers
-Fix Apps to work correctly with VMs (and not rely on out of scope variables) -Fix Apps to work correctly with VMs (and not rely on out of scope variables)