Aktualisieren von Windows-Kennwortrichtlinien mit PowerShell

Wichtigste Erkenntnisse

  • Passwortrichtlinien sind für die Stärkung der IT-Sicherheit in Unternehmen unerlässlich.
  • Das mitgelieferte Skript automatisiert den Prozess der Änderung der Passwortkomplexität in Windows.
  • Das Skript unterstützt sowohl Domänen- als auch Nicht-Domänen-Computer.
  • Die Ausführung des Skripts ohne Administratorrechte führt zum Abbruch.
  • Herkömmliche Methoden zur Änderung von Kennwortrichtlinien können zeitaufwändiger sein als dieses Skript.
  • Testen Sie das Skript immer in einer kontrollierten Umgebung, bevor Sie es einsetzen.
  • Die Information der Benutzer:innen über Änderungen der Kennwortrichtlinien kann Verwirrung und mögliche Aussperrungen verhindern.
  • Regelmäßige Überprüfungen und Anpassungen der Passwortrichtlinien sind eine bewährte Praxis.
  • NinjaOne kann nahtlos mit Automatisierungswerkzeugen integriert werden, um die Sicherheitsmaßnahmen zu verstärken.

In der heutigen IT-Landschaft steht die Sicherheit an erster Stelle. In Zeiten, in denen Datenschutzverletzungen Schlagzeilen machen, ist die Festlegung starker Passwortrichtlinien nicht nur eine Option, sondern eine Notwendigkeit. Da Unternehmen ihre Verteidigungsmaßnahmen verstärken wollen, ist eine effiziente Methode zur Aktualisierung von Windows-Kennwortrichtlinien ein wichtiges Thema.

Hintergrund

Kennwortrichtlinien, insbesondere in Windows-Umgebungen, helfen bei der Festlegung der Anforderungen für die Erstellung und Verwaltung von Kennwörtern. Wenn diese streng eingehalten werden, verringert sich das Risiko eines unbefugten Zugriffs. Das hier zur Verfügung gestellte Skript ist für IT-Profis und Managed Service Provider (MSPs ) zugeschnitten, um mühelos die Passwortkomplexität für einen Domänen- oder Nicht-Domänen-Computer zu ändern.

Warum ist ein solches Tool von unschätzbarem Wert? Die manuelle Änderung von Kennwortrichtlinien kann vor allem in großen Unternehmen mühsam sein. Ein Skript wie dieses automatisiert den Prozess und macht ihn schneller und weniger anfällig für menschliche Fehler.

Das Skript

#Requires -Version 5.1

<#
.SYNOPSIS
    Modifies the password complexity settings for a domain or a non-domain computer.
.DESCRIPTION
    This script can be used to enable or disable the password complexity requirement on a domain or a non-domain computer.
    When using -Domain, this script must be run on a Domain Controller with the RSAT feature installed.
.PARAMETER ComplexityEnabled
    Enables the Password Complexity requirement.
.PARAMETER Domain
    Specifies the name of the domain. If specified, it will enable or disable the password complexity requirement on the Active Directory Default Domain Password Policy.
    The computer this script is executed on MUST have the PowerShell RSAT features installed when using the -Domain flag.
.EXAMPLE
     -ComplexityEnabled
    Enables the password complexity requirement on the computer this script runs on.
.EXAMPLE
     No param needed
    Disables the password complexity requirement on the computer this script runs on.
.EXAMPLE
     -ComplexityEnabled -Domain "test.consto.com"
    Enables the password complexity requirement in Active Directory for the Default Domain Password Policy.
    When using -Domain, this script must be run on a Domain Controller with the RSAT feature installed.
.EXAMPLE
     -Domain "test.consto.com"
    Disables the password complexity requirement in Active Directory for the Default Domain Password Policy.
    When using -Domain, this script must be run on a Domain Controller with the RSAT feature installed.
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes: Renamed script and added Script Variable support
    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 = $false)]
    [Switch]
    $ComplexityEnabled,
    [Parameter(Mandatory = $false)]
    [String]
    $Domain
)
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 Test-DomainJoined {
        # Check if the computer is domain joined
        if ((Get-Command -Name Get-WmiObject -ErrorAction SilentlyContinue)) {
            return $(Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain
        }
        elseif ((Get-Command -Name Get-CIMInstance -ErrorAction SilentlyContinue)) {
            return $(Get-CimInstance -ClassName Win32_ComputerSystem).PartOfDomain
        }
        else {
            Write-Host "[Error] Get-WmiObject and Get-CIMInstance are not available. This script requires at least one of these cmdlets to run."
            exit 1
        }
    }
}
process {
    if (-not (Test-IsElevated)) {
        Write-Host "[Error] Access Denied. Please run with Administrator privileges."
        exit 1
    }
    
    if (-not ([string]::IsNullOrWhiteSpace($Domain))) {
        # Active Directory
    
        # Check if we are running on a Domain Controller, exit if we aren't
        $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
        if ($osInfo.ProductType -ne 2) {
            Write-Host "[Error] This needs to run on a Domain Controller."
            exit 1
        }
        # Set ComplexityEnabled to what was passed into $ComplexityEnabled
        Set-ADDefaultDomainPasswordPolicy -Identity $Domain -ComplexityEnabled $(if ($ComplexityEnabled) { $true }else { $false }) -Confirm:$false
        # Sleep for a while, just in case Get-ADDefaultDomainPasswordPolicy connects to a different AD server and replication is slow
        Start-Sleep -Seconds 60
        # Check if the ComplexityEnabled policy was applied correctly
        $Results = Get-ADDefaultDomainPasswordPolicy -Identity $Domain
        # Check that the policy matches what was requested
        if ($Results -and $Results.ComplexityEnabled -eq $ComplexityEnabled) {
            Write-Host "[Info] Set Complexity in Default Domain Password Policy to $ComplexityEnabled"
            return
        }
        else {
            # The policy was not set for some reason
            Write-Host "[Error] Failed to set Complexity in Default Domain Password Policy to $ComplexityEnabled"
            exit 1
        }
    }
    else {
        # Localhost
        # Check if the computer is domain joined

        if ($(Test-DomainJoined)) {
            Write-Host "[Error] This Computer is domain joined. Modifying the local policy is not supported for domain joined computers."
            exit 1
        }

        # Set the path for our temp local policy config file
        $Path = "$PSScriptRoot\Set-Password-Complexity-secpol.cfg"
        # Get our local policy
        SecEdit.exe /export /cfg $Path
        if ($LASTEXITCODE -gt 0) {
            Write-Host "[Error] Failed to read local machine Policy"
            exit 1
        }

        # Next make sure that we are not changing something that does not need to be changed
        # if $ComplexityEnabled is True and the temp local policy config file has PasswordComplexity set to 0, then proceed
        if ($ComplexityEnabled -and $(Get-Content -Path $Path) -Match "^PasswordComplexity = 0$") {
            # Change PasswordComplexity from 0 to 1
            $(Get-Content -Path $Path) -Replace "PasswordComplexity = 0", "PasswordComplexity = 1" | Out-File $Path
            # Update the local policy with our changes
            SecEdit.exe /configure /db c:\windows\security\local.sdb /cfg $Path /areas SECURITYPOLICY
            if ($LASTEXITCODE -gt 0) {
                Write-Host "[Error] Failed to set Complexity in local machine Policy to $ComplexityEnabled"
                exit 1
            }
        }# if $ComplexityEnabled is False and the temp local policy config file has PasswordComplexity set to 1, then proceed
        elseif (-not $ComplexityEnabled -and $(Get-Content -Path $Path) -Match "^PasswordComplexity = 1$") {
            # Change PasswordComplexity from 1 to 0
            $(Get-Content -Path $Path) -Replace "PasswordComplexity = 1", "PasswordComplexity = 0" | Out-File $Path
            # Update the local policy with our changes
            SecEdit.exe /configure /db c:\windows\security\local.sdb /cfg $Path /areas SECURITYPOLICY
            if ($LASTEXITCODE -gt 0) {
                Write-Host "[Error] Failed to set Complexity in local machine Policy to $ComplexityEnabled"
                exit 1
            }
        }
        # Remove our temp local policy config file
        Remove-Item $Path -Force
        # Get our local policy
        SecEdit.exe /export /cfg $Path
        # Check if the temp local policy config file has PasswordComplexity set to match our $ComplexityEnabled
        if (
            ($ComplexityEnabled -and $(Get-Content $Path) -Match "^PasswordComplexity = 1$") -or
            (-not $ComplexityEnabled -and $(Get-Content $Path) -Match "^PasswordComplexity = 0$")
        ) {
            # Remove our temp local policy config file again
            Remove-Item $Path -Force
            Write-Host "[Info] Set Complexity in local machine Policy to $ComplexityEnabled"
            return
        }
        else {
            # Remove our temp local policy config file again
            Remove-Item $Path -Force
            Write-Host "[Error] Failed to set Complexity in local machine Policy to $ComplexityEnabled"
            exit 1
        }
    }
}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

  • Ursprüngliche Anforderungen: Das Skript ist für Windows 10 oder Windows Server 2016 konzipiert. Wenn die Active Directory-Funktion verwendet wird, sollte zusätzlich RSAT für Active Directory installiert werden.
  • Parameter: Das Skript akzeptiert zwei optionale Parameter:
  • $ComplexityEnabled: Ein Schalter, der die Passwortkomplexität aktiviert oder deaktiviert.
  • $Domain: Gibt den Domänennamen an, für den die Kennwortrichtlinie geändert werden soll.
  • Elevation-Check: Vor jeder Aktion prüft das Skript, ob es über Administratorrechte verfügt. Ist dies nicht der Fall, bricht es ab und stellt sicher, dass Aktionen nur mit den richtigen Berechtigungen ausgeführt werden.
  • Active Directory: Wenn eine Domäne angegeben ist, wird sie:
  • Überprüft, ob es auf einem Domain Controller ausgeführt wird.
  • Überprüft das Vorhandensein des ActiveDirectory-Moduls.
  • Ändert die Standard-Domänenkennwortrichtlinie basierend auf dem Parameter $ComplexityEnabled.
  • Lokale Richtlinie: Wenn keine Domäne angegeben ist:
  • Erstellt es eine temporäre Konfigurationsdatei für die Richtlinie (Policy).
  • Überprüft die aktuelle Einstellung zur Passwortkomplexität.
  • Modifiziert diese Einstellung basierend auf dem Parameter $ComplexityEnabled.

Potenzielle Anwendungsfälle

Fallstudie: Stellen Sie sich eine IT-Fachkraft, Jane, vor, die in einem Unternehmen arbeitet, das kürzlich von Phishing-Angriffen betroffen war. Ihr Team beschließt, strengere Passwortrichtlinien durchzusetzen. Anstatt jedes System oder jeden Domänencontroller manuell zu überprüfen, verwendet Jane dieses Skript. Indem sie es einfach mit den gewünschten Parametern ausführt, kann sie die Kennwortrichtlinien schnell aktualisieren und so die Sicherheitslage des Unternehmens verbessern.

Vergleiche

Bisher musste man sich beim Ändern von Kennwortrichtlinien in Windows oft durch mehrere GUI-basierte Schnittstellen wie die Gruppenrichtlinienverwaltung oder die lokale Sicherheitsrichtlinie bewegen. Diese Methoden sind zwar wirksam, können aber zeitaufwändig sein. Dieses PowerShell-Skript rationalisiert den Prozess und bietet einen schnelleren und effizienteren Ansatz.

FAQs

  • Welche Versionen von Windows unterstützt dieses Skript? 
    Windows 10 und Windows Server 2016.
  • Muss ich immer eine Domäne angeben? 
    Nein, wenn Sie keine Domäne angeben, wird das Skript standardmäßig auf dem lokalen Computer ausgeführt.
  • Was passiert, wenn das Skript nicht mit administrativen Rechten ausgeführt wird?
    Es bricht ab und gibt eine Fehlermeldung aus.

Auswirkungen

Auch wenn die Automatisierung von Änderungen der Kennwortrichtlinien die Prozesse beschleunigen kann, ist es wichtig, die Benutzer:innen über diese Änderungen zu informieren. Plötzliche Änderungen der Kennwortrichtlinien können zu Verwirrung und möglichen Aussperrungen führen und die Produktivität beeinträchtigen. Darüber hinaus kann aus Sicht der IT-Sicherheit die regelmäßige Aktualisierung und Durchsetzung von Passwortrichtlinien das Risiko eines unbefugten Zugriffs drastisch reduzieren.

Empfehlungen

  • Führen Sie das Skript immer in einer Testumgebung aus, bevor Sie es in einer realen Umgebung anwenden.
  • Regelmäßige Überprüfung und Anpassung der Passwortrichtlinien an die neuesten Sicherheitsempfehlungen.
  • Informieren Sie die Benutzer:innen über die Bedeutung sicherer Passwörter und den Grund für die Änderung der Richtlinien.

Abschließende Überlegungen

Im Zusammenhang mit der Verbesserung der IT-Sicherheit können Werkzeuge wie NinjaOne eine entscheidende Rolle spielen. Sie bieten nicht nur Überwachungslösungen, sondern lassen sich auch nahtlos in Skripte wie das hier beschriebene integrieren. Durch den Einsatz von NinjaOne zusammen mit solchen Automatisierungstools können IT-Experten die Sicherheitsmaßnahmen ihres Unternehmens effizient verwalten und verstärken.

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