Saltar al contenido
Microsoft scripts

Scripts de Intune

Actualizaciones pendientes de Office en Mac

Pregunta a Microsoft AutoUpdate qué actualizaciones de Office y otras apps de Microsoft están pendientes y lo reporta en una sola línea por Mac.

Antes de correrloEjecuta msupdate como el usuario con sesión iniciada; sin nadie en sesión reporta "Error: No user logged in". Requiere Microsoft AutoUpdate instalado.

Ejemplo en México

Agencia de publicidad en Guadalajara, 30 Mac con Office

Seguridad pidió que Word y Excel estén al día y TI no sabe cuántas Mac están atrasadas.

check-available-msupdate-updates.sh

Descubre 12 Mac con actualizaciones pendientes y las incluye en el siguiente despliegue.

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 check-available-msupdate-updates.sh.
  3. Asígnalo a un grupo piloto de Mac. Intune ejecuta el script cada 8 horas y reporta lo que imprime.
  4. Filtra los equipos con "Updates available" y empuja la actualización de Office por Intune o pide al usuario abrir MAU.
Ver el código completo de check-available-msupdate-updates.sh
#!/bin/bash

# TITLE: Check Available Microsoft Updates
# SYNOPSIS: Checks for available updates for Microsoft applications via MAU
# DESCRIPTION: This script uses Microsoft AutoUpdate (MAU) to check for available updates
#              for Microsoft Office applications and other Microsoft software on macOS.
#              It runs the msupdate command in the context of the logged-in user to ensure
#              proper access to user-specific update information. Results are formatted
#              for Intune custom attributes to provide visibility into pending updates.
# TAGS: Monitoring,Updates
# PLATFORM: macOS
# MIN_OS_VERSION: 10.15
# AUTHOR: Ugur Koc
# VERSION: 1.0
# LASTUPDATE: 2025-06-04
# CHANGELOG:
#   1.0 - Initial release
#
# EXAMPLE:
#   ./check-available-msupdate-updates.sh
#   Checks for available Microsoft application updates and outputs the list
#
# NOTES:
#   - Requires Microsoft AutoUpdate to be installed on the device
#   - Runs msupdate in the context of the logged-in user
#   - Designed for Intune custom attributes (single line output)
#   - Returns "No updates available" or lists available updates
#   - For more scripts and guides, visit: IntuneMacAdmins.com

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

# Path to msupdate CLI
MSUPDATE="/Library/Application Support/Microsoft/MAU2.0/Microsoft AutoUpdate.app/Contents/MacOS/msupdate"

# Get the current console user (if needed)
loggedInUser=$(stat -f "%Su" /dev/console 2>/dev/null)

# ============================================================================
# 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 if MAU is installed
check_mau_installed() {
    if [[ ! -f "$MSUPDATE" ]]; then
        output_result "Microsoft AutoUpdate not installed"
    fi
}

# Function to check if msupdate is executable
check_msupdate_executable() {
    if [[ ! -x "$MSUPDATE" ]]; then
        output_result "Error: msupdate not executable"
    fi
}

# Function to validate user context
validate_user_context() {
    if [ -z "$loggedInUser" ] || [ "$loggedInUser" = "root" ] || [ "$loggedInUser" = "_windowserver" ]; then
        output_result "Error: No user logged in"
    fi
}

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

main() {
    # Check if MAU is installed
    check_mau_installed

    # Check if msupdate is executable
    check_msupdate_executable

    # Validate user context
    validate_user_context

    # Get user ID for the logged-in user
    local user_id
    user_id=$(id -u "$loggedInUser" 2>/dev/null)

    if [[ -z "$user_id" ]]; then
        output_result "Error: Unable to get user ID"
    fi

    # Run msupdate using the user's launchctl session
    local raw_output
    if ! raw_output=$(launchctl asuser "$user_id" sudo -u "$loggedInUser" "$MSUPDATE" --list 2>&1); then
        # Check for specific error messages
        if echo "$raw_output" | grep -q "Failed to connect"; then
            output_result "Error: Failed to connect to MAU service"
        else
            output_result "Error: Unable to check for updates"
        fi
    fi

    # Check if "No updates available" is in the output
    if echo "$raw_output" | grep -q "No updates available"; then
        output_result "No updates available"
    else
        # Process available updates for single-line output
        # Extract update information and format for Intune
        local updates
        updates=$(echo "$raw_output" | grep -E "^\s*[A-Za-z]" | grep -v "Updates available:" | tr '\n' ' ' | sed 's/  */ /g' | xargs)

        if [[ -n "$updates" ]]; then
            output_result "Updates available: $updates"
        else
            output_result "Updates available (check MAU for details)"
        fi
    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