Saltar al contenido
Microsoft scripts

Scripts de Intune

Cuentas administradoras locales en Mac

Lista quién pertenece al grupo de administradores locales de cada Mac, con el conteo.

Antes de correrloSolo lista, no quita a nadie. Si responde "Permission denied", revisa que el script corra como root.

Ejemplo en México

Despacho jurídico en Aguascalientes, 22 Mac

Varios abogados se dieron permisos de administrador para instalar software y la socia pidió revisarlo.

local-admins.sh

Encuentra 9 Mac con 3 o más administradores y regulariza los permisos.

Cómo implementarlo

  1. En el centro de administración de Microsoft Intune ve a Dispositivos > Por plataforma > macOS > Organizar dispositivos > Atributos personalizados para macOS (Custom attributes for macOS) > Agregar.
  2. Ponle nombre al perfil, elige el tipo de dato String y sube el archivo local-admins.sh.
  3. Asígnalo a un grupo piloto de Mac. Intune ejecuta el script cada 8 horas y reporta lo que imprime.
  4. Compara contra tu lista de administradores autorizados y quita los permisos sobrantes desde Intune o con un perfil.
Ver el código completo de local-admins.sh
#!/bin/bash

# TITLE: List Local Admin Users
# SYNOPSIS: Lists all users with local administrator privileges on macOS
# DESCRIPTION: This script retrieves the list of users who are members of the local admin
#              group on macOS. It uses the Directory Service command line utility (dscl)
#              to query the admin group membership. The output is formatted for Intune
#              custom attributes to provide visibility into privileged access on managed devices.
# TAGS: Monitoring,Security
# PLATFORM: macOS
# MIN_OS_VERSION: 10.15
# AUTHOR: Ugur Koc
# VERSION: 1.0
# LASTUPDATE: 2025-06-04
# CHANGELOG:
#   1.0 - Initial release
#
# EXAMPLE:
#   ./local-admins.sh
#   Outputs the list of local admin users
#
# NOTES:
#   - Uses dscl to query admin group membership
#   - No external dependencies required
#   - Designed for Intune custom attributes (single line output)
#   - Includes count of admin users for quick assessment
#   - For more scripts and guides, visit: IntuneMacAdmins.com

# ============================================================================
# VARIABLES AND INITIALIZATION
# ============================================================================

# ============================================================================
# FUNCTIONS
# ============================================================================

# Function to output result (for Intune custom attributes)
output_result() {
    # For Intune custom attributes, output should be a single line
    echo "$1"
    exit 0
}

# Function to check prerequisites
check_prerequisites() {
    if ! command -v dscl >/dev/null 2>&1; then
        output_result "Error: dscl command not found"
    fi
}

# Function to get admin users
get_admin_users() {
    # Query the admin group membership
    local admin_output
    if ! admin_output=$(dscl . -read /Groups/admin GroupMembership 2>&1); then
        # Check if it's a permission issue
        if echo "$admin_output" | grep -q "eDSPermissionError"; then
            output_result "Error: Permission denied"
        else
            output_result "Error: Unable to query admin group"
        fi
    fi

    # Extract just the user list (remove "GroupMembership:" prefix)
    local admin_list
    admin_list=${admin_output#GroupMembership: }

    # Check if we got valid output
    if [[ -z "$admin_list" ]] || [[ "$admin_list" == "$admin_output" ]]; then
        output_result "Error: No admin users found or invalid format"
    fi

    echo "$admin_list"
}

# ============================================================================
# MAIN SCRIPT LOGIC
# ============================================================================

main() {
    # Check prerequisites
    check_prerequisites

    # Get the list of admin users
    local admin_users
    admin_users=$(get_admin_users)

    # Count the number of admin users
    local admin_count
    admin_count=$(echo "$admin_users" | wc -w | tr -d ' ')

    # Format output for Intune custom attributes
    if [[ $admin_count -eq 0 ]]; then
        output_result "Admin Users: None found"
    elif [[ $admin_count -eq 1 ]]; then
        output_result "Admin Users (1): $admin_users"
    else
        # For multiple users, show count and list
        output_result "Admin Users ($admin_count): $admin_users"
    fi
}

# ============================================================================
# ERROR HANDLING
# ============================================================================

# For Intune custom attributes - handle errors gracefully
trap 'output_result "Error: Script failed"' ERR

# ============================================================================
# SCRIPT EXECUTION
# ============================================================================

# Only run main if script is executed directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi

# Exit successfully (if not using output_result)
exit 0

# Copia sin cambios de https://github.com/ugurkocde/IntuneAutomation (commit 797bb2e).
# MIT License
#
# Copyright (c) 2025 Ugur Koc | Microsoft MVP
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

Relacionados