Skip to content

Instantly share code, notes, and snippets.

@theonlyway
Created August 9, 2016 05:06
Show Gist options
  • Save theonlyway/9aa386d2b4f658db562e54971a0d9d05 to your computer and use it in GitHub Desktop.
Save theonlyway/9aa386d2b4f658db562e54971a0d9d05 to your computer and use it in GitHub Desktop.
Function Get-LoggedOnUsers {
<#
.Synopsis
Queries a computer to check for interactive sessions
.DESCRIPTION
This script takes the output from the quser program and parses this to PowerShell objects
.NOTES
Name: Get-LoggedOnUser
Author: Jaap Brasser
Version: 1.2.1
DateUpdated: 2015-09-23
.LINK
http://www.jaapbrasser.com
.PARAMETER ComputerName
The string or array of string for which a query will be executed
.EXAMPLE
.\Get-LoggedOnUser.ps1 -ComputerName server01,server02
Description:
Will display the session information on server01 and server02
.EXAMPLE
'server01','server02' | .\Get-LoggedOnUser.ps1
Description:
Will display the session information on server01 and server02
#>
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string[]]$ServerName = 'testserver1'
)
begin {
$ErrorActionPreference = 'Stop'
}
process {
foreach ($Computer in $ServerName) {
try {
quser /server:$Computer 2>&1 | Select-Object -Skip 1 | ForEach-Object {
$CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
$HashProps = @{
UserName = $CurrentLine[0]
ServerName = $Computer
}
# If session is disconnected different fields will be selected
if ($CurrentLine[2] -eq 'Disc') {
$HashProps.SessionName = $null
$HashProps.Id = $CurrentLine[1]
$HashProps.State = $CurrentLine[2]
$HashProps.IdleTime = $CurrentLine[3]
$HashProps.LogonTime = $CurrentLine[4..6] -join ' '
$HashProps.LogonTime = $CurrentLine[4..($CurrentLine.GetUpperBound(0))] -join ' '
} else {
$HashProps.SessionName = $CurrentLine[1]
$HashProps.Id = $CurrentLine[2]
$HashProps.State = $CurrentLine[3]
$HashProps.IdleTime = $CurrentLine[4]
$HashProps.LogonTime = $CurrentLine[5..($CurrentLine.GetUpperBound(0))] -join ' '
}
New-Object -TypeName PSCustomObject -Property $HashProps |
Select-Object -Property UserName,ServerName,SessionName,Id,State,IdleTime,LogonTime,Error
}
} catch {
New-Object -TypeName PSCustomObject -Property @{
ServerName = $Computer
Error = $_.Exception.Message
} | Select-Object -Property UserName,ServerName,SessionName,Id,State,IdleTime,LogonTime,Error
}
}
}
}
$results = Get-LoggedOnUsers -ServerName TestServer1
$html = $results | convertto-html
$style = "<style>"
$style = $style + "table{border-collapse:collapse;border-spacing:0}"
$style = $style + "th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;text-align:center}"
$style = $style + "td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;text-align:center}"
$style = $style + "</style>"
$htmlcode = @"
$style
<p>The following user(s) currently have sessions open<br></p>
$html
"@
$date = Get-Date -Format "yyyy-MM-dd"
Send-MailMessage -To "ENTEREMAILADDRESSHERE" -From "ENTEREMAILADDRESSHERE" -Body $htmlcode -SmtpServer "ENTERSMTPSERVERHERE" -Subject "[Logged on users] $date" -BodyAsHtml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment