Get-ActiveSession.psm1

Function Get-PSCActiveSession{

    [CmdletBinding()]   
    Param
    (
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $Name


    )

    Begin
    {
    }
    Process
    {

        Try{
           [System.Collections.ArrayList]$fullList=@()
    $queryresult=query user /server:$($Name) 2> $Null
    if (!($queryresult)){Write-host "It Seems there was an issue for $($Name)`n Or there is no active session The Error is $($Error[0])" -ForegroundColor Red}
    Else{
            Foreach ($resultline in ($queryresult | Select-Object -Skip 1)){
                $Parsedline=$resultline.Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)
                $ComputerList=[PSCustomObject]@{Name=''
                                            Username=''
                                            SessionState=''
                                            SessionID=''
                                            }
                            switch ($resultline)
                            {
                                {$_ -like '*console*'}{
                                        $ComputerList.Name=$Name
                                        $ComputerList.SessionID=$Parsedline[2]
                                        $ComputerList.SessionState="Console"
                                        $ComputerList.Username=$Parsedline[0].Replace(">","")
                                       }
                                {$_ -like '*Disc*'}{
                                        $ComputerList.Name=$Name
                                        $ComputerList.SessionID=$Parsedline[1]
                                        $ComputerList.SessionState="Disconnected"
                                        $ComputerList.Username=$Parsedline[0]
                                        }
                                Default {
                                        $ComputerList.Name=$Name
                                        $ComputerList.SessionID=$Parsedline[2]
                                        $ComputerList.SessionState="Active"
                                        $ComputerList.Username=$Parsedline[0]
                                        }
                                
                        }
                        $fullList.Add($ComputerList) |Out-Null

      

            }
            
   

     }
    return $fullList
    }

    catch{
    Write-Host $_.excption.Message 
        }
    }


    }
Export-ModuleMember Get-PSCActiveSession

Function Start-PSCRemoteLogoff{
   [CmdletBinding(DefaultParameterSetName='AllUsers')]   
    Param
    (
        [Parameter(Mandatory=$True,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $Name,
        [Parameter(Mandatory=$false,ParameterSetName="SingleUser")]$TargetUser,
        [parameter(mandatory=$false,ParameterSetName="AllUsers")][switch]$LogoffAll
     

    )
    Begin{

    }

    Process{
    $ActiveSession=Get-PSCActiveSession -name $Name
    if (!( $ActiveSession)){Write-Host "Cannot logoff remote user, Please make sure the computer name and the username is correct" -ForegroundColor Red
                    Write-Host "The Username shouls not be assigned with any domain, just the username" -ForegroundColor Red
                    Write-Host "If the Username and password are correct, the there maybe no active session." -ForegroundColor Red
                    return
                    }
    if ($PSBoundParameters.ContainsKey("TargetUser")){       
            Write-Host "Logging Off $($TargetUser) from $($Name)"
            $UserToLogoff= $ActiveSession | Where-Object {$_.Username -like $TargetUser}
            if (!($UserToLogoff)){write-host "$($TargetUser) is not logged in" -ForegroundColor Yellow
                return}
            $LogoffStatus=logoff $UserToLogoff.SessionID /Server:$Name /V
            Write-Host $LogoffStatus -ForegroundColor Green
            
      }

    if ($PSBoundParameters.ContainsKey("LogoffAll"))
    {       
       Write-Host "Logging Off All Users from $($Name)"
        foreach($RemSession in $ActiveSession){
        $RemSession
        $LogoffAllStatus=logoff $RemSession.SessionID /Server:$Name /V
        Write-Host $LogoffAllStatus -ForegroundColor Green
        }
            
    }

    }

}

Export-ModuleMember Start-PSCRemoteLogoff