Zurücksetzen von Benutzerkennwörtern mit PowerShell

Die Verwaltung des Benutzerzugangs und der Anmeldedaten ist ein wesentlicher Bestandteil der IT-Verwaltung. Egal, ob es sich um ein großes Unternehmen mit komplexen IT-Anforderungen oder ein kleineres Unternehmen mit einfachen Anforderungen handelt, eine gemeinsame Aufgabe ist die Passwortverwaltung. Das bereitgestellte Skript zeigt eine Methode zum Zurücksetzen von Benutzerkennwörtern mit PowerShell, entweder für lokale Windows-Benutzer oder innerhalb einer Active Directory-Umgebung.

Hintergrund

Das Skript wurde in erster Linie entwickelt, um IT-Fachleuten und Managed Service Providern (MSPs ) einen rationellen Ansatz zum Zurücksetzen von Benutzerpasswörtern zu bieten, ohne tief in die Benutzeroberflächen des Systems einzutauchen. Angesichts der ständig wachsenden Zahl von Benutzer:innen und der ständigen Anforderung, Sicherheitspraktiken durchzusetzen, ist ein Tool, das solche Vorgänge schnell und zuverlässig durchführt, von unschätzbarem Wert. Dieses Skript, insbesondere in Kombination mit Tools wie NinjaOne, bietet Automatisierung und Effizienz für diese Aufgaben.

Das Skript

#Requires -Version 5.1

<#
.SYNOPSIS
    Resets a users password.
.DESCRIPTION
    Resets a users password. Either a local user that this script runs on, or in Active directory.
.EXAMPLE
     -UserName "Fred" -Password "Somepass1"
    Resets Fred's password to Somepass1 .
.EXAMPLE
     -UserName "Fred" -Password "Somepass1" -IsDomainUser
    Resets Fred's password to Somepass1 in Active Directory.
.EXAMPLE
    PS C:> .Reset-User-Password.ps1 -UserName "Fred" -Password "Somepass1" -IsDomainUser
    Resets Fred's password to Somepass1 in Active Directory.
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2012
    The RSAT feature for Active Directory needs to be installed on the computer this runs on.
    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 (
    [Parameter(Mandatory = $true)]
    [String]
    $UserName,
    [Parameter(Mandatory = $true)]
    [String]
    $Password,
    [Switch]
    $IsDomainUser
)
    
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 }
    }
}
    
process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }
    if ($IsDomainUser) {
        # Active Directory
        # Check if the ActiveDirectory module is installed
        if ((Get-Module -Name ActiveDirectory -ListAvailable -ErrorAction SilentlyContinue)) {
            try {
                # Try to import the ActiveDirectory module
                Import-Module -Name ActiveDirectory
            }
            catch {
                Write-Error -Message "Ninja Agent could not access AD, either RSAT was not installed or that the agent does not have permissions to add and remove users from groups."
                exit 5 # Access Denied exit code
            }
            try {
                $User = Get-ADUser -Identity $UserName
                Set-ADAccountPassword -Identity $User -Reset -NewPassword $(ConvertTo-SecureString -String $Password -AsPlainText -Force)    
                Write-Host "Reset Password for user: $UserName"
                exit 0
            }
            catch {
                Write-Host "Failed to Reset Password for user: $UserName"
                exit 1
            }
        }
        else {
            Write-Host "User ($UserName) does not exist."
            exit 1
        }
    }
    else {
        $User = Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue
        if ($User) {
            try {
                Set-LocalUser -Name $UserName -Password $(ConvertTo-SecureString -String $Password -AsPlainText -Force) -Confirm:$false
                Write-Host "Reset Password for user: $UserName"
                exit 0
            }
            catch {
                Write-Host "Failed to Reset Password for user: $UserName"
                exit 1
            }
        }
        else {
            Write-Host "User ($UserName) does not exist."
            exit 1
        }    
    }
}
    
end {}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

Parameter: Das Skript beginnt mit der Definition der erforderlichen Parameter, d. h. des Benutzernamens und des Passworts. Es gibt auch einen optionalen Schalter für IsDomainUser, der, wenn er verwendet wird, angibt, dass der Vorgang für einen Active Directory-Benutzer bestimmt ist.

Initialisierung:

Vor dem Hauptvorgang prüft das Skript, ob es mit Administrator-Rechten ausgeführt wird. Dies geschieht mit der Funktion Test-IsElevated, die die aktuelle Ausführungsrolle des Skripts überprüft.

Prozess:

Je nach dem Wert des Schalters IsDomainUser verzweigt das Skript in zwei verschiedene Vorgänge:

  • Active Directory: Wenn der Schalter IsDomainUser gesetzt ist, prüft das Skript, ob das ActiveDirectory-Modul vorhanden ist, importiert es und versucht dann, das Passwort des Benutzers im Active Directory zurückzusetzen.
  • Lokaler Benutzer: Ohne den Schalter IsDomainUser zielt das Skript auf einen lokalen Benutzer ab, prüft dessen Existenz und versucht dann, das Kennwort zurückzusetzen.

Potenzielle Anwendungsfälle

Stellen Sie sich einen IT-Administrator namens John vor, der die Netzwerkinfrastruktur eines mittelständischen Unternehmens verwaltet. Er hat bereits mehrere Anfragen von Mitarbeiter:innen erhalten, die ihre Passwörter vergessen haben. Anstatt sie manuell über die grafische Benutzeroberfläche zurückzusetzen, verwendet John dieses Skript. Mit einem einzigen Befehl kann er nun das Passwort jedes Benutzers zurücksetzen, was ihm Zeit spart und eine einheitliche Vorgehensweise gewährleistet.

Vergleiche

Für das Zurücksetzen von Kennwörtern in Windows oder Active Directory waren bisher GUI-basierte Tools wie „Computer Management“ für lokale Benutzer oder „Active Directory Users and Computers“ für AD-Benutzer erforderlich. Diese Tools sind zwar robust und funktionsreich, umfassen aber oft mehrere Schritte. Dieses PowerShell-Skript bietet einen optimierten Befehlszeilenansatz, der sich leicht in Automatisierungsworkflows integrieren lässt.

FAQs

  • Sind Administratorrechte erforderlich? 
    Ja, das Skript muss mit Administrator-Rechten ausgeführt werden.
  • Kann dieses Skript sowohl lokale als auch AD-Benutzer:innen verwalten? 
    Ja, mit dem Schalter IsDomainUser kann das Skript AD-Benutzer ansprechen.

Auswirkungen

Das Skript ist zwar effizient, aber auch sicherheitsrelevant. Missbrauch kann zu unbeabsichtigtem Zugriff oder Kontosperrungen führen. Stellen Sie immer sicher, dass Passwörter verantwortungsvoll und mit dem Wissen des Benutzers zurückgesetzt werden.

Empfehlungen

  • Erstellen Sie immer eine Sicherungskopie, bevor Sie größere Änderungen vornehmen.
  • Testen Sie das Skript in einer kontrollierten Umgebung, bevor Sie es in der Produktion einsetzen.
  • Verwenden Sie beim Zurücksetzen sichere, eindeutige Kennwörter.

Abschließende Überlegungen

IT-Fachleute, die eine weitere Automatisierung und optimierte Abläufe anstreben, können dieses Skript mit Tools wie NinjaOne ergänzen. NinjaOne bietet robuste IT-Management-Lösungen, die sich gut in benutzerdefinierte Skripte wie dieses integrieren lassen, so dass Fachleute das Beste aus ihrer Infrastruktur herausholen können. Die Gewährleistung eines sicheren Zugriffs auf die Benutzerkonten ist für das IT-Management von grundlegender Bedeutung. Durch den Einsatz von PowerShell und Tools wie NinjaOne kann die Komplexität von Aufgaben wie dem Zurücksetzen von Kennwörtern reduziert werden, so dass sich IT-Teams auf dringendere Probleme konzentrieren können.

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

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