Get-PC/Private/PCMonitor.ps1

47 lines
1.5 KiB
PowerShell
Raw Normal View History

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))
}
}