I am writing a script that will filter the servers based on whether there are users logged in or not. If there is no user logged in, the script will export the name of the server to a text file. If there are users logged in, the script will go through the users and remove those that are either disconnected or have an idle time of greater than 60min.
I’ve completed the first filter, but I am unable to filter the idle time and pick out which users have idle time over 60min, I was able to filter those who have been logged in for more than a day, but I don’t think this helps.
I’ve included the relevant code below: Thank you.
$Servers = Get-Content 'H:maincomputernames.txt' $openservers =@() foreach ($Server in $Servers) { if (-not( Test-Connection $Server -Count 1 -Quiet )) { continue } if (-not( Convert-QueryToObjects $Server -ErrorAction SilentlyContinue)) { $openservers += $server $openservers | Out-File 'H:mainserveropenservers.txt' } else { Convert-QueryToObjects -Name $Server | Where-Object {@('Active','Disconnected') -contains $_.SessionState} | select @{Name='Server Name';Expression={$_.ComputerName}}, @{Name='Username'; Expression={$_.Username}}, @{Name='Session State'; Expression={$_.SessionState}}, @{Name='Idle Time'; Expression={$_.IdleTime}}, @{Name='ID'; Expression={$_.ID}} if((Convert-QueryToObjects -Name $Server|? {@('Disconnected') -contains $_.SessionState}) -or (Convert- QueryToObjects -Name $Server|Where-Object {[datetime]$_.LogonTime -le (Get-Date).AddDays(-1)})) { H:WindowsPowerShellGet-LoggedOnUser.ps1 - ComputerName $Server | Disconnect-LoggedOnUser -Verbose Write-Output "--------------------------------" } } }
submitted by /u/ruslive109
[link] [comments]
The post Disconnect users with Idle time greater than or equal to 1hr appeared first on How to Code .NET.