Demo ansehen×
×

Sehen Sie NinjaOne in Aktion!

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

So entfernen Sie einen Computer mithilfe von PowerShell aus einer Domäne

Im komplexen Ökosystem der IT-Verwaltung ist es von grundlegender Bedeutung, sicherzustellen, dass Geräte korrekt in eine Netzwerkdomäne aufgenommen oder aus ihr entfernt werden. Für IT-Expert:innen ist diese Aufgabe in der Regel repetitiv, aber die Bedeutung einer nahtlosen Ausführung kann nicht hoch genug eingeschätzt werden. Heute stellen wir Ihnen ein PowerShell-Skript vor, mit dem Sie einen Computer mühelos aus einer Domäne entfernen können.

Hintergrund

Das bereitgestellte Skript ist auf den speziellen Bedarf der Automatisierung von Domänen-Egress-Vorgängen ausgerichtet. Domänen dienen dazu, Benutzerkonten, Computer und Gruppenrichtlinien unter einem zentralen Dach zusammenzufassen. Es gibt verschiedene Gründe, warum ein IT-Experte einen Computer aus einer Domäne entfernen möchte, sei es aus Sicherheitsgründen, wegen Änderungen an der Infrastruktur oder wegen der Außerbetriebnahme von Hardware. Die manuelle Durchführung dieser Aufgabe ist nicht nur zeitaufwändig, sondern auch anfällig für menschliche Fehler. Daher ist ein robustes PowerShell-Skript wie das obige von unschätzbarem Wert für IT-Experten und Managed Service Provider (MSPs).

Das Skript

#Requires -Version 2.0

<#
.SYNOPSIS
    Removes the computer from the domain.
.DESCRIPTION
    Removes the computer from the domain.
.EXAMPLE
     -UserName "MyDomainUser" -Password "Somepass1" -LocalUserName "Administrator" -LocalPassword "Somepass1"
    Removes the computer from the domain and restarts the computer.
.EXAMPLE
     -UserName "MyDomainUser" -Password "Somepass1" -LocalUserName "Administrator" -LocalPassword "Somepass1" -NoRestart
    Removes the computer from the domain and does not restart the computer.
.EXAMPLE
    PS C:> Leave-Domain.ps1 -UserName "MyDomainUser" -Password "Somepass1" -LocalUserName "Administrator" -LocalPassword "Somepass1" -NoRestart
    Removes the computer from the domain and does not restart the computer.
.OUTPUTS
    String[]
.NOTES
    Minimum OS Architecture Supported: Windows 7, Windows Server 2012
    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).
.COMPONENT
    ManageUsers
#>

[CmdletBinding()]
param (
    # Use a Domain UserName to remove this computer to a domain, this requires the Password parameter to be used as well
    [Parameter(Mandatory = $true)]
    [String]
    $UserName,
    # Use a Domain Password to remove a computer from a domain
    [Parameter(Mandatory = $true)]
    $Password,
    # Use a local admin's UserName to remove this computer from a domain, this requires the Password parameter to be used as well
    [Parameter(Mandatory = $true)]
    [String]
    $LocalUserName,
    # Use a local admin's Password to remove this computer from a domain
    [Parameter(Mandatory = $true)]
    $LocalPassword,
    # Do not restart computer after leaving to a domain
    [Switch]
    $NoRestart
)
    
begin {
    Write-Output "Starting Leave Domain"
    
    # Converts username and password into a credential object
    $LeaveCred = [PSCredential]::new($UserName, $(ConvertTo-SecureString -String $Password -AsPlainText -Force))
    
    # Converts username and password into a credential object
    $LocalCred = [PSCredential]::new($LocalUserName, $(ConvertTo-SecureString -String $LocalPassword -AsPlainText -Force))
}
    
process {
    Write-Output "Removing computer($env:COMPUTERNAME) from domain"
    $script:LeaveResult = $false
    try {
        $LeaveResult = if ($NoRestart) {
            (Remove-Computer -UnjoinDomainCredential $LeaveCred -PassThru -Force -LocalCredential $LocalCred -Confirm:$false).HasSucceeded
            # Do not restart after leaving
        }
        else {
            # Restart after leaving
            (Remove-Computer -UnjoinDomainCredential $LeaveCred -PassThru -Force -Restart -LocalCredential $LocalCred -Confirm:$false).HasSucceeded
        }    
    }
    catch {
        Write-Error "Failed to Leave Domain"
    }
    if ($LeaveResult) {
        if ($NoRestart) {
            Write-Output "Removed computer($env:COMPUTERNAME) from domain and not restarting computer"
        }
        else {
            Write-Output "Removed computer($env:COMPUTERNAME) from domain and restarting computer"
        }
    }
    else {
        Write-Output "Failed to remove computer($env:COMPUTERNAME) from domain"
        # Clean up credentials so that they don't leak outside this script
        $LeaveCred = $null
        $LocalCred = $null
        exit 1
    }
}
    
end {
    # Clean up credentials so that they don't leak outside this script
    $LeaveCred = $null
    $LocalCred = $null
    Write-Output "Completed Leave Domain"
}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detaillierte Aufschlüsselung

Das Skript folgt einem modularen Ansatz und umfasst drei Hauptabschnitte – Anfang, Verlauf und Ende.

  • Im Abschnitt “begin“ wird das Skript initialisiert, es akzeptiert Parameter wie Domänen- und lokale Benutzernamen/Passwörter und konstruiert aus diesen Parametern Anmeldeinformationen.
  • Der Prozessteil ist der Ort, an dem der eigentliche Vorgang stattfindet. Es verwendet das Cmdlet “ Remove-Computer“ von PowerShell, ein leistungsfähiges Tool zum Entfernen des Computers aus seiner aktuellen Domäne. Die Flexibilität des Skripts ermöglicht es, den Computer nach der Entfernung neu zu starten oder ihn laufen zu lassen, je nachdem, was der/die Benutzer:in bevorzugt.
  • Der letzte Abschnitt befasst sich mit der Bereinigung, wobei sichergestellt wird, dass die verwendeten Anmeldeinformationen gelöscht werden, um mögliche Sicherheitsprobleme zu vermeiden.

Potenzielle Anwendungsfälle

Stellen Sie sich einen IT-Experten namens Alex vor, der die Netzwerkinfrastruktur eines mittelständischen Unternehmens verwaltet. Sie haben gerade mehrere alte Arbeitsstationen außer Betrieb genommen und durch neue ersetzt. Anstatt jeden Computer manuell von der Domäne zu trennen, verwendet Alex dieses Skript und spart so Stunden an Arbeit, da sichergestellt wird, dass keine Anmeldeinformationen auf den stillgelegten Computern zurückbleiben.

Vergleiche

Während die auf der grafischen Benutzeroberfläche basierende Methode, die über „Systemeigenschaften“ durchgeführt wird, einen visuelleren Ansatz bietet, zeichnet sich das PowerShell-Skript durch Automatisierung, Skalierbarkeit und Präzision aus. Für das Entfernen eines einzelnen Computers mag die grafische Benutzeroberfläche ausreichen, aber für Massenoperationen ist PowerShell unübertroffen.

FAQs

  • Benötige ich Administratorrechte, um dieses Skript auszuführen? Ja, das Entfernen eines Computers aus einer Domäne erfordert Administratorrechte sowohl auf dem lokalen Rechner als auch in der Domäne.
  • Was passiert, wenn ich den Schalter -NoRestart nicht zur Verfügung stelle? Standardmäßig wird der Computer neu gestartet, nachdem er aus der Domäne entfernt wurde.

Auswirkungen

Die Verwendung solcher Skripte zur Verwaltung von Domänenoperationen ist nicht nur eine Frage der Bequemlichkeit, sondern auch der Sicherheit. Es ist von größter Wichtigkeit, dass die Anmeldedaten nicht nach außen dringen, wie es das Skript zu Recht tut. Ein schlecht verwalteter Domänenumzug kann Schlupflöcher für Cyber-Angreifer bieten.

Empfehlungen

  • Testen Sie das Skript immer in einer kontrollierten Umgebung, bevor Sie es in einem Live-Szenario einsetzen.
  • Stellen Sie sicher, dass Sicherungsmechanismen vorhanden sind, insbesondere wenn Sie umfangreiche Änderungen vornehmen.
  • Aktualisieren Sie das Skript regelmäßig, um Änderungen am Betriebssystem oder an PowerShell-Cmdlets zu berücksichtigen.

Abschließende Überlegungen

PowerShell-Skripte wie das besprochene bieten zwar einen immensen Nutzen, aber der Einsatz von Plattformen wie NinjaOne kann den IT-Betrieb weiter optimieren. NinjaOne bietet eine einheitliche IT-Überwachungs- und Verwaltungsplattform, die Skripte, Automatisierung und eine Vielzahl von IT-Aufgaben nahtlos unter einem Dach integriert. Für Fachleute, die ihre IT-Verwaltungsaufgaben optimieren wollen, bieten Tools wie dieses Skript, wenn sie in Plattformen wie NinjaOne integriert sind, einen vielversprechenden Horizont.

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