Demo ansehen×
×

Sehen Sie NinjaOne in Aktion!

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

So fügen Sie einen Computer mit PowerShell zu einer Domäne hinzu

In der modernen IT-Landschaft ist die Sicherstellung, dass ein Computer der richtigen Domäne beitritt, mehr als nur eine Bequemlichkeit – sie ist ein wesentlicher Schritt zur Konsolidierung der Kontrolle, zur Vereinfachung des Zugriffs und zur Aufrechterhaltung der Netzwerksicherheit. Dieses PowerShell-Skript wurde entwickelt, um den Prozess des Hinzufügens eines Computers zu einer Domänezu optimieren.

Hintergrund

PowerShell hat sich bei IT-Expert:innen und Managed Service Providern (MSPs ) als robuste und vielseitige Skriptsprache etabliert. Seine Flexibilität ermöglicht es Administratoren, sich wiederholende Aufgaben zu automatisieren, Richtlinien durchzusetzen und Systeme schnell zu konfigurieren. Das hier vorgestellte Skript erfüllt eine wichtige Funktion: Es ermöglicht Computern den nahtlosen Beitritt zu einer Domäne. Domänen sind in der IT-Welt von Unternehmen von zentraler Bedeutung, da sie die gemeinsame Nutzung von Ressourcen, die zentralisierte Authentifizierung und die Durchsetzung von Richtlinien erleichtern. Ein Skript, das zuverlässig Computer zu einer Domäne hinzufügen kann, ist daher von großem Wert.

Das Skript

#Requires -Version 5.1

<#
.SYNOPSIS
    Joins a computer to a domain.
.DESCRIPTION
    Joins a computer to a domain.
.EXAMPLE
     -DomainName "Domain.com" -UserName "DomainMyDomainUser" -Password "Somepass1"
    Joins a computer to a "Domain.com" domain and restarts the computer. Don't expect a success result in Ninja as the computer will reboot before the script can return a result.
.EXAMPLE
     -DomainName "Domain.com" -UserName "DomainMyDomainUser" -Password "Somepass1" -NoRestart
    Joins a computer to a "Domain.com" domain and does not restart the computer.
.EXAMPLE
    PS C:> Join-Domain.ps1 -DomainName "domain.com" -UserName "DomainMyDomainUser" -Password "Somepass1" -NoRestart
    Joins a computer to a "Domain.com" domain and does not restart the computer.
.EXAMPLE
     -DomainName "Domain.com" -UserName "DomainMyDomainUser" -Password "Somepass1" -Server "192.168.0.1"
    Not recommended if the computer this script is running on does not have one of the Domain Controllers set as its DNS server.
    Joins a computer to a "Domain.com" domain, talks to the domain with the IP address of "192.168.0.1", and restarts the computer.
.OUTPUTS
    String[]
.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).
.COMPONENT
    ManageUsers
#>

[CmdletBinding()]
param (
    # Domain Name to join computer to
    [Parameter(Mandatory = $true)]
    [String]
    $DomainName,
    # Use a Domain UserName to join 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 join this computer from a domain
    [Parameter(Mandatory = $true)]
    $Password,
    # Used only when computer can't locate a domain controler via DNS or you wish to connect to a specific DC
    [Parameter()]
    $Server,
    # Do not restart computer after joining to a domain
    [Switch]
    $NoRestart
)
    
begin {
    function Join-ComputerToDomainPS2 {
        param (
            [String]
            $DomainName,
            [PSCredential]
            $Credential,
            $Restart,
            $Server
        )
        if ($Credential) {
            # Use supplied Credentials
            if ($Server) {
                Add-Computer -DomainName $DomainName -Credential $Credential -Server $Server -Force -Confirm:$false -PassThru
            }
            else {
                Add-Computer -DomainName $DomainName -Credential $Credential -Force -Confirm:$false -PassThru
            }
        }
        else {
            # No Credentials supplied, use current user
            Add-Computer -DomainName $DomainName -Force -Confirm:$false -PassThru
        }
    }
    Write-Output "Starting Join Domain"
    
    # Convert username and password into a credential object
    $JoinCred = [PSCredential]::new($UserName, $(ConvertTo-SecureString -String $Password -AsPlainText -Force))
}
    
process {
    Write-Output "Joining computer($env:COMPUTERNAME) to domain $DomainName"
    $script:JoinResult = $false
    try {
        $JoinResult = if ($NoRestart) {
            # Do not restart after joining
            if ($PSVersionTable.PSVersion.Major -eq 2) {
                if ($Server) {
                    (Join-ComputerToDomainPS2 -DomainName $DomainName -Credential $Credential -Server $Server).HasSucceeded
                }
                else {
                    (Join-ComputerToDomainPS2 -DomainName $DomainName -Credential $Credential).HasSucceeded
                }
            }
            else {
                if ($Server) {
                    (Add-Computer -DomainName $DomainName -Credential $JoinCred -Server $Server -Force -Confirm:$false -PassThru).HasSucceeded
                }
                else {
                    (Add-Computer -DomainName $DomainName -Credential $JoinCred -Force -Confirm:$false -PassThru).HasSucceeded
                }
            }
        }
        else {
            # Restart after joining
            if ($PSVersionTable.PSVersion.Major -eq 2) {
                if ($Server) {
                    (Join-ComputerToDomainPS2 -DomainName $DomainName -Credential $Credential -Server $Server).HasSucceeded
                }
                else {
                    (Join-ComputerToDomainPS2 -DomainName $DomainName -Credential $Credential).HasSucceeded
                }
            }
            else {
                if ($Server) {
                    (Add-Computer -DomainName $DomainName -Credential $JoinCred -Restart -Server $Server -Force -Confirm:$false -PassThru).HasSucceeded
                }
                else {
                    (Add-Computer -DomainName $DomainName -Credential $JoinCred -Restart -Force -Confirm:$false -PassThru).HasSucceeded
                }
            }
        }    
    }
    catch {
        Write-Error "Failed to Join Domain: $DomainName"
    }

    if ($NoRestart -and $JoinResult) {
        Write-Output "Joined computer($env:COMPUTERNAME) to Domain: $DomainName and not restarting computer"
    }
    elseif ($JoinResult) {
        Write-Output "Joined computer($env:COMPUTERNAME) to Domain: $DomainName and restarting computer"
        if ($PSVersionTable.PSVersion.Major -eq 2) {
            shutdown.exe -r -t 60
        }
    }
    else {
        Write-Output "Failed to Join computer($env:COMPUTERNAME) to Domain: $DomainName"
        # Clean up credentials so that they don't leak outside this script
        $JoinCred = $null
        exit 1
    }
}
    
end {
    # Clean up credentials so that they don't leak outside this script
    $JoinCred = $null
    Write-Output "Completed Join Domain"
}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

Das Skript beginnt mit umfassenden Metadaten, die den Zweck, die Anwendungsbeispiele und die erwarteten Ergebnisse beschreiben. Die Kernlogik folgt einem standardmäßigen PowerShell-Funktionslayout: begin, process und end.

  • Parameter: Dies sind Eingaben des Benutzers, einschließlich DomainName, UserName, Passwort und Server. Mit einem Schalterparameter namens NoRestart können Benutzer:innen entscheiden, ob der Computer nach dem Beitritt zur Domäne neu gestartet werden soll.
  • Beginnen Sie den Block: Dieser Abschnitt initialisiert das Skript. Es definiert eine Funktion Join-ComputerToDomainPS2 und konvertiert den angegebenen Benutzernamen und das Passwort in ein sicheres Credential-Objekt, das für den Beitrittsprozess verwendet werden kann.
  • Prozess-Block: Hier liegt die zentrale Logik. Je nach den angegebenen Parametern wird das Skript den Computer mit oder ohne Neustart in die Domäne aufnehmen. Es verwendet zwei primäre PowerShell-Befehle: Add-Computer und eine benutzerdefinierte Funktion. Wenn dabei Fehler auftreten, werden sie erkannt und gemeldet.
  • Endblock: Damit wird das Skript abgeschlossen und alle während seiner Ausführung verwendeten Anmeldeinformationen werden gelöscht.

Potenzielle Anwendungsfälle

Fallstudie: Stellen Sie sich einen MSP vor, der die IT für mehrere kleine Unternehmen übernimmt. Sie haben gerade 50 neue Rechner in Betrieb genommen und brauchen sie alle bis zum Geschäftsbeginn am nächsten Tag in der Domäne des Unternehmens. Mithilfe des Skripts gibt der MSP schnell die erforderlichen Daten für alle Maschinen ein. Innerhalb weniger Minuten ist jeder Computer eingerichtet, ohne dass manuelle Konfigurationen oder zeitaufwändige Einrichtungsarbeiten erforderlich sind.

Vergleiche

Herkömmliche Methoden zum Beitritt eines Computers zu einer Domäne umfassen manuelle Schritte über die Windows-Oberfläche oder die Verwendung von Legacy-Skripten. Diese Methoden können zeitaufwändig und fehleranfällig sein und lassen sich bei großen Einsätzen nicht gut skalieren. Unser PowerShell-Ansatz ist effizienter, weniger fehleranfällig und skalierbar, um mehrere Computer zu verwalten.

FAQs

  • Kann dieses Skript das massenhafte Hinzufügen von Computern zu einer Domäne verarbeiten?
    Ja, mit der richtigen Schleifenlogik können Sie mehrere Computer verarbeiten.
  • Was geschieht, wenn die angegebenen Domaindaten falsch sind?
    Das Skript gibt einen Fehler aus, der das Scheitern des Domänenbeitritts anzeigt.

Auswirkungen

Ein falscher Beitritt zu Domänen oder die Preisgabe von Anmeldedaten kann die IT-Sicherheit beeinträchtigen. Wenn ein Computer der falschen Domäne beitritt, kann er auf unberechtigte Ressourcen zugreifen oder Sicherheitsrisiken ausgesetzt werden.

Empfehlungen

  • Überprüfen Sie vor der Ausführung immer die Domänendetails.
  • Schützen Sie die Domänenanmeldeinformationen und wechseln Sie sie regelmäßig.
  • Testen Sie das Skript in einer kontrollierten Umgebung, bevor Sie es einsetzen.

Abschließende Überlegungen

In einer Zeit, in der die Automatisierung an erster Stelle steht, können Werkzeuge wie dieses Skript in Kombination mit Plattformen wie NinjaOne die Effizienz, Sicherheit und Verwaltbarkeit verbessern. NinjaOne bietet robuste Überwachungs- und Verwaltungsfunktionen, die sicherstellen, dass Computer, die einmal der Domäne hinzugefügt wurden, konform, sicher und leistungsoptimiert bleiben.

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