Demo ansehen×
×

Sehen Sie NinjaOne in Aktion!

Mit dem Absenden dieses Formulars akzeptiere ich die Datenschutzerklärung von NinjaOne.

Erkennen der Leerlaufzeit von Benutzern mit PowerShell

Die Erkennung von Leerlaufzeiten auf Computern ist ein wichtiger Aspekt für IT-Fachleute, insbesondere in Zeiten erhöhter Bedrohungen der Cyber-Sicherheit und der Ressourcenverwaltung. Angesichts der rasanten Digitalisierung von Unternehmen und der Tatsache, dass Benutzer oft über längere Zeiträume angemeldet bleiben, ist die Erkennung von Leerlaufzeiten aus Sicherheits- und Betriebsgründen von größter Bedeutung. In diesem Beitrag tauchen wir tief in ein PowerShell-Skript ein, das die längste Leerlaufzeit von Benutzern erkennt und diese Aufgabe nahtlos und effizient gestaltet.

Hintergrund

Die Möglichkeit, festzustellen, wie lange ein Benutzer inaktiv war, kann IT-Fachleuten und Managed Service Providern (MSPs) wertvolle Erkenntnisse liefern. Ob es nun darum geht, Ressourcen freizugeben, sicherzustellen, dass Sitzungen nicht für potenzielle Sicherheitsbedrohungen offen bleiben, oder sogar um Abrechnungserwägungen für Cloud-Service-Anbieter, eine effektive und zuverlässige Methode zur Bestimmung der Inaktivität von Benutzern ist unerlässlich. Dieses spezielle Skript nutzt die Fähigkeiten von PowerShell, um direkt mit dem Windows-Betriebssystem zu interagieren und relevante Daten zur Messung der Leerlaufzeiten abzurufen.

Das Skript

#Requires -Version 5.1

<#
.SYNOPSIS
    Returns the longest idle time of any user logged in or for a specific user.
.DESCRIPTION
    Returns the longest idle time of any user logged in or for a specific user.
    If RDS(Remote Desktop Services) is installed and the RSAT tools for it as well,
     then this will get the idle time of each logged in user.
    For workstations and servers(with out RDS installed),
     this will get the current idle of the currently logged in user.
    If a user is logged in via the console and another is via the admin RDP session,
     then both will be considered as one user for calculating idle time.
.EXAMPLE
    No parameters needed.
    Returns the longest idle time of all users logged in.
.EXAMPLE
     -UserName "Fred"
    Returns the longest idle time of the user Fred.
.EXAMPLE
    PS C:> Get-User-Idle-Time.ps1 -UserName "Fred"
    Returns the longest idle time of the user Fred.
.OUTPUTS
    PSCustomObject[]
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes:
    Adds functions to get idle time from RDS and non-RDS computers.
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
.COMPONENT
    ManageUsers
#>

[CmdletBinding()]
param (
    # Specify one user on a Terminal Services Server, else leave blank for normal servers and workstations
    [Parameter(Mandatory = $false)]
    $UserName
)

begin {
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
        { Write-Output $true }
        else
        { Write-Output $false }
    }
    Function Get-QueryUser() {
        Param()
        $Result = @()
        # Replaces all occurrences of 2 or more spaces in a row with a single comma
        $Lines = @(query.exe user).foreach({ $(($_) -replace ('s{2,}', ',')) })
        if ($Lines.Count -gt 1) {
            $Header = $($Lines[0].split(',').trim())
            for ($i = 1; $i -lt $($Lines.Count); $i++) {
                $Res = "" | Select-Object $Header
                $Line = $($Lines[$i].split(',')).foreach({ $_.trim().trim('>') })
                # Accounts for disconnected users
                if ($Line.count -eq 5) {
                    $Line = @($Line[0], "$($null)", $Line[1], $Line[2], $Line[3], $Line[4] )
                }
                for ($j = 0; $j -lt $($Line.count); $j++) {
                    $Res.$($Header[$j]) = $Line[$j]
                }
                $Result += $Res
                Remove-Variable Res
            }
            return $Result
        }
        else {
            return $null
        }
    }

    Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace GetLastUserInput
{
    public class GetLastUserInput
    {
        private struct LASTINPUTINFO
        {
            public uint cbSize;
            public uint dwTime;
        }
        private static LASTINPUTINFO lastInPutNfo;
        static GetLastUserInput()
        {
            lastInPutNfo = new LASTINPUTINFO();
            lastInPutNfo.cbSize = (uint)Marshal.SizeOf(lastInPutNfo);
        }
        [DllImport("User32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        /// <summary>
        /// Idle time in ticks
        /// </summary>
        /// <returns></returns>
        public static uint GetIdleTickCount()
        {
            return ((uint)Environment.TickCount - GetLastInputTime());
        }
        /// <summary>
        /// Last input time in ticks
        /// </summary>
        /// <returns></returns>
        public static uint GetLastInputTime()
        {
            if (!GetLastInputInfo(ref lastInPutNfo))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            return lastInPutNfo.dwTime;
        }
    }
}
"@
}

process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }
    if ($(Get-Module -Name "RemoteDesktop") -and $(Get-RDServer -ErrorAction SilentlyContinue)) {
        try {
            $Sessions = Get-RDUserSession
            $Sessions | Select-Object UserName, IdleTime
        }
        catch {
            Write-Warning -Message "A Remote Desktop Services deployment does not exist on $env:COMPUTERNAME."
        }
    }
    else {
        Write-Warning -Message "Remote Desktop Services is not installed on this computer, Falling back to query user."
        $Results = Get-QueryUser
        if ($null -eq $Results) {
            Write-Host "No user(s) logged in."
            exit 0
        }
        # Parse query results and loop through each user
        $Results | ForEach-Object {
            $CurrentUser = $_.USERNAME
            # If UserName param is used, only filter that user; If UserName param isn't used, return all users
            if ($CurrentUser -like $UserName -or ([string]::IsNullOrEmpty($UserName) -or [string]::IsNullOrWhiteSpace($UserName))) {
                # Output a PowerShell Custom Object array
                [PSCustomObject]@{
                    UserName    = $CurrentUser
                    SessionName = $_.SESSIONNAME
                    Id          = $_.ID
                    State       = $_.STATE
                    LogonTime   = $_.'LOGON TIME'
                    IdleTime    = if ($_.'IDLE TIME' -like 'none') { 0 }else { $_.'IDLE TIME' }
                }
            }
        } | Sort-Object -Property IdleTime | Select-Object -Property UserName, @{
            # Modify IdleTime when it shows none
            Label      = "IdleTime"
            Expression = {
                New-TimeSpan -Start $(Get-Date) -End $(Get-Date).AddMilliseconds([GetLastUserInput.GetLastUserInput]::GetIdleTickCount())
            }
        }
    }
}

end {}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

  • Cmdlet-Bindung & Parameter: Das Skript beginnt mit einem CmdletBinding, so dass es als Cmdlet verwendet werden kann. Er enthält einen optionalen Parameter für einen bestimmten Benutzernamen.
  • Innere Funktionen:
  • Test-IsElevated prüft, ob das Skript mit administrativen Rechten läuft.
  • Get-QueryUser arbeitet mit query.exe zusammen, um Details über die aktuellen Benutzer abzurufen.
  • Integration externer Bibliotheken: Eine externe Bibliothek (GetLastUserInput) wird verwendet, um die genaue Anzahl der Leerlaufticks abzurufen.
  • Prozess-Block: Hier liegt der Kern der Logik.
  • Zunächst wird geprüft, ob das Skript mit Administratorrechten ausgeführt wird.
  • Wenn die Remote-Desktop-Dienste (RDS) installiert sind, wird die Leerlaufzeit für jeden angemeldeten Benutzer abgefragt.
  • Wenn kein RDS vorhanden ist, wird auf query user zurückgegriffen und die Leerlaufzeit auf der Grundlage der letzten Benutzereingabe berechnet.

Potenzielle Anwendungsfälle

Stellen Sie sich vor, Sie sind IT-Administrator in einer mittelgroßen Organisation. Ihnen ist aufgefallen, dass viele Benutzer ihre Arbeitsplätze eingeschaltet und angemeldet lassen, auch nach den Bürozeiten. Dies stellt nicht nur ein Sicherheitsrisiko dar, sondern belastet auch die Netz- und Energieressourcen. Wenn Sie dieses Skript unternehmensweit einsetzen, können Sie schnell feststellen, welche Benutzer aktiv arbeiten und welche Arbeitsstationen im Leerlauf waren. Mit diesen Informationen können Sie Richtlinien für die automatische Abmeldung durchsetzen oder Erinnerungen an die Benutzer senden, damit diese ihre Rechner herunterfahren.

Vergleiche

Es gibt zwar Tools von Drittanbietern, die Leerlaufzeiten erkennen, aber sie sind oft überladen oder haben nicht die erforderliche Detailgenauigkeit. Manche entscheiden sich für die Abfrage des Windows-Ereignisprotokolls nach bestimmten Ereignissen, aber bei diesem Ansatz könnten Nuancen wie ein laufendes Video, das die Sitzung aktiv hält, übersehen werden. Dieses Skript bietet eine direkte und anpassbare Methode, die die nativen Windows-Tools und -Befehle nutzt.

FAQs

  • Ist RDS für dieses Skript erforderlich?
    Nein, es ist so konzipiert, dass es Leerlaufzeiten sowohl mit als auch ohne RDS erkennt.
  • Wie genau ist die Berechnung der Leerlaufzeit?
    Sie ist präzise auf die Anzahl der Systemticks seit der letzten Benutzereingabe.

Auswirkungen

Bei der Erkennung von Leerlaufzeiten geht es nicht nur um Ressourcenmanagement. Eine offene und inaktive Sitzung könnte eine potenzielle Schwachstelle darstellen. Böswillige Akteure können, sobald sie in das Netz eingedrungen sind, diese Sitzungen kapern. Daher sind die rechtzeitige Erkennung und das Session-Management für die IT-Sicherheit von entscheidender Bedeutung.

Empfehlungen

  • Führen Sie das Skript immer mit administrativen Rechten aus, um genaue Ergebnisse zu erzielen.
  • Aktualisieren und pflegen Sie das Skript regelmäßig, um es an Änderungen in der Windows-Umgebung anzupassen.
  • Integrieren Sie es mit Überwachungstools, um Echtzeitwarnungen bei längeren Leerlaufsitzungen zu erhalten.

Abschließende Überlegungen

Im Zusammenhang mit der Verwaltung ungenutzter Benutzersitzungen können Tools wie NinjaOne die Effektivität weiter steigern, indem sie eine einheitliche Plattform zur Überwachung, Warnung und Verwaltungbereitstellen. Ganz gleich, ob es um die Optimierung von Ressourcen oder die Verschärfung der Sicherheitsvorkehrungen geht, das Verständnis des Benutzerverhaltens während der Leerlaufzeiten kann eine entscheidende Rolle spielen. Der Einsatz von Skripten wie dem hier vorgestellten kann das Rückgrat dieses Unterfangens bilden, insbesondere in Kombination mit umfassenden IT-Management-Lösungen.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Kategorien:

Das könnte Sie auch interessieren

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).