Addded Monitor to monitor a pc's status like online and login status.

This commit is contained in:
Zachary Gorman 2024-07-01 08:34:28 -07:00
parent e60cd96bc6
commit 2b0c9ed93c
4 changed files with 73 additions and 9 deletions

View file

@ -52,9 +52,11 @@ function Get-PCBatchInvoke {
#Adapter #Adapter
$adapter = ($win32_networkadapterconfiguration | Where-Object {$_.IpEnabled -Match "True"} | Select-Object -Expand Description) $adapter = ($win32_networkadapterconfiguration | Where-Object {$_.IpEnabled -Match "True"} | Select-Object -Expand Description)
<#
if($adapter -is [array]){ if($adapter -is [array]){
$adapter = $adapter[0] $adapter = $adapter[0]
} }
#>
#UserName #UserName
$Username = $PCInfo.CSUserName $Username = $PCInfo.CSUserName

View file

@ -1,15 +1,38 @@
function Invoke-Monitor { function Invoke-PCMonitor {
param( param(
[string[]] $comps [string[]] $comps
) )
# Ask to notify # Ask to notify
$notify = $(Read-Host "Notify with pop-up window? (y/N)") -match "^[yY]"
if ($notify) {
Add-Type -AssemblyName PresentationFramework
}
$sleepTime = $(Read-Host "Checkin interval in seconds (Default: 300 [5 min])")
if (-not ($sleepTime)) { $sleepTime = 300 }
$dogs = @()
# Initialize computer sessions # Initialize computer sessions
foreach ($comp in $comps) {
$dog = [WatchDog]::new($comp)
$null = $dog.Update()
$dogs += $dog
}
while ($true) {
foreach ($dog in $dogs) {
$msg = $dog.ReportChange()
if ($msg) {
if ($notify) {
$null = [System.Windows.MessageBox]::Show($msg)
}
Write-Host "$msg"
}
}
Start-Sleep $sleepTime
}
} }
class WatchDog { class WatchDog {
[string]$Hostname [string]$Hostname
[PSSession]$Session [System.Management.Automation.Runspaces.PSSession]$Session
[string]$LastUser [string]$LastUser
WatchDog() { $this.Init(@{})} WatchDog() { $this.Init(@{})}
WatchDog([hashtable]$Properties) { $this.Init($Properties)} WatchDog([hashtable]$Properties) { $this.Init($Properties)}
@ -19,12 +42,25 @@ class WatchDog {
$this.$prop = $Properties.$prop $this.$prop = $Properties.$prop
} }
} }
[string] ToString() {
return `
"Hostname: $($this.Hostname) `n" +
"Status: $(if ($this.Session) { 'Online' } else { 'Offline' }) `n" +
"User: $($this.LastUser) `n"
}
[WatchDog] Copy() {
$props = @{}
foreach ($prop in $this.PSObject.Properties) {
$props.$($prop.Name) = $prop.Value
}
return [WatchDog]$props
}
# Returns true if a value was updated # Returns true if a value was updated
[bool] Update() { [bool] Update() {
$old = $this $old = $this.Copy()
if ((Test-Connection -ComputerName $this.Hostnmae -Count 1)) { if (-not (Test-Connection -ComputerName $this.Hostname -Count 1)) {
$this.Session = $null $this.Session = $null
return -not ($old.Session) return ($old.Session)
} }
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)
@ -43,4 +79,28 @@ class WatchDog {
} }
return (($this.Session -ne $old.Session) -or ($this.LastUser -ne $old.LastUser)) return (($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 }
$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"
} 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"
}
}
return $null
}
} }

View file

@ -112,7 +112,6 @@
$SourceRoot = "\\$Computer\c$\Users\$User" $SourceRoot = "\\$Computer\c$\Users\$User"
$BackupDestination = "\\SHSCMGSRMC\ProfileBackup" $BackupDestination = "\\SHSCMGSRMC\ProfileBackup"
$Destination = "\\SHSCMGSRMC\ProfileBackup\$DestinationFileName" $Destination = "\\SHSCMGSRMC\ProfileBackup\$DestinationFileName"
$CopiedUserDesktop = Join-Path -Path $Destination -ChildPath "Desktop"
#region BACKUP #region BACKUP
if($ConfirmUser -eq 'y'){ if($ConfirmUser -eq 'y'){
@ -155,6 +154,7 @@
Write-Warning "Invalid input please restart script" Write-Warning "Invalid input please restart script"
return return
} }
#Creates a log file #Creates a log file
New-Item -Path $Destination\backuplog.txt -Force | Write-Verbose New-Item -Path $Destination\backuplog.txt -Force | Write-Verbose
$log = Join-Path -Path $Destination -ChildPath backuplog.txt $log = Join-Path -Path $Destination -ChildPath backuplog.txt
@ -171,6 +171,7 @@
#Cleans up Citrix Shortcuts #Cleans up Citrix Shortcuts
Write-Host "Removing Citrix Shortcuts from backup" Write-Host "Removing Citrix Shortcuts from backup"
$CopiedUserDesktop = Join-Path -Path $Destination -ChildPath "Desktop"
$Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk $Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk
foreach($Shortcut in $Shortcuts){ foreach($Shortcut in $Shortcuts){

View file

@ -4,7 +4,7 @@
#region Module Import Block #region Module Import Block
$ErrorActionPreference = 'SilentlyContinue' #$ErrorActionPreference = 'SilentlyContinue'
#DevStage can take either Dev or Prod as values #DevStage can take either Dev or Prod as values
$devStage = 'Dev' $devStage = 'Dev'
#Locations for dev build and prod build #Locations for dev build and prod build
@ -544,6 +544,7 @@ Function Get-PC {
if ($adTest) { if ($adTest) {
Write-Warning "$comp is either disabled or off the domain" Write-Warning "$comp is either disabled or off the domain"
} }
Write-Progress -Activity "Looking up computer in AD" -Completed
} }
#Direct SCCM Queries #Direct SCCM Queries