Aktivieren oder Deaktivieren des Remotedesktopprotokolls (RDP) auf Workstations mithilfe von PowerShell

Wichtigste Erkenntnisse

  • RDP ist ein wichtiges Tool für IT-Experten, das den Fernzugriff auf Computer ermöglicht.
  • Das bereitgestellte PowerShell-Skript bietet eine optimierte Methode zum Aktivieren oder Deaktivieren von RDP auf Arbeitsstationen.
  • Es werden hauptsächlich zwei Maßnahmen durchgeführt: Änderung der Registrierungseinstellungen und Anpassung der Firewall-Regeln.
  • Das Skript erfordert Administratorrechte und prüft, ob es sich bei dem Rechner um eine Workstation handelt.
  • RDP bietet zwar Komfort, kann aber bei falscher Konfiguration auch Sicherheitsrisiken bergen.
  • Für eine sichere RDP-Nutzung werden die Überwachung und die Verwendung starker Authentifizierungsmethoden empfohlen.
  • Plattformen wie NinjaOne ergänzen das Skript und bieten eine umfassende IT-Managementlösung.

DasRemotedesktopprotokoll (RDP) ist ein unverzichtbares Tool im Arsenal von IT-Fachleuten, mit dem Benutzer:innen über eine Netzwerkverbindung eine Fernverbindung zu einem anderen Computer herstellen können. Doch wie jedes leistungsfähige Tool erfordert auch RDP eine umsichtige Verwaltung, insbesondere angesichts der zunehmenden Sicherheitsbedenken. In diesem Blog wird ein PowerShell-Skript vorgestellt, das für die Verwaltung und Konfiguration von Remotedesktop (RDP)-Einstellungen auf Workstations entwickelt wurde.

Hintergrund

PowerShell hat sich aufgrund seiner Flexibilität und Tiefe schnell zu einem grundlegenden Tool für IT-Administratoren entwickelt. Das bereitgestellte Skript nutzt dieses Potenzial, indem es eine übersichtliche Methode zum Aktivieren oder Deaktivieren von RDP für Arbeitsstationen bietet. Da IT-Umgebungen immer komplexer werden, sind rationalisierte Lösungen wie dieses Skript für Managed Service Provider (MSPs ) und IT-Experten unverzichtbar. Die korrekte Konfiguration von RDP ist von entscheidender Bedeutung, da jede Fehlkonfiguration zu Schwachstellen führen kann.

Das Skript

<#
.SYNOPSIS
    Enables or Disables RDP for workstations only.
.DESCRIPTION
    Enables or Disables RDP for workstations only.
.EXAMPLE
    -Disable
    Disables RDP for a workstation.
.EXAMPLE
    -Enable
    Enables RDP for a workstation.
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes:
    Initial Release
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).
#>
[CmdletBinding(DefaultParameterSetName = "Disable")]
param (
    [Parameter(Mandatory = $true, ParameterSetName = "Enable")]
    [switch]
    $Enable,
    [Parameter(Mandatory = $true, ParameterSetName = "Disable")]
    [switch]
    $Disable
)

begin {
    function Set-ItemProp {
        param (
            $Path,
            $Name,
            $Value,
            [ValidateSet("DWord", "QWord", "String", "ExpandedString", "Binary", "MultiString", "Unknown")]
            $PropertyType = "DWord"
        )
        # Do not output errors and continue
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
        if (-not $(Test-Path -Path $Path)) {
            # Check if path does not exist and create the path
            New-Item -Path $Path -Force | Out-Null
        }
        if ((Get-ItemProperty -Path $Path -Name $Name)) {
            # Update property and print out what it was changed from and changed to
            $CurrentValue = Get-ItemProperty -Path $Path -Name $Name
            try {
                Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force -Confirm:$false -ErrorAction Stop | Out-Null
            }
            catch {
                Write-Error $_
            }
            Write-Host "$Path$Name changed from $CurrentValue to $Value"
        }
        else {
            # Create property with value
            try {
                New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType $PropertyType -Force -Confirm:$false -ErrorAction Stop | Out-Null
            }
            catch {
                Write-Error $_
            }
            Write-Host "Set $Path$Name to $Value"
        }
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue
    }
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }

    # Registry settings
    $Path = 'HKLM:\System\CurrentControlSet\Control\Terminal Server'
    $Name = "fDenyTSConnections"
    $RegEnable = 0
    $RegDisable = 1

    $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
    $IsWorkstation = if ($osInfo.ProductType -eq 1) {
        $true
    }
    else {
        $false
    }
}
process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }
    if (-not $IsWorkstation) {
        # System is a Domain Controller or Server
        Write-Error "System is a Domain Controller or Server. Skipping."
        exit 1
    }

    # Registry
    if ($Disable) {
        $RegCheck = $null
        $RegCheck = $(Get-ItemPropertyValue -Path $Path -Name $Name -ErrorAction SilentlyContinue)
        if ($null -eq $RegCheck) {
            $RegCheck = 0
        }
        if ($RegDisable -ne $RegCheck) {
            Set-ItemProp -Path $Path -Name $Name -Value $RegDisable
            Write-Host "Disabled $Path$Name"
        }
        else {
            Write-Host "$Path$Name already Disabled."
        }
    }
    elseif ($Enable) {
        $RegCheck = $null
        $RegCheck = $(Get-ItemPropertyValue -Path $Path -Name $Name -ErrorAction SilentlyContinue)
        if ($null -eq $RegCheck) {
            $RegCheck = 0
        }
        if ($RegEnable -ne $RegCheck) {
            Set-ItemProp -Path $Path -Name $Name -Value $RegEnable
            Write-Host "Enabled $Path$Name"
        }
        else {
            Write-Host "$Path$Name already Enabled."
        }
    }
    else {
        Write-Error "Enable or Disable was not specified."
        exit 1
    }

    # Firewall
    if ($Disable) {
        # Disable if was enabled and Disable was used
        try {
            Disable-NetFirewallRule -DisplayGroup "Remote Desktop" -ErrorAction Stop
        }
        catch {
            Write-Error $_
            Write-Host "Remote Desktop firewall group is missing?"
        }
        Write-Host "Disabled Remote Desktop firewall rule groups."
    }
    elseif ($Enable) {
        # Enable if was disabled and Enable was used
        try {
            Enable-NetFirewallRule -DisplayGroup "Remote Desktop" -ErrorAction Stop
        }
        catch {
            Write-Error $_
            Write-Host "Remote Desktop firewall group is missing?"
        }
        Write-Host "Enabled Remote Desktop firewall rule groups."
    }
    else {
        Write-Error "Enable or Disable was not specified."
        exit 1
    }
}
end {}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

  • Parameter: Das Skript verwendet zwei Parameter, Enable und Disable, die festlegen, ob RDP ein- oder ausgeschaltet werden soll. Diese schließen sich gegenseitig aus; es kann immer nur eine verwendet werden.
  • Hilfsfunktionen: Zwei Funktionen unterstützen die Hauptaufgabe:
  • Set-ItemProp: Aktualisiert oder erstellt Registrierungseigenschaften, behandelt mögliche Fehler und hält den Benutzer auf dem Laufenden.
  • Test-IsElevated: Prüft, ob das Skript mit Administratorrechten ausgeführt wird.
  • Prozess: Dies ist der Kern des Drehbuchs. Zunächst wird geprüft, ob der Rechner über Administratorrechte verfügt und ob es sich um einen Arbeitsplatzrechner handelt. Dann fährt es fort:
  • Ändern Sie die Registrierungseinstellungen, um RDP zu aktivieren oder zu deaktivieren.
  • Passen Sie die Firewall-Einstellungen an, um den RDP-Datenverkehr zuzulassen oder zu blockieren.

Potenzielle Anwendungsfälle

Stellen Sie sich ein mittelständisches Unternehmen mit mehreren Arbeitsplätzen für seine Mitarbeiter:innen vor. Die IT-Abteilung hat aus Sicherheitsgründen RDP auf allen Rechnern deaktiviert. Ein externer Berater benötigt jedoch Fernzugriff auf eine Workstation für die Diagnose. Mit diesem Skript kann der IT-Administrator RDP nahtlos auf dieser speziellen Workstation aktivieren und nach Abschluss der Aufgabe deaktivieren.

Vergleiche

Es gibt zwar GUI-basierte Tools und andere Methoden zur Verwaltung von RDP, aber das mitgelieferte Skript bietet die folgenden Vorteile:

  • Skalierbarkeit: Kann über ein Skript oder einen Aufgabenplaner auf mehreren Arbeitsplätzen ausgeführt werden.
  • Flexibilität: Einfache Integration in umfangreichere IT-Workflows.
  • Transparenz: Da es sich um eine Open-Source-Lösung handelt, kann das IT-Team das Skript validieren und an die jeweiligen Anforderungen anpassen.

FAQs

  • Kann dieses Skript auf Servern ausgeführt werden?
    Nein, das Skript prüft ausdrücklich, ob es sich bei dem Rechner um eine Arbeitsstation handelt, bevor es ausgeführt wird.
  • Was passiert, wenn das Skript ohne Admin-Rechte ausgeführt wird?
    Es wird eine Fehlermeldung angezeigt, die den Benutzer auffordert, das Programm mit Administratorrechten auszuführen.

Auswirkungen

Die Aktivierung von RDP ist zwar bequem, aber ein ungeschütztes RDP kann ein erhebliches Sicherheitsrisiko darstellen. Cyberkriminelle nutzen häufig falsch konfigurierte RDPs aus. Daher ist es unerlässlich, ein Gleichgewicht zwischen Komfort und Sicherheit zu finden.

Empfehlungen

  • Deaktivieren Sie RDP immer, wenn es nicht verwendet wird.
  • Überwachen Sie RDP-Protokolle auf verdächtige Aktivitäten.
  • Verwenden Sie starke Authentifizierungsmethoden, wenn RDP aktiviert ist.

Abschließende Überlegungen

Tools wie NinjaOne verbessern den IT-Betrieb und bieten eine zentrale Plattform zur Verwaltung und Überwachung von Netzwerken, Geräten und mehr. Bei der Integration von Lösungen wie dem besprochenen PowerShell-Skript in umfassendere IT-Frameworks bieten Plattformen wie NinjaOne unschätzbare Übersicht und Effizienz.

Durch das Verständnis und die Bereitstellung von PowerShell-Skripten wie dem oben beschriebenen können IT-Experten ihre Effizienz und die Sicherheit ihrer Arbeitsumgebungen verbessern. Durch die Kombination mit leistungsstarken Tools wie NinjaOne wird das IT-Management noch robuster.

Nächste Schritte

Der Aufbau eines effizienten und effektiven IT-Teams erfordert eine zentralisierte Lösung, die als vereintes Tool für die Bereitstellung von Dienstleistungen fungiert. NinjaOne ermöglicht es IT-Teams, all ihre Geräte zu überwachen, verwalten, sichern und zu unterstützen, unabhängig von ihrem Ort und komplexer Infrastruktur vor Ort.

Erfahren Sie mehr über NinjaOne Endpoint Management schauen Sie sich eine Live-Tour an oder starten Sie Ihre kostenlose Testversion der NinjaOne Plattform.

Kategorien:

Das könnte Sie auch interessieren

Demo ansehen×
×

Sehen Sie NinjaOne in Aktion!

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

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).