Demo ansehen×
×

Sehen Sie NinjaOne in Aktion!

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

Effiziente Auflistung aller Gruppen, zu denen Benutzer:innen in PowerShell gehören

In der schnelllebigen Welt der IT bleiben effektives Benutzermanagement und das Verständnis von Benutzerrollen entscheidend für die Sicherheit und Effizienz jeder Organisation. In der IT-Welt sind PowerShell-Skripte kraftvolle Werkzeuge zur Automatisierung von Aufgaben. Ein solches Skript hat das Ziel, alle Gruppen, denen eine Person angehört, aufzulisten.

Hintergrund

Die Zugehörigkeit zu einer Benutzergruppe definiert die Rechte und Berechtigungen, die eine Person innerhalb eines Netzwerks hat. Mit dem Unternehmenswachstum gewinnt die Mitgliedschaftsverfolgung an Bedeutung. Dennoch kann es umständlich und fehleranfällig sein, wenn es manuell erledigt wird. Hier zeigt unser PowerShell-Skript seine Stärken. Mit seiner speziellen Ausrichtung, alle Gruppen aufzulisten, denen eine Person angehört, erweist sich dieses Skript als unverzichtbares Werkzeug für IT-Profis und Managed Service Provider (MSPs). Durch die Sicherstellung der richtigen Gruppenzugehörigkeit wird eine übermäßige Vergabe von Privilegien vermieden und die Sicherheitsstruktur eines Unternehmens gestärkt.

Das Skript

#Requires -Version 4.0 -RunAsAdministrator

<#
.SYNOPSIS
    This will output the groups that the specified user belongs to.
.DESCRIPTION
    This will output the groups that the specified user belongs to.
.EXAMPLE
     -UserName "Administrator" -IsDomainUser
    Will get the groups that the user Administrator belongs to in Active Directory.
.EXAMPLE
     -UserName "Administrator"
    Will get the groups that the user Administrator belongs to on the machine it runs on.
.EXAMPLE
    PS C:> Get-User-Membership.ps1 -UserName "Administrator"
    Will get the groups that the user Administrator belongs to on the machine it runs on.
.OUTPUTS
    Output (PSCustomObject)
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2012
    If you wish to interact with AD you will need to install RSAT with at least the AD feature.
    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 (
    # Specify one user
    [Parameter(Mandatory = $false)]
    [String]
    $UserName,
    # Modify Local User Membership
    [Parameter(Mandatory = $false)]
    [Switch]
    $IsDomainUser
)

begin {}

process {
    if (-not $IsDomainUser) {
        # Get local machine groups
        Get-LocalGroup | ForEach-Object {
            $Group = $_.Name
            # Get users in current group
            # Check that $UserName is a member of this current group and output it to StdOut/Write-Output
            Get-LocalGroupMember -Group $Group | Where-Object { $_.Name -like "*$UserName" } | ForEach-Object {
                [PSCustomObject]@{
                    Group = $Group
                    User  = $_.Name
                }
            }
        }
    }
    else {
        if ((Get-Module -Name ActiveDirectory -ListAvailable -ErrorAction SilentlyContinue)) {
            try {
                Import-Module -Name ActiveDirectory
                # Get most of our data needed for the logic, and to reduce the number of time we need to talk to AD
                $ADUser = (Get-ADUser -Identity $UserName -Properties SamAccountName -ErrorAction SilentlyContinue).SamAccountName
            }
            catch {
                Write-Error -Message "Ninja Agent could not access AD, either RSAT was not installed or that the agent does not have permissions to view users and groups."
                exit 5 # Access Denied exit code
            }
            # Get a list of groups that the user is in
            # Loop through each group
            Get-ADGroup -Filter * -ErrorAction SilentlyContinue | ForEach-Object {
                $ADGroup = $_
                # Get users from current group and filter out all other users
                Get-ADGroupMember -Identity $ADGroup -ErrorAction SilentlyContinue | Where-Object {
                    $_.SamAccountName -like $ADUser
                } | ForEach-Object {
                    # Write out to StandardOutput
                    [PSCustomObject]@{
                        Group = $ADGroup.Name
                        User  = $_.SamAccountName
                    }
                }
            }
        }
        else {
            # Throw error that RSAT: ActiveDirectory isn't installed
            Write-Error -Message "RSAT: ActiveDirectory is not installed or not found on this computer. The PowerShell Module called ActiveDirectory is needed to proceed." -RecommendedAction "https://docs.microsoft.com/en-us/powershell/module/activedirectory/"
            exit 2 # File Not Found exit code
        }
    }
}
end {}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

Das Skript lässt sich je nach seiner Funktion in zwei Hauptkomponenten gliedern:

  • Überprüfung der Mitgliedschaft in lokalen Gruppen Sofern der Schalter -IsDomainUser nicht festgelegt ist, ruft das Skript die Gruppen ab, denen die angegebene Person auf dem lokalen Computer angehört, und listet sie auf.
  • Prüfung der Active Directory-Gruppenmitgliedschaft: Ist der Schalter -IsDomainUser aktiviert, greift das Skript auf das Active Directory zu, um die Gruppen aufzulisten, denen der angegebene Benutzer angehört.

Das Skript gibt die Ergebnisse in Form eines PSCustomObject aus, um die Daten strukturiert und leicht verständlich oder für weitere Verarbeitung zugänglich zu machen.

Potenzielle Anwendungsszenarien

Stellen Sie sich einen IT-Experten in einem großen Unternehmen vor, der die Gruppenzugehörigkeiten eines neuen Abteilungsleiters überprüfen muss. Mit diesem Skript kann der IT-Experte mühelos sämtliche Gruppen auflisten, denen der Benutzer sowohl in lokalen als auch in Active Directory-Umgebungen angehört. Das stellt sicher, dass der Abteilungsleiter die korrekten Berechtigungen hat und potenzielle Sicherheitsverletzungen oder Zugriffsfehler verhindert werden.

Alternative Herangehensweise

Die grafische Benutzeroberfläche von „Active Directory Users and Computers“ (ADUC) erlaubt zwar das Ansehen von Gruppenmitgliedschaften, jedoch ist dies eine manuelle Methode und nicht geeignet für Massenüberprüfungen. Unser PowerShell-Skript automatisiert diese Aufgabe, was die Überprüfung von mehreren Benutzern oder regelmäßige Prüfungen effizient gestaltet. Zudem ermöglicht PowerShell eine einfache Ausgabe, Filterung und Integration der Ergebnisse in Berichte, was Flexibilität bietet, die GUI-Tools nicht bieten.

FAQs

  • F: Kann das Skript ohne Administratorrechte ausgeführt werden?
    A: Das Skript erfordert Administratorrechte, um durch den Zugriff auf die erforderlichen Systemressourcen genaue Ergebnisse zu gewährleisten.
  • F: Was ist, wenn das Active Directory-Modul nicht auf dem System gefunden wird?
    A: Das Skript gibt einen Fehler aus, der auf die Installation von RSAT mit der Active Directory-Funktion hinweist.

Auswirkungen

Falsche Gruppenmitgliedschaften können zu unberechtigtem Datenzugriff oder zur Verweigerung des Zugangs zu wichtigen Ressourcen für Benutzer:innen führen. Durch die genaue Bestimmung von Benutzer-Mitgliedschaften mit unserem PowerShell-Skript können IT-Experten die Sicherheit verbessern und die Einhaltung von Vorschriften gewährleisten.

Empfehlungen

  • Führen Sie das Skript immer zuerst in einer sicheren und kontrollierten Umgebung aus.
  • Überprüfen Sie regelmäßig die Mitgliedschaften in Benutzergruppen, insbesondere bei Rollen mit hohen Rechten.
  • Integrieren Sie die Skriptergebnisse in eine umfassendere IAM-Strategie (Identity and Access Management).

Abschließende Überlegungen

NinjaOne bietet umfassende IT-Lösungen für die Automatisierung, Verwaltung und Sicherheit von IT-Umgebungen. Durch die Kombination von Skripten wie dem zuvor besprochenen und den Funktionen von NinjaOne können IT-Experten ihre Effizienz, Genauigkeit und Sicherheit steigern. Diese Zusammenarbeit ermöglicht einen proaktiven Ansatz in der IT-Verwaltung und -Sicherheit.

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