Demo ansehen×
×

Sehen Sie NinjaOne in Aktion!

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

Verwenden von PowerShell zum Hinzufügen oder Entfernen von Benutzer:innen aus Active Directory & Lokale Computergruppen

Die Verwaltung und Änderung der Mitgliedschaft von Benutzer:innen in Gruppen, sei es auf einem lokalen Computer oder in Active Directory, ist eine häufige Aufgabe für IT-Expert:innen. Eine effiziente Abwicklung dieser Vorgänge kann die Systemverwaltung erheblich verbessern und die Prozesse straffen und fehlerfrei gestalten. In diesem Zusammenhang kommt die Leistungsfähigkeit der Skripterstellung zum Tragen, die Automatisierung und Präzision bietet.

Hintergrund

Das mitgelieferte Skript taucht tief in das Wesen der IT-Verwaltung ein und ermöglicht es Administratoren, Benutzer:innen zu bestimmten Gruppen hinzuzufügen oder zu entfernen. Es ist auf Vielseitigkeit ausgelegt und funktioniert sowohl im Bereich eines lokalen Computers als auch in der breiteren Sphäre von Active Directory. Wenn Unternehmen und Managed Service Provider (MSPs) wachsen, kann die manuelle Benutzerverwaltung mühsam werden. Solche Skripte reduzieren nicht nur den Zeitaufwand für Routineaufgaben, sondern minimieren auch menschliche Fehler.

Das Skript

#Requires -Version 2.0

<#
.SYNOPSIS
    Add or remove a user to a group in Active Directory or the local computer.
.DESCRIPTION
    Add or remove a user to a group in Active Directory or the local computer.
.EXAMPLE
     -Group "MyGroup" -UserName "MyUser" -Action Add -IsDomainUser
    Adds MyUser to the group MyGroup in AD.
.EXAMPLE
     -Group "MyGroup" -UserName "MyUser" -Action Remove -IsDomainUser
    Removes MyUser from the group MyGroup in AD.
.EXAMPLE
     -Group "MyGroup" -UserName "MyUser" -Action Add
    Adds MyUser to the group MyGroup on the local computer.
.EXAMPLE
    PS C:> Modify-User-Membership.ps1 -Group "MyGroup" -UserName "MyUser" -Action Remove
    Removes MyUser from the group MyGroup on the local computer.
.OUTPUTS
    String[]
.NOTES
    Minimum OS Architecture Supported: Windows 7, Windows Server 2012
    This will require RSAT with the AD feature to be installed to function.
    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/de/nutzungsbedingungen
    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 Group
    [Parameter(Mandatory = $true)]
    [String]
    $Group,
    # Specify one User
    [Parameter(Mandatory = $true)]
    [String]
    $UserName,
    # Add or Remove user from group
    [Parameter(Mandatory = $true)]
    [ValidateSet("Add", "Remove")]
    [String]
    $Action,
    # Modify a domain user's membership
    [Parameter(Mandatory = $false)]
    [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 (-not $IsDomainUser) {
        # Modify Local User
        if ($Action -like "Remove") {
            if ($PSVersionTable.PSVersion.Major -lt 3) {
                # Connect to localhost
                try {
                    $ADSI = [ADSI]("WinNT://$env:COMPUTERNAME")
                }
                catch {
                    Write-Error -Message "Failed to connect to $env:COMPUTERNAME via ADSI object"
                    exit 1
                }
                # Find the group
                try {
                    $ASDIGroup = $ADSI.Children.Find($Group, 'group')
                }
                catch {
                    Write-Error -Message "Failed to find $Group via ADSI object"
                    exit 1
                }
                # Remove the user from the group
                try {
                    $ASDIGroup.Remove(("WinNT://$env:COMPUTERNAME/$UserName"))
                }
                catch {
                    Write-Error -Message "Failed to remove User $UserName from Group $Group"
                    exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                }
                
            }
            else {
                if (
                    # Check that the group exists
                (Get-LocalGroup -Name $Group -ErrorAction SilentlyContinue) -and
                    # Check that the user exists in the group
                (Get-LocalGroupMember -Group $Group -Member $UserName -ErrorAction SilentlyContinue)
                ) {
                    Write-Output "Found $UserName in Group $Group, removing."
                    try {
                        # Remove user from Group, -Confirm:$false used to not prompt and stop the script
                        Remove-LocalGroupMember -Group $Group -Member $UserName -Confirm:$false
                        Write-Output "Removed User $UserName from Group $Group"
                    }
                    catch {
                        Write-Error -Message "Failed to remove User $UserName from Group $Group"
                        exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                    }
                }
                elseif (-not (Get-LocalGroup -Name $Group -ErrorAction SilentlyContinue)) {
                    Write-Error -Message "Group $Group does not exist"
                    exit 528 # ERROR_NO_SUCH_GROUP
                }
                elseif (-not (Get-LocalGroupMember -Group $Group -Member $UserName -ErrorAction SilentlyContinue)) {
                    Write-Error -Message "User does not exist in Group $Group"
                    exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                }
            }
            
        }
        elseif ($Action -like "Add") {
            if ($PSVersionTable.PSVersion.Major -lt 3) {
                # Connect to localhost
                try {
                    $ADSI = [ADSI]("WinNT://$env:COMPUTERNAME")
                }
                catch {
                    Write-Error -Message "Failed to connect to $env:COMPUTERNAME via ADSI object"
                    exit 1
                }
                # Find the group
                try {
                    $ASDIGroup = $ADSI.Children.Find($Group, 'group')
                }
                catch {
                    Write-Error -Message "Failed to find $Group via ADSI object"
                    exit 1
                }
                # Get the members of the group
                $GroupResults = try {
                    $ASDIGroup.psbase.invoke('members')  | ForEach-Object {
                        $_.GetType().InvokeMember("Name", "GetProperty", $Null, $_, $Null)
                    }
                }
                catch {
                    $null
                }
                # Check if the user is in the group
                if ($UserName -in $GroupResults) {
                    # User already in Group
                    Write-Output "User $UserName already in Group $Group"
                    exit 1320 # ERROR_MEMBER_IN_GROUP
                }
                else {
                    # User not in group, add them to the group
                    try {
                        $ASDIGroup.Add(("WinNT://$env:COMPUTERNAME/$UserName"))
                    }
                    catch {
                        Write-Error -Message "Failed to add User $UserName to Group $Group"
                        exit 1388 # ERROR_INVALID_MEMBER
                    }
                    
                    # We can verify the membership by running the following  command:
                    if ($UserName -in (
                            $ASDIGroup.psbase.invoke('members')  | ForEach-Object {
                                $_.GetType().InvokeMember("Name", "GetProperty", $Null, $_, $Null)
                            }
                        )
                    ) {
                        # User in Group
                        Write-Output "Added User $UserName to Group $Group"
                    }
                    else {
                        Write-Error -Message "Failed to add User $UserName to Group $Group"
                        exit 1388 # ERROR_INVALID_MEMBER
                    }
                }
                
            }
            else {
                # Verify that the user and group exist
                if (
                    # Check that the user exists
                (Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue) -and
                    # Check that the group exists
                (Get-LocalGroup -Name $Group -ErrorAction SilentlyContinue)
                ) {
                    # Check if user is already in group
                    if (-not (Get-LocalGroupMember -Group $Group -Member $UserName -ErrorAction SilentlyContinue)) {
                        # User not in group, good to add
                        try {
                            # Add user to group
                            Add-LocalGroupMember -Group $Group -Member (Get-LocalUser -Name $UserName)
                            Write-Output "Added User $UserName to Group $Group"
                        }
                        catch {
                            Write-Error -Message "Failed to add User $UserName to Group $Group"
                            exit 1388 # ERROR_INVALID_MEMBER
                        }
                    }
                    else {
                        # User already in Group
                        Write-Output "User $UserName already in Group $Group"
                        exit 1320 # ERROR_MEMBER_IN_GROUP
                    }
                
                }
            }
        }
    }
    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
                $ADGroup = Get-ADGroup -Identity $Group -ErrorAction SilentlyContinue
                $ADInGroup = Get-ADGroupMember -Identity $Group -ErrorAction SilentlyContinue | Where-Object { $_.SamAccountName -like $ADUser }
            }
            catch {
                Write-Error -Message "Ninja Agent could not access AD, please check that the agent has permissions to add and remove users from groups."
                exit 5 # Access Denied exit code
            }
            
            # Modify AD User
            if ($Action -like "Remove") {
                # Verify that the user and group exist, and if the user is in the group
                if (
                    $ADUser -and
                    # Check that the group exists
                    $ADGroup -and
                    # Check that the user exists in the group
                    $ADInGroup
                ) {
                    Write-Output "Found $UserName in Group $Group, removing."
                    try {
                        # Remove user from Group, -Confirm:$false used to not prompt and stop the script
                        Remove-ADGroupMember -Identity $Group -Members $ADUser -Confirm:$false
                        Write-Output "Removed User $UserName from Group $Group"
                    }
                    catch {
                        Write-Error -Message "Failed to remove User $UserName from Group $Group"
                        exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                    }
                }
                elseif (-not $ADGroup) {
                    Write-Error -Message "Group $Group does not exist"
                    exit 528 # ERROR_NO_SUCH_GROUP
                }
                elseif (-not $ADInGroup) {
                    Write-Error -Message "User does not exist in Group $Group"
                    exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                }
            }
            elseif ($Action -like "Add") {
                # Verify that the user and group exist
                if (
                    # Check that the user exists
                    $ADUser -and
                    # Check that the group exists
                    $ADGroup
                ) {
                    # Check if user is already in group
                    if (-not $ADInGroup) {
                        # User not in group, good to add
                        try {
                            # Add user to group
                            Add-ADGroupMember -Identity $Group -Members $ADUser
                            Write-Output "Added User $UserName to Group $Group"
                        }
                        catch {
                            Write-Error -Message "Failed to add User $UserName to Group $Group"
                            exit 1388 # ERROR_INVALID_MEMBER
                        }
                    }
                    else {
                        # User already in Group
                        Write-Output "User $UserName already in Group $Group"
                        exit 1320 # ERROR_MEMBER_IN_GROUP
                    }
                }
            }
        }
        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/?view=windowsserver2019-ps"
            exit 2 # File Not Found exit code
        }
    }
}

end {}

|

#Requires -Version 2.0

<#
.SYNOPSIS
    Add or remove a user to a group in Active Directory or the local computer.
.DESCRIPTION
    Add or remove a user to a group in Active Directory or the local computer.
.EXAMPLE
     -Group "MyGroup" -UserName "MyUser" -Action Add -IsDomainUser
    Adds MyUser to the group MyGroup in AD.
.EXAMPLE
     -Group "MyGroup" -UserName "MyUser" -Action Remove -IsDomainUser
    Removes MyUser from the group MyGroup in AD.
.EXAMPLE
     -Group "MyGroup" -UserName "MyUser" -Action Add
    Adds MyUser to the group MyGroup on the local computer.
.EXAMPLE
    PS C:> Modify-User-Membership.ps1 -Group "MyGroup" -UserName "MyUser" -Action Remove
    Removes MyUser from the group MyGroup on the local computer.
.OUTPUTS
    String[]
.NOTES
    Minimum OS Architecture Supported: Windows 7, Windows Server 2012
    This will require RSAT with the AD feature to be installed to function.
    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 Group
    [Parameter(Mandatory = $true)]
    [String]
    $Group,
    # Specify one User
    [Parameter(Mandatory = $true)]
    [String]
    $UserName,
    # Add or Remove user from group
    [Parameter(Mandatory = $true)]
    [ValidateSet("Add", "Remove")]
    [String]
    $Action,
    # Modify a domain user's membership
    [Parameter(Mandatory = $false)]
    [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 (-not $IsDomainUser) {
        # Modify Local User
        if ($Action -like "Remove") {
            if ($PSVersionTable.PSVersion.Major -lt 3) {
                # Connect to localhost
                try {
                    $ADSI = [ADSI]("WinNT://$env:COMPUTERNAME")
                }
                catch {
                    Write-Error -Message "Failed to connect to $env:COMPUTERNAME via ADSI object"
                    exit 1
                }
                # Find the group
                try {
                    $ASDIGroup = $ADSI.Children.Find($Group, 'group')
                }
                catch {
                    Write-Error -Message "Failed to find $Group via ADSI object"
                    exit 1
                }
                # Remove the user from the group
                try {
                    $ASDIGroup.Remove(("WinNT://$env:COMPUTERNAME/$UserName"))
                }
                catch {
                    Write-Error -Message "Failed to remove User $UserName from Group $Group"
                    exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                }
                
            }
            else {
                if (
                    # Check that the group exists
                (Get-LocalGroup -Name $Group -ErrorAction SilentlyContinue) -and
                    # Check that the user exists in the group
                (Get-LocalGroupMember -Group $Group -Member $UserName -ErrorAction SilentlyContinue)
                ) {
                    Write-Output "Found $UserName in Group $Group, removing."
                    try {
                        # Remove user from Group, -Confirm:$false used to not prompt and stop the script
                        Remove-LocalGroupMember -Group $Group -Member $UserName -Confirm:$false
                        Write-Output "Removed User $UserName from Group $Group"
                    }
                    catch {
                        Write-Error -Message "Failed to remove User $UserName from Group $Group"
                        exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                    }
                }
                elseif (-not (Get-LocalGroup -Name $Group -ErrorAction SilentlyContinue)) {
                    Write-Error -Message "Group $Group does not exist"
                    exit 528 # ERROR_NO_SUCH_GROUP
                }
                elseif (-not (Get-LocalGroupMember -Group $Group -Member $UserName -ErrorAction SilentlyContinue)) {
                    Write-Error -Message "User does not exist in Group $Group"
                    exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                }
            }
            
        }
        elseif ($Action -like "Add") {
            if ($PSVersionTable.PSVersion.Major -lt 3) {
                # Connect to localhost
                try {
                    $ADSI = [ADSI]("WinNT://$env:COMPUTERNAME")
                }
                catch {
                    Write-Error -Message "Failed to connect to $env:COMPUTERNAME via ADSI object"
                    exit 1
                }
                # Find the group
                try {
                    $ASDIGroup = $ADSI.Children.Find($Group, 'group')
                }
                catch {
                    Write-Error -Message "Failed to find $Group via ADSI object"
                    exit 1
                }
                # Get the members of the group
                $GroupResults = try {
                    $ASDIGroup.psbase.invoke('members')  | ForEach-Object {
                        $_.GetType().InvokeMember("Name", "GetProperty", $Null, $_, $Null)
                    }
                }
                catch {
                    $null
                }
                # Check if the user is in the group
                if ($UserName -in $GroupResults) {
                    # User already in Group
                    Write-Output "User $UserName already in Group $Group"
                    exit 1320 # ERROR_MEMBER_IN_GROUP
                }
                else {
                    # User not in group, add them to the group
                    try {
                        $ASDIGroup.Add(("WinNT://$env:COMPUTERNAME/$UserName"))
                    }
                    catch {
                        Write-Error -Message "Failed to add User $UserName to Group $Group"
                        exit 1388 # ERROR_INVALID_MEMBER
                    }
                    
                    # We can verify the membership by running the following  command:
                    if ($UserName -in (
                            $ASDIGroup.psbase.invoke('members')  | ForEach-Object {
                                $_.GetType().InvokeMember("Name", "GetProperty", $Null, $_, $Null)
                            }
                        )
                    ) {
                        # User in Group
                        Write-Output "Added User $UserName to Group $Group"
                    }
                    else {
                        Write-Error -Message "Failed to add User $UserName to Group $Group"
                        exit 1388 # ERROR_INVALID_MEMBER
                    }
                }
                
            }
            else {
                # Verify that the user and group exist
                if (
                    # Check that the user exists
                (Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue) -and
                    # Check that the group exists
                (Get-LocalGroup -Name $Group -ErrorAction SilentlyContinue)
                ) {
                    # Check if user is already in group
                    if (-not (Get-LocalGroupMember -Group $Group -Member $UserName -ErrorAction SilentlyContinue)) {
                        # User not in group, good to add
                        try {
                            # Add user to group
                            Add-LocalGroupMember -Group $Group -Member (Get-LocalUser -Name $UserName)
                            Write-Output "Added User $UserName to Group $Group"
                        }
                        catch {
                            Write-Error -Message "Failed to add User $UserName to Group $Group"
                            exit 1388 # ERROR_INVALID_MEMBER
                        }
                    }
                    else {
                        # User already in Group
                        Write-Output "User $UserName already in Group $Group"
                        exit 1320 # ERROR_MEMBER_IN_GROUP
                    }
                
                }
            }
        }
    }
    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
                $ADGroup = Get-ADGroup -Identity $Group -ErrorAction SilentlyContinue
                $ADInGroup = Get-ADGroupMember -Identity $Group -ErrorAction SilentlyContinue | Where-Object { $_.SamAccountName -like $ADUser }
            }
            catch {
                Write-Error -Message "Ninja Agent could not access AD, please check that the agent has permissions to add and remove users from groups."
                exit 5 # Access Denied exit code
            }
            
            # Modify AD User
            if ($Action -like "Remove") {
                # Verify that the user and group exist, and if the user is in the group
                if (
                    $ADUser -and
                    # Check that the group exists
                    $ADGroup -and
                    # Check that the user exists in the group
                    $ADInGroup
                ) {
                    Write-Output "Found $UserName in Group $Group, removing."
                    try {
                        # Remove user from Group, -Confirm:$false used to not prompt and stop the script
                        Remove-ADGroupMember -Identity $Group -Members $ADUser -Confirm:$false
                        Write-Output "Removed User $UserName from Group $Group"
                    }
                    catch {
                        Write-Error -Message "Failed to remove User $UserName from Group $Group"
                        exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                    }
                }
                elseif (-not $ADGroup) {
                    Write-Error -Message "Group $Group does not exist"
                    exit 528 # ERROR_NO_SUCH_GROUP
                }
                elseif (-not $ADInGroup) {
                    Write-Error -Message "User does not exist in Group $Group"
                    exit 529 # ERROR_MEMBER_NOT_IN_GROUP
                }
            }
            elseif ($Action -like "Add") {
                # Verify that the user and group exist
                if (
                    # Check that the user exists
                    $ADUser -and
                    # Check that the group exists
                    $ADGroup
                ) {
                    # Check if user is already in group
                    if (-not $ADInGroup) {
                        # User not in group, good to add
                        try {
                            # Add user to group
                            Add-ADGroupMember -Identity $Group -Members $ADUser
                            Write-Output "Added User $UserName to Group $Group"
                        }
                        catch {
                            Write-Error -Message "Failed to add User $UserName to Group $Group"
                            exit 1388 # ERROR_INVALID_MEMBER
                        }
                    }
                    else {
                        # User already in Group
                        Write-Output "User $UserName already in Group $Group"
                        exit 1320 # ERROR_MEMBER_IN_GROUP
                    }
                }
            }
        }
        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/?view=windowsserver2019-ps"
            exit 2 # File Not Found exit code
        }
    }
}

end {}

 

Zugriff auf über 300 Skripte im NinjaOne Dojo

Zugang erhalten

Detailansicht

Das Skript beginnt mit einem umfassenden Kommentar, der einen Einblick in seine Funktionen, Beispiele und Anforderungen bietet. Es werden wesentliche Parameter wie Group, UserName, Action und ein optionaler IsDomainUser-Schalter definiert, die die Hauptlogik steuern. Eine Hilfsfunktion, Test-IsElevated, prüft, ob das Skript mit administrativen Rechten ausgeführt wird. Die Hauptlogik beginnt mit einer Berechtigungsprüfung, gefolgt davon, ob die Aufgabe einen lokalen Benutzer oder einen Active Directory-Benutzer betrifft. Abhängig von der PowerShell-Version und der gewünschten Aktion (Hinzufügen/Entfernen) verfügt das Skript über eine Schnittstelle zu Active Directory Service Interfaces (ADSI) oder zu einer native PowerShell-Cmdlets. Für Active Directory-Benutzer:innen wird das Active Directory-Modul verwendet, das eine nahtlose Integration und Verwaltung bietet.

Potenzielle Anwendungsfälle

Fallstudie:

Sarah, eine IT-Administratorin in einem wachsenden Unternehmen, muss 50 neue Mitarbeiter:innen einarbeiten. Da die Abteilungen und Rollen variieren, wäre eine manuelle Zuordnung der Benutzer:innen zu den jeweiligen AD-Gruppen sehr zeitaufwändig. Mithilfe dieses Skripts weist Sarah die Benutzer:innen schnell den jeweiligen Gruppen zu und stellt sicher, dass die Zugriffskontrollen effizient durchgesetzt werden. Bei den vierteljährlichen IT-Audits verwendet sie das Skript auch, um Benutzer:innen aus bestimmten Gruppen oder von den lokalen Rechnern gekündigter Mitarbeiter:innen zu entfernen.

Vergleiche

Die herkömmliche Benutzerverwaltung basiert in der Regel auf GUI-basierten Tools wie Active Directory Users and Computers (ADUC) oder Computer Management für lokale Benutzer:innen. Sie sind zwar benutzerfreundlich, aber für Massenoperationen nicht effizient. Dieses Skript, das PowerShell nutzt, sorgt dafür, dass Aufgaben, die sonst Stunden dauern, auf wenige Minuten reduziert werden. Im Gegensatz zu GUI-Tools, die visuelles Feedback geben, muss das Skript jedoch gründlich getestet werden, um sicherzustellen, dass keine unbeabsichtigten Aktionen auftreten.

FAQs

  • Kann ich dieses Skript in älteren Versionen von PowerShell verwenden?
    Ja, das Skript unterstützt PowerShell-Versionen, die älter als 2.0 sind. Die Funktionalität kann jedoch je nach Version variieren.
  • Ist das Modul Active Directory zwingend erforderlich?
    Für Aktionen, die Domänenbenutzer betreffen, ist das Modul Active Directory erforderlich.
  • Wie stelle ich sicher, dass ich die erforderlichen Verwaltungsrechte habe?
    Das Skript enthält integrierte Überprüfungen auf administrative Rechte und gibt eine Fehlermeldung aus, wenn es nicht mit den erforderlichen Rechten ausgeführt wird.

Auswirkungen

Das Skript kann Fehler bei der Verwaltung von Benutzergruppen drastisch reduzieren, was erhebliche Auswirkungen auf die IT-Sicherheit haben kann. Indem sichergestellt wird, dass die Benutzer:innen nur den erforderlichen Gruppen angehören, wird das Prinzip der geringsten Privilegien durchgesetzt, ein Eckpfeiler der IT-Sicherheit. Mit der Automatisierung geht jedoch auch die Verantwortung einher, sicherzustellen, dass Skripte nicht versehentlich einen übermäßigen Zugang gewähren und damit potenziell die Tür für Sicherheitsverletzungen öffnen.

Empfehlungen

  • Testen Sie das Skript immer in einer kontrollierten Umgebung, bevor Sie es in der Produktion einsetzen.
  • Führen Sie ein Protokoll über alle mit dem Skript vorgenommenen Änderungen zu Prüfzwecken.
  • Stellen Sie sicher, dass Sie über Backups von Active Directory oder lokalen Benutzerdatenbanken verfügen, um unbeabsichtigte Änderungen rückgängig machen zu können.

Abschließende Überlegungen

Da die IT-Umgebungen immer komplexer werden, sind Tools wie NinjaOne für die Bereitstellung umfassender Lösungen unerlässlich. Für Aufgaben wie die Verwaltung von Benutzergruppen können Skripte wie das oben beschriebene in Plattformen wie NinjaOne integriert werden. Dadurch wird sichergestellt, dass IT-Administratoren die besten Werkzeuge zur Verfügung stehen, um Prozesse zu automatisieren und zu rationalisieren und gleichzeitig optimale Sicherheit zu gewährleisten.

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