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) {
$null = [System.Windows.MessageBox]::Show($msg)
}
Write-Host "$msg"
Write-Host "$msg" -NoNewline
}
}
# Update date and time so user knows script hasn't frozen
@ -42,6 +42,7 @@ function Invoke-PCMonitor {
class WatchDog {
[string]$Hostname
[System.Management.Automation.Runspaces.PSSession]$Session
[bool]$Online
[string]$LastUser
WatchDog() { $this.Init(@{})}
WatchDog([hashtable]$Properties) { $this.Init($Properties)}
@ -54,7 +55,8 @@ class WatchDog {
[string] ToString() {
return `
"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"
}
[WatchDog] Copy() {
@ -67,49 +69,60 @@ class WatchDog {
# Returns true if a value was updated
[bool] Update() {
$old = $this.Copy()
if (-not (Test-Connection -ComputerName $this.Hostname -Count 1)) {
if (-not (Test-Connection -ComputerName $this.Hostname -Count 3)) {
$this.Session = $null
return ($old.Session)
$this.Online = $false
$this.LastUser = ""
return ($old.Online)
} else {
$this.Online = $true
}
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){
if ($this.Session) {
$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
$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)"
}
}
}
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
[string] ReportChange() {
$old = $this.Copy()
if (-not ($this.Update())) { return $null }
$msg = ""
$date = Get-Date -Format "[MM/dd/yy HH:MM:ss]"
if ($old.Session -ne $this.Session) {
if ($old.Session -and $this.Session) {
return "$date $($this.Hostname) | Session re-established"
} elseif ($this.Session) {
return "$date $($this.Hostname) | Online"
$msg += "$date $($this.Hostname) | Session re-established`r`n"
}
}
if ($old.Online -ne $this.Online) {
if ($this.Online) {
$msg += "$date $($this.Hostname) | Online`r`n"
} else {
return "$date $($this.Hostname) | Offline"
}
} elseif ($old.LastUser -ne $this.LastUser) {
if ($old.LastUser -and $this.LastUser) {
return "$date $($this.Hostname) | $($old.LastUser) logged off and $($this.LastUser) logged on"
} elseif ($old.LastUser) {
return "$date $($this.Hostname) | $($old.LastUser) logged off"
} else {
return "$date $($this.Hostname) | $($this.LastUser) logged on"
$msg += "$date $($this.Hostname) | Offline`r`n"
}
}
return $null
if ($old.LastUser -ne $this.LastUser) {
if ($old.LastUser -and $this.LastUser) {
$msg += "$date $($this.Hostname) | $($old.LastUser) logged off and $($this.LastUser) logged on`r`n"
} elseif ($old.LastUser -ne "") {
$msg += "$date $($this.Hostname) | $($old.LastUser) logged off`r`n"
} elseif ($this.LastUser -ne "") {
$msg += "$date $($this.Hostname) | $($this.LastUser) logged on`r`n"
}
}
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
-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)