Skip to content

Instantly share code, notes, and snippets.

@theagreeablecow
Last active April 30, 2020 14:37
Show Gist options
  • Save theagreeablecow/5890562 to your computer and use it in GitHub Desktop.
Save theagreeablecow/5890562 to your computer and use it in GitHub Desktop.
SAMReport Module for PC Health
############################################################################################################
# Module Variables - PC Health #
#----------------------------------#
# Customise report Variables
$EmailTo = $EmailTo
$EmailSubject = $EmailSubject
$ReportTitle = $ReportTitle
$ReportSubTitle = $ReportSubTitle
# Load required plug-ins
import-module ActiveDirectory
# Load server and array information
$Computers = Get-ADComputer -SearchBase “OU=COMPUTERS,DC=mydomain,DC=com,DC=au” -Filter * | Select-Object –ExpandProperty Name
$Repositories = @("\\userfiles\home$\")
<#
$OU = "OU=MyServers,DC=mydomain,DC=com,DC=au"
$Servers = Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -SearchBase $OU | Select-Object –ExpandProperty Name
$Servers = @("Server1","Server2","Server3")
$Servers = Get-Content .\<path>\servers.txt
$Servers = ($env:COMPUTERNAME)
#>
#Miscellaneous
############################################################################################################
# Info #
#------------------------
$Title = "PC Uptime"
$Comment = "List of active computers that have not been restarted for specified periods"
$Author = "The Agreeable Cow"
$PluginDate = "6/3/2013"
$Version = "v1.0"
# 1.0 06/03/2013 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
#Create an Array and run query
$ResultsData = @()
$WarnDays = 14
$AlertDays = 21
$WarningCount = $NULL
$AlertCount = $NULL
$TriggerEmail = "Y" #Sends and email to logged in user if uptime is greater than $TriggerEmailDays (asking them to restart and warns that PC may soon be restarted automatically).
$TriggerEmailDays = 14
$TriggerRestart = "Y" #Restarts PC if uptime is greater than $IdleRestartDays (and no one is logged in), or greater than $ForceRestartDays (even if someone is logged in).
$IdleRestartDays = 14
$ForceRestartDays = 28
$RestartCount = $NULL
$now = Get-Date
foreach ($computer in $computers) {
if (Test-Connection -computername $computer -count 1 -quiet){
#Last Reboot
$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem" -ErrorAction SilentlyContinue
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d =$uptime.days
$h =$uptime.hours
#IP Address
$IPQuery = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -computer $computer -Filter "IpEnabled = True" -ErrorAction SilentlyContinue
$IPAddress = $IPQuery.IPAddress[0]
if ($d -ge $WarnDays){
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Computer" -value $computer
$obj | Add-Member -MemberType NoteProperty -name "IP Address" -value $IPAddress
$obj | Add-Member -MemberType NoteProperty -name "Last Reboot" -value $boottime
$obj | Add-Member -MemberType NoteProperty -name "Uptime" -value "$d days $h hours"
#Current Logged in User
$Session = Get-WmiObject -Class win32_process -computer $computer -Filter "name='explorer.exe'" | Foreach-Object {$_.GetOwner()}
if($Session.count -gt 1){
$SessionUser = $Session[0].user
$Others = $Session[1].user
$Extra = " (+$Others)"
}
else{
$SessionUser = $Session.user
$Extra = ""
}
if ($Session -ne $null){
$ADUser = Get-ADUser $SessionUser -Properties "physicalDeliveryOfficeName","DisplayName","Title","Mail" | select-object physicalDeliveryOfficeName,DisplayName,Title,Mail
$UserDisplay = $ADUser.DisplayName + $Extra
$obj | Add-Member -MemberType NoteProperty -name "Current User" -value $UserDisplay
$obj | Add-Member -MemberType NoteProperty -name "Office" -value $ADUser.physicalDeliveryOfficeName
$obj | Add-Member -MemberType NoteProperty -name "Title" -value $ADUser.Title
}
elseif ($Session -eq $null){
$obj | Add-Member -MemberType NoteProperty -name "Current User" -value "(None)"
$obj | Add-Member -MemberType NoteProperty -name "Office" -value ""
$obj | Add-Member -MemberType NoteProperty -name "Title" -value ""
}
# Restart Computer
$RestartedFlag = $NULL
if ($TriggerRestart -eq "Y"){
if ($d -ge $ForceRestartDays){
Restart-Computer -ComputerName $computer -Force
$obj | Add-Member -MemberType NoteProperty -name "Restarted" -value "Yes"
$RestartCount += $RestartCount.count + 1
$RestartedFlag = "Y"
}
elseif ($d -ge $IdleRestartDays){
if ($Session -eq $null){
Restart-Computer -ComputerName $computer -Force
$obj | Add-Member -MemberType NoteProperty -name "Restarted" -value "Yes"
$RestartCount += $RestartCount.count + 1
$RestartedFlag = "Y"
}
}
}
if ($RestartCount -gt 1){
$RestartText = "$RestartCount computers have been restarted by this script. </br>"
}
elseif ($RestartCount -eq 1){
$RestartText = "$RestartCount computer was restarted by this script. </br>"
}
else{
$RestartText = "No computers were restarted by this script. </br>"
}
#Email Users
if ($TriggerEmail -eq "Y"){
if($Session -ne $null){
if ($RestartedFlag -eq "Y"){
$obj | Add-Member -MemberType NoteProperty -name "Emailed" -value "Restarted Notice"
$MailSubject = "[AUTO] PC Health Report - Computer was restarted"
$MailBody = "An automated report identified that a computer you were logged into, had not been restarted for a long period of time.
User: $UserDisplay
Computer: $computer
Boot Date: $boottime
Up-Time: $d days $h hours
----------------------------------------------------------------------------------------------
NB. The Computer was automatically restarted over the weekend because Up-Time exceeded $ForceRestartDays days!
----------------------------------------------------------------------------------------------
Please log off regularly and occasionally restart your PC, to ensure it receives updates and patches.
This is important to keep your PC running smoothly and safely.
Please contact Helpdesk if you need more information.
Regards
Admin Scripts"
Send-MailMessage -To $ADUser.Mail -From "[email protected]" -Subject $MailSubject -SmtpServer $exchangeserver -body $MailBody
}
else{
$obj | Add-Member -MemberType NoteProperty -name "Emailed" -value "Warning Notice"
$MailSubject = "[AUTO] PC Health Report - Computer has not been restarted for $d days"
$MailBody = "An automated report has identified that a computer you are logged into, has not been restarted for a long period of time.
Current User: $UserDisplay
Computer: $computer
Boot Date: $boottime
Up-Time: $d days $h hours
----------------------------------------------------------------------------------------------
NB. Computers will be automatically restarted over the weekend once Up-Time exceeds $ForceRestartDays days!
----------------------------------------------------------------------------------------------
Please log off regularly and occasionally restart your PC, to ensure it receives updates and patches.
This is important to keep your PC running smoothly and safely.
Please contact Helpdesk if you need more information.
Regards
Admin Scripts"
Send-MailMessage -To $ADUser.Mail -From "[email protected]" -Subject $MailSubject -SmtpServer $exchangeserver -body $MailBody
}
}
}
# Update Text and Alert count
if ($d -ge $AlertDays){
$ResultsData += $obj
$AlertCount += $AlertCount.count + 1
if ($AlertCount -gt 1){
$AlertText = "!RED!Alert: $AlertCount PC's were not restarted for more than $AlertDays days.</br>"
}
else{
$AlertText = "!RED!Alert: $AlertCount PC was not restarted for more than $AlertDays days. </br>"
}
}
elseif ($d -ge $WarnDays){
$ResultsData += $obj
$WarningCount += $WarningCount.count + 1
if ($WarningCount -gt 1){
$WarningText = "Warning: $WarningCount PC's were not restarted for more than $WarnDays days. </br>"
}
else{
$WarningText = "Warning: $WarningCount PC was not restarted for more than $WarnDays days. </br>"
}
}
}
}
}
# Results Text
if ($AlertText -ne $null -or $WarningText -ne $null){
$ResultsText = $AlertText + $WarningText + $RestartText
}
else{
$ResultsText = "All computers have been restarted recently"
}
# Results Data
$ResultsData = $ResultsData | sort -Property "Last Reboot"
# Results Alert
if ($AlertCount -ge 1){
$ResultsAlert = "Alert"
}
elseif ($WarningCount -ge 1){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "User Logon Duration"
$Comment = "List of users who have not logged off for specified periods"
$Author = "The Agreeable Cow"
$PluginDate = "6/3/2013"
$Version = "v1.0"
# 1.0 06/03/2013 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
#Create an Array and run query
$ResultsData = @()
$WarnDays = 7
$AlertDays = 14
$WarningCount = $NULL
$AlertCount = $NULL
$now = Get-Date
foreach ($computer in $computers) {
if (Test-Connection -computername $computer -count 1 -quiet){
#Check for active session and lookup last user logon
$Session = Get-WmiObject -Class win32_process -computer $computer -Filter "name='explorer.exe'" -ErrorAction SilentlyContinue
if ($Session -ne $NULL){
#Session Owner
$SessionOwner = Get-WmiObject -Class win32_process -computer $computer -Filter "name='explorer.exe'" | Foreach-Object {$_.GetOwner()}
if($SessionOwner.count -gt 1){
$SessionUser = $SessionOwner[0].user
$Others = $Session[1].user
$Extra = " (+$Others)"
}
else{
$SessionUser = $SessionOwner.user
$Extra = ""
}
#Local Logged in User
$LocalUser = (Get-WmiObject -Class Win32_ComputerSystem -computer $computer).username
If ($LocalUser -ne $NULL){
$SessionType = "Local"
}
else{
$SessionType = "Remote"
}
#Start Time
$SessionStart = $Session.ConvertToDateTime($Session.creationdate)
$Logontime = $now - $SessionStart
$d =$Logontime.days
$h =$Logontime.hours
#IP Address
$IPQuery = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -computer $computer -Filter "IpEnabled = True" -ErrorAction SilentlyContinue
$IPAddress = $IPQuery.IPAddress[0]
if ($d -ge $WarnDays){
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Computer" -value $computer
$obj | Add-Member -MemberType NoteProperty -name "IP Address" -value $IPAddress
$obj | Add-Member -MemberType NoteProperty -name "Session" -value $SessionType
$obj | Add-Member -MemberType NoteProperty -name "Logged On" -value $SessionStart
$obj | Add-Member -MemberType NoteProperty -name "Duration" -value "$d days $h hours"
$ADUser = Get-ADUser $SessionUser -Properties "physicalDeliveryOfficeName","DisplayName","Title" | select-object physicalDeliveryOfficeName,DisplayName,Title
$UserDisplay = $ADUser.DisplayName + $Extra
$obj | Add-Member -MemberType NoteProperty -name "Current User" -value $UserDisplay
$obj | Add-Member -MemberType NoteProperty -name "Office" -value $ADUser.physicalDeliveryOfficeName
$obj | Add-Member -MemberType NoteProperty -name "Title" -value $ADUser.Title
$ResultsData += $obj
}
# Update Text and Alert count
if ($d -ge $AlertDays){
$AlertCount += $AlertCount.count + 1
if ($AlertCount -gt 1){
$AlertText = "!RED!Alert: $AlertCount User's have not logged off for more than $AlertDays days. </br>"
}
else{
$AlertText = "!RED!Alert: $AlertCount User has not logged off for more than $AlertDays days. </br>"
}
}
elseif ($d -ge $WarnDays){
$WarningCount += $WarningCount.count + 1
$WarningTotal = $WarningCount - $AlertCount
if ($WarningCount -gt 1){
$WarningText = "Warning: $WarningCount User's have not logged off for more than $WarnDays days. </br>"
}
else{
$WarningText = "Warning: $WarningCount User has not logged off for more than $WarnDays days. </br>"
}
}
}
}
}
# Results Text
if ($AlertText -ne $null -or $WarningText -ne $null){
$ResultsText = $AlertText + $WarningText
}
else{
$ResultsText = "All users have logged off recently"
}
# Results Data
$ResultsData = $ResultsData | sort -Property "Logged On"
# Results Alert
if ($AlertCount -ge 1){
$ResultsAlert = "Alert"
}
elseif ($WarningCount -ge 1){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
############################################################################################################
# Info #
#------------------------
$Title = "User Profiles"
$Comment = "List of users with excessive profile size"
$Author = "The Agreeable Cow"
$PluginDate = "07/03/2013"
$Version = "v1.0"
# 1.0 07/03/2013 The Agreeable Cow Original Build
############################################################################################################
# Main Script #
#------------------------
#Create an Array and run query
$ResultsData = @()
$WarnSize = 250 #MB
$AlertSize = 500 #MB
foreach ($Path in $Repositories){
$Profile = "$Path\profile.v2"
$Folders = Get-ChildItem $Profile -force | Where-Object {$_.PSIsContainer}
foreach ($Folder in $Folders){
$Size = (Get-DirectoryStats $Folder.Fullname).Sum/1MB
if ($Size -ge $WarnSize){
$RoundedSize = [Math]::Round([Decimal]$Size,2)
$Items = (Get-DirectoryStats $Folder.Fullname).Count
$obj = New-Object PSobject
$obj | Add-Member -MemberType NoteProperty -name "Name" -value $Folder.Name
$obj | Add-Member -MemberType NoteProperty -name "Path" -value "<a href='$Path\$Folder' target='_blank'>$Path\$Folder</a>"
$obj | Add-Member -MemberType NoteProperty -name "Size (MB)" -value $RoundedSize
$obj | Add-Member -MemberType NoteProperty -name "Items" -value $Items
$ResultsData += $obj
}
# Update Text and Alert count
if ($Size -ge $AlertSize){
$AlertCount += $AlertCount.count + 1
$AlertText = "!RED!Alert: $AlertCount large profiles found greater that $AlertSize MB</br>"
}
elseif ($Size -ge $WarnSize){
$WarningCount += $WarningCount.count + 1
$WarningText = "Warning: $WarningCount large profiles found greater than $WarnSize MB</br>"
}
}
}
# Results Text
if ($AlertText -ne $null -or $WarningText -ne $null){
$ResultsText = $AlertText + $WarningText
}
else{
$ResultsText = "All profile sizes are normal"
}
# Results Data
$ResultsData = $ResultsData | sort -descending -Property "Size (MB)"
# Results Alert
if ($AlertCount -ge 1){
$ResultsAlert = "Alert"
}
elseif ($WarningCount -ge 1){
$ResultsAlert = "Warning"
}
else{
$ResultsAlert = "Good"
}
############################################################################################################
# Output #
#------------------------
$OutText = $ResultsText # $OutText MUST be either $ResultsText or "" Valid $ResultsText is any text string
$OutData = $ResultsData # $OutData MUST be either $ResultsData or "" Valid $ResultsData is any data array
$OutAlert = $ResultsAlert # $OutAlert MUST be either $ResultsAlert or "" Valid $ResultsAlert are 'Good', 'Warning' or 'Alert'
$Attachment = "" # $Attachment MUST be either UNC path or ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment