Created
August 30, 2022 12:19
-
-
Save pashalvov/569e257200f1c5932f159fb9ad8f27af to your computer and use it in GitHub Desktop.
Logoff Users Idle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clear-Host | |
$LogFileName = (Join-Path $PSScriptRoot ("\logs\" + (Get-Date).ToString("yyyy-MM-dd_HH-mm-ss") + '_LogOff.log')) | |
function Log | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
#[OutputType([String])] | |
Param | |
( | |
# Уровень лога | |
[Parameter(Mandatory=$false, | |
Position=1)] | |
[ValidateSet("ERR", "INF", "WARN", "DEBUG")] | |
$LogLevel, | |
[Parameter(Mandatory=$true, | |
Position=0)] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
$InputObject | |
) | |
Begin | |
{ | |
if ($LogLevel -like '') {$LogLevel = 'INF'} | |
} | |
Process | |
{ | |
$LogString = (Get-Date -Format 'dd.MM.yyyy HH:mm:ss') + " | " + $LogLevel + " | " + $InputObject | |
$LogString | Out-File -FilePath $LogFileName -Encoding default -Append | |
} | |
End | |
{ | |
} | |
} | |
[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
$DataOutput = (((quser) -replace '^>', '') -replace '\s{2,}', ',').Trim() | ForEach-Object { | |
if ($_.Split(',').Count -eq 5) { | |
Write-Output ($_ -replace '(^[^,]+)', '$1,') | |
} else { | |
Write-Output $_ | |
} | |
} | ConvertFrom-Csv | |
$Results = @() | |
foreach ($do in $DataOutput) | |
{ | |
$IdleTime = [timespan]::Zero | |
if ($do.'IDLE TIME' | Select-String "+" -SimpleMatch) | |
{ | |
[timespan]$TimeSpan = $do.'IDLE TIME'.Split("+")[1] | |
$IdleTime = ($TimeSpan.Add([timespan]::FromDays($do.'IDLE TIME'.Split("+")[0]))) | |
} | |
else | |
{ | |
try | |
{ | |
if ($do.'IDLE TIME'.Length -le 2) | |
{ | |
$TimeSpan = [timespan]::ParseExact($do.'IDLE TIME',"mm",[CultureInfo]::CurrentCulture) | |
} | |
else | |
{ | |
$TimeSpan = [timespan]$do.'IDLE TIME' | |
} | |
$IdleTime = $TimeSpan | |
} | |
catch | |
{ | |
} | |
} | |
$DateTimeTemp = [datetime]::ParseExact($do.'LOGON TIME',"dd.MM.yyyy H:mm",[Globalization.CultureInfo]::CreateSpecificCulture('ru-RU')) | |
$HashTable = [ordered]@{ | |
'username' = $do.USERNAME | |
'sessionname' = $do.SESSIONNAME | |
'id' = $do.ID | |
'state' = $do.STATE | |
'idletime' = $IdleTime | |
'logontime' = Get-Date $DateTimeTemp | |
'LogOff' = if (($IdleTime.TotalHours -gt 3) -and ($do.STATE -notlike 'Active')) {$true} else {$false} | |
} | |
$Results += New-Object -TypeName PSObject -Property $HashTable | |
} | |
foreach ($r in $Results) | |
{ | |
if ($r.LogOff) | |
{ | |
Log ("Пользователь: " + $r.username + ", ID: " + $r.id + ", со статусом: " + $r.state + ", простоем " + $r.idletime + " будет выбит из системы") | |
$LogOffId = $r.id | |
$ExitCode = Start-Process C:\Windows\system32\logoff.exe -ArgumentList "$LogOffId" -Wait -WindowStyle Hidden -PassThru | |
if ($ExitCode.ExitCode -eq 0) | |
{ | |
Log ("Выполнил команду с кодом выхода: " + $ExitCode.ExitCode) | |
} | |
else | |
{ | |
Log ("Ошибка! Код выхода: " + $ExitCode.ExitCode) -LogLevel ERR | |
} | |
} | |
else | |
{ | |
Log ("Выгружать юзверя: " + $r.username + ' не будем') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment