Skip to content

Instantly share code, notes, and snippets.

@hagatorn
Created May 18, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagatorn/14937f183d68947383551e5e060f3a4e to your computer and use it in GitHub Desktop.
Save hagatorn/14937f183d68947383551e5e060f3a4e to your computer and use it in GitHub Desktop.
param(
[CmdletBinding(DefaultParameterSetName="OU")]
[Parameter(
ParameterSetName="Computer",
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName,
[Parameter(ParameterSetName="OU")]
[string]$SearchBase = "OU=Target,OU=Stations,DC=hagatorn,DC=local",
[Parameter(ParameterSetName="Computer")]
[Parameter(ParameterSetName="OU")]
[string]$Idletime = 1440, #1 Day
[Parameter(ParameterSetName="Computer")]
[Parameter(ParameterSetName="OU")]
[switch]$disconnected = $true,
[Parameter(ParameterSetName="Computer")]
[Parameter(ParameterSetName="OU")]
[switch]$idle = $true,
[Parameter(ParameterSetName="OU")]
[switch]$All
)
Write-verbose ("Param Set: "+($psCmdlet.ParameterSetName))
if($psCmdlet.ParameterSetName -eq "OU"){
$targetComputers = Get-ADComputer -filter '*' -SearchBase $SearchBase
}
else{
$targetComputers = New-Object psobject -Property @{Name = $ComputerName}
}
$command = {
param(
[CmdletBinding()]
[string]$Idletime,
[switch]$disconnected,
[switch]$idle
)
function Get-LoggedOnUser{
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = 'localhost'
)
begin {
$ErrorActionPreference = 'Stop'
}
process {
foreach ($Computer in $ComputerName) {
try {
quser /server:$Computer 2>&1 | Select-Object -Skip 1 | ForEach-Object {
$CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
$HashProps = @{
UserName = $CurrentLine[0]
ComputerName = $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,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
}
} catch {
New-Object -TypeName PSCustomObject -Property @{
ComputerName = $Computer
Error = $_.Exception.Message
} | Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
}
}
}
}
function Parse-Time{
param(
[CmdletBinding()]
[ValidatePattern('^(((?<days>[0-9]+)\+)?((?<hours>[0-9]{1,2}):))?(?<mins>[0-9]{1,2})$')]
[string]$quserTimeString
)
$quserTimeString -match '^(((?<days>[0-9]+)\+)?((?<hours>[0-9]{1,2}):))?(?<mins>[0-9]{1,2})$' | Out-Null
[Timespan]::new($Matches.days, $Matches.hours, $Matches.mins, 0)
}
$idlemins = @{Name = "TotalIdleMinutes" ; Expression = {(Parse-Time $_.IdleTime).TotalMinutes} }
try{
Get-LoggedOnUser |
?{$_.Error -ne "No User exists for *"} |
select *, $idlemins |
%{ if( $_.State -eq "Disc"){
Write-Output ($env:computername+": "+$_.Username+" disconnected and idle for "+$_.TotalIdleMinutes+" Mins")
if($disconnected){Write-Output ( $_.id+" | % {Logoff `$_}")}
}elseif($_.IdleTime -ne "none" -and $_.TotalIdleMinutes -gt $Idletime)
{
Write-Output ($env:computername+": "+$_.Username+" idle for "+$_.TotalIdleMinutes+" Mins")
if($idle){ Write-Output ( $_.id+" | % {Logoff `$_}")}
}
else{
Write-Output ($env:computername+": "+$_.Username+" is active")
}
}
}
catch [System.Management.Automation.RuntimeException]{
Write-warning "Error Executing Get-LoggedOnUser on $env:computername"
$_
}
}
$targetComputers.name |
%{
try{
invoke-command -computer $_ -ErrorAction Stop -command $command -ArgumentList $Idletime,$disconnected,$idle
}
catch [System.Management.Automation.RuntimeException]{
Write-warning ("Error Connecting to "+($_.TargetObject))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment