The following script is supposed to got through each server in the server list and check if there are users logged in. If there aren't, it supposed to disable logon using PSExec (essentially place it in maintenance mode). If there are users logged in, it will go through each of there sessions and log out those that have an idle time of greater than an hour.

Please let me know if this code can be considered production ready. I've excluded the primary functions for log off and only included the script I was not sure about.

$content = "H:\demo\computernames.txt"  #please input where the list of servers is found (in a txt doc please)
$input = "H:\demo\session\run11.csv"  #please input where you want the csv to be exported (this includes all the active and disconnected servers)
$export ='H:\demo\session\openservers.txt' #please input where you want to 'open servers' (no users) to be exported.
#and that does it. cheers!

if (Test-Path $input) {   
    Clear-Content $input

$Servers = Get-Content "$content"
$openservers = @()
foreach ($Server in $Servers) {
    #tests the connection to the server (general test for the entire server)
    if (-not (Test-Connection $Server -Count 1 -Quiet)) { continue }
    #if their are no users logged in (hence the count of zero), push that server name onto a txt doc
    if (-not (Convert-QueryToObjects $Server -ErrorAction SilentlyContinue)) {
        $openservers += $server
        $openservers | Out-File $export #**TXT EXPORT**#
      #if there are users logged onto the server, make sure they are either in disc or active state and push all the session details onto a seperate csv
        else {  
                Convert-QueryToObjects -Name $Server | Where-Object {
                    @('Disconnected','Active') -contains $_.SessionState
                       }|Export-Csv $input -NoTypeInformation -Append 
                       # does the append so each time the content is kept (however, the issue of duplicate content arose (hence the fix above).
    } #concludes the first foreach loop
#<--- the folllowing function works with PsExec(external program)
    $openservers = Get-Content "$export" #**TXT IMPORT**# 
    #for each server in the server list, it will check to make sure its online (Again).
    foreach($openserver in $openservers){  
        if(  Test-Connection -Cn $openserver -Quiet) {
        H:\PsTools\PsExec.exe \\$openserver change logon /enable # calls an external tool and passes it the 
    else {
       Write-Host("the server>> $openserver is NOT online") -ForegroundColor DarkYellow

        Import-Csv $input | Where-Object { 
                ($_.SessionState -eq 'Disconnected') -or #if its disconnected or
                (($_.IdleTime -like "*:*") -and ($_.IdleTime -gt "00:59")) #idle time is over an hour, kindly do this:
                    } | ForEach-Object { #disconnect the session by using the server name and session ID
                             Disconnect-LoggedOnUser -ComputerName $_.ComputerName -Id $_.ID -Verbose 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.