Une gestion efficace de l’énergie est essentielle pour les professionnels de l’informatique et les fournisseurs de services gérés (MSP) chargés de la maintenance des systèmes macOS. Ce script Bash personnalisé simplifie le processus de gestion des paramètres d’alimentation macOS, permettant aux utilisateurs d’ajuster des paramètres tels que la minuterie de veille, le délai pour la mise en veille et les fonctions d’économie d’énergie. Cet article plonge dans les fonctionnalités, les cas d’utilisation et les implications de l’utilisation de ce script.
Comprendre la gestion de l’énergie dans macOS
La gestion de l’alimentation permet aux systèmes d’équilibrer les performances et l’efficacité énergétique, en prolongeant la durée de vie de la batterie et en minimisant l’utilisation des ressources inutiles. Bien que macOS propose des outils natifs tels que la commande pmset, la configuration manuelle de ces paramètres peut s’avérer longue et sujette à des erreurs, en particulier dans les environnements comportant de nombreux appareils. Ce script permet une approche rationalisée de la gestion des paramètres d’alimentation, en fournissant un contrôle précis ¤sur différents paramètres.
Le script :
#!/usr/bin/env bash # # Description: Set Power and Sleep settings. It can adjust either the plugged-in or battery settings if requested. Please note, not all devices support all options. # 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). # # Preset Parameter: --powerSourceSetting "Both" # Should these changes apply when the device is plugged in, on battery, or both? # # Preset Parameter: --screenTimeout "10" # Time in minutes to wait before turning off the screen. (0 to disable) # # Preset Parameter: --sleepTimeout "10" # Time in minutes to wait before the computer sleeps. (0 to disable) # # Preset Parameter: --diskTimeout "10" # Time in minutes for the disk to spin down when idle. (0 to disable) # # Preset Parameter: --powernap "Disable" # When the Mac goes to sleep, Power Nap activates periodically to update information such as Mail, Calendar, and other iCloud events. # # Preset Parameter: --terminalKeepAlive "Enable" # Prevents the system from going to sleep when a terminal session (like a remote login) is active, only allowing sleep if the session has been idle for too long. # # Preset Parameter: --dimOnBattery "Enable" # Slightly turn down the display brightness when switching to battery power. # # Preset Parameter: --lowPowerMode "Enable" # Reduces energy consumption by lowering the system performance and brightness, stopping background processes, and adjusting system settings to extend battery life. # # Preset Parameter: --tcpKeepAlive "Enable" # Ensures that a network connection remains active by periodically sending keepalive packets to prevent the connection from being dropped due to inactivity. # # Preset Parameter: --wakeOnNetwork "Only on Power Adapter" # Wake when an Ethernet magic packet is received. # # Preset Parameter: --help # Displays some help text. # # Release Notes: Initial Release # Define initial argument variables with default values _arg_powerSourceSetting="Both" _arg_screenTimeout= _arg_sleepTimeout= _arg_diskTimeout= _arg_powernap= _arg_terminalKeepAlive= _arg_dimOnBattery= _arg_wakeOneNetwork= _arg_lowPowerMode= _arg_tcpKeepAlive= # Function to display help menu print_help() { printf '\n\n%s\n\n' 'Usage: [--powerSourceSetting|-p <arg>] [--someSwitch|-s] [--help|-h]' printf '%s\n' 'Preset Parameter: --powerSourceSetting "Both"' printf '\t%s\n' "Should these changes apply when the device is plugged in, on battery, or both?" printf '%s\n' 'Preset Parameter: --screenTimeout "10"' printf '\t%s\n' "Time in minutes to wait before turning off the screen. (0 to disable)" printf '%s\n' 'Preset Parameter: --sleepTimeout "10"' printf '\t%s\n' "Time in minutes to wait before the computer sleeps. (0 to disable)" printf '%s\n' 'Preset Parameter: --diskTimeout "10"' printf '\t%s\n' "Time in minutes for the disk to spin down when idle. (0 to disable)" printf '%s\n' 'Preset Parameter: --powernap "Disable"' printf '\t%s\n' "When the Mac goes to sleep, Power Nap activates periodically to update information such as Mail, Calendar and other iCloud events." printf '%s\n' 'Preset Parameter: --terminalKeepAlive "Enable"' printf '\t%s\n' "Prevents the system from going to sleep when a terminal session (like a remote login) is active, only allowing sleep if the session has been idle for too long." printf '%s\n' 'Preset Parameter: --dimOnBattery "Enable"' printf '\t%s\n' "Slightly turn down the display brightness when switching to battery power." printf '%s\n' 'Preset Parameter: --lowPowerMode "Enable"' printf '\t%s\n' "Reduces energy consumption by lowering the system performance and brightness, stopping background processes, and adjusting system settings to extend battery life." printf '%s\n' 'Preset Parameter: --tcpKeepAlive "Enable"' printf '\t%s\n' "Ensures that a network connection remains active by periodically sending keepalive packets to prevent the connection from being dropped due to inactivity." printf '%s\n' 'Preset Parameter: --wakeOnNetwork "Only on Power Adapter"' printf '\t%s\n' "Wake when an Ethernet magic packet is received." printf '\n%s\n' 'Preset Parameter: --help' printf '\t%s\n' "Displays this help menu." } # Function to display error message and exit the script die() { local _ret="${2:-1}" echo "$1" >&2 test "${_PRINT_HELP:-no}" = yes && print_help >&2 exit "${_ret}" } # Function to parse command-line arguments parse_commandline() { while test $# -gt 0; do _key="$1" case "$_key" in --powerSourceSetting | --powersourcesetting | --powersource) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_powerSourceSetting=$2 shift ;; --powerSourceSetting=*) _arg_powerSourceSetting="${_key##--powerSourceSetting=}" ;; --screenTimeout | --screentimeout | --screen) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_screenTimeout=$2 shift ;; --screenTimeout=*) _arg_screenTimeout="${_key##--screenTimeout=}" ;; --sleepTimeout | --sleeptimeout | --sleep) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_sleepTimeout=$2 shift ;; --sleepTimeout=*) _arg_sleepTimeout="${_key##--sleepTimeout=}" ;; --diskTimeout | --disktimeout | --disk) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_diskTimeout=$2 shift ;; --diskTimeout=*) _arg_diskTimeout="${_key##--diskTimeout=}" ;; --powernap | --powerNap) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_powernap=$2 shift ;; --powernap=*) _arg_powernap="${_key##--powernap=}" ;; --terminalKeepAlive | --terminalkeepawake | --terminalkeepalive) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_terminalKeepAlive=$2 shift ;; --terminalKeepAlive=*) _arg_terminalKeepAlive="${_key##--terminalKeepAlive=}" ;; --dimOnBattery | --dimonbattery | --dim) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_dimOnBattery=$2 shift ;; --dimOnBattery=*) _arg_dimOnBattery="${_key##--dimOnBattery=}" ;; --lowPowerMode | --lowpowermode | --lpm) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_lowPowerMode=$2 shift ;; --lowPowerMode=*) _arg_lowPowerMode="${_key##--lowPowerMode=}" ;; --tcpKeepAlive | --tcpkeepalive | --tcp) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_tcpKeepAlive=$2 shift ;; --tcpKeepAlive=*) _arg_tcpKeepAlive="${_key##--tcpKeepAlive=}" ;; --wakeOnNetwork | --wakeonnetwork | --won) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_wakeOneNetwork=$2 shift ;; --wakeOnNetwork=*) _arg_wakeOneNetwork="${_key##--wakeOnNetwork=}" ;; --help | -h) _PRINT_HELP=yes die 0 ;; *) _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'"1 ;; esac shift done } # Function to set power settings set_powersetting() { while test $# -gt 0; do local _key="$1" case "$_key" in --setting) test $# -lt 2 && die "Missing value for the argument '$_key'."1 local _setting=$2 shift ;; --value) test $# -lt 2 && die "Missing value for the argument '$_key'."1 local _value=$2 shift ;; --errorMessage) test $# -lt 2 && die "Missing value for the argument '$_key'."1 local _errorMessage=$2 shift ;; --successMessage) test $# -lt 2 && die "Missing value for the argument '$_key'."1 local _successMessage=$2 shift ;; --singleSetting) local _singleSetting="on" ;; *) _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'"1 ;; esac shift done # Check if required parameters are provided if [[ -z $_setting || -z $_value || -z $_errorMessage || -z $_successMessage ]]; then echo "[Error] One of the required parameters was not provided." >&2 exit 1 fi # Apply setting for "Plugged In" if applicable if [[ ($_arg_powerSourceSetting == "Both" || $_arg_powerSourceSetting == "Plugged In") && $_singleSetting != "on" ]]; then pmset -c "$_setting" "$_value" fi # Apply setting for "On Battery" if applicable if [[ ($_arg_powerSourceSetting == "Both" || $_arg_powerSourceSetting == "On Battery") && $_singleSetting != "on" && -n $batteryOptions ]]; then pmset -b "$_setting" "$_value" fi # Apply setting for all power sources if single setting is enabled if [[ $_singleSetting == "on" ]]; then pmset -a "$_setting" "$_value" fi # Retrieve and verify the new value of the setting newvalue=$(pmset -g custom | grep -w "$_setting") if [[ -z $batteryOptions && $_singleSetting != "on" ]]; then newvalueAC=$(echo "$newvalue" | xargs | cut -f2 -d' ') elif [[ $_singleSetting != "on" ]]; then newvalueDC=$(echo "$newvalue" | xargs | cut -f2 -d' ') newvalueAC=$(echo "$newvalue" | xargs | cut -f4 -d' ') else newvalue=$(echo "$newvalue" | xargs | cut -f2 -d' ') fi # Warn if unable to verify the setting change if [[ -z $newvalue ]]; then echo "Warning: Unable to verify setting change for '$_setting'." fi # Check and report the new value for "Plugged In" setting if [[ ($_arg_powerSourceSetting == "Both" || $_arg_powerSourceSetting == "Plugged In") && $_singleSetting != "on" && -n $newvalue ]]; then if [[ $newvalueAC != "$_value" ]]; then echo "[Error] $_errorMessage on the 'Plugged In' Policy." >&2 EXITCODE=1 else echo "$_successMessage on the 'Plugged In' Policy." fi fi # Check and report the new value for "On Battery" setting if [[ ($_arg_powerSourceSetting == "Both" || $_arg_powerSourceSetting == "On Battery") && $_singleSetting != "on" && -n $newvalue && -n $batteryOptions ]]; then if [[ $newvalueDC != "$_value" ]]; then echo "[Error] $_errorMessage on the 'Battery' Policy." >&2 EXITCODE=1 else echo "$_successMessage on the 'Battery' Policy." fi fi # Check and report the new value for single setting if [[ $_singleSetting == "on" && -n $newvalue ]]; then if [[ $newvalue != "$_value" ]]; then echo "[Error] $_errorMessage" >&2 EXITCODE=1 else echo "$_successMessage" fi fi echo "" } # Call the function to parse command-line arguments parse_commandline "$@" # If script form values are set, replace the command-line parameters with these values if [[ -n $powerSourceSetting ]]; then _arg_powerSourceSetting="$powerSourceSetting" fi if [[ -n $screenTimeoutInMinutes ]]; then _arg_screenTimeout="$screenTimeoutInMinutes" fi if [[ -n $sleepTimeoutInMinutes ]]; then _arg_sleepTimeout="$sleepTimeoutInMinutes" fi if [[ -n $diskTimeoutInMinutes ]]; then _arg_diskTimeout="$diskTimeoutInMinutes" fi if [[ -n $powerNap ]]; then _arg_powernap="$powerNap" fi if [[ -n $terminalKeepAlive ]]; then _arg_terminalKeepAlive="$terminalKeepAlive" fi if [[ -n $dimTheDisplayOnBattery ]]; then _arg_dimOnBattery="$dimTheDisplayOnBattery" fi if [[ -n $lowPowerMode ]]; then _arg_lowPowerMode="$lowPowerMode" fi if [[ -n $tcpKeepAlive ]]; then _arg_tcpKeepAlive="$tcpKeepAlive" fi if [[ -n $wakeOnNetwork ]]; then _arg_wakeOneNetwork="$wakeOnNetwork" fi # Check if the device has battery options available batteryOptions=$(pmset -g custom | grep "Battery Power") # Validate the power source setting if [[ -z $_arg_powerSourceSetting || ($_arg_powerSourceSetting != "Both" && $_arg_powerSourceSetting != "Plugged In" && $_arg_powerSourceSetting != "On Battery") ]]; then echo "[Error] An invalid power source was given '$_arg_powerSourceSetting'. The only valid options are 'Both', 'Plugged In', and 'On Battery'." >&2 exit 1 fi if [[ -z $_arg_screenTimeout && -z $_arg_sleepTimeout && -z $_arg_diskTimeout && -z $_arg_powernap && -z $_arg_terminalKeepAlive && -z $_arg_dimOnBattery && -z $_arg_wakeOneNetwork && -z $_arg_lowPowerMode && -z $_arg_tcpKeepAlive ]]; then PRINT_HELP=yes die "[Error] No action given to take. Please specify a power setting to set." 1 fi # Check if setting battery power source on a device without a battery if [[ $_arg_powerSourceSetting == "On Battery" && -z $batteryOptions ]]; then echo "[Error] Cannot set battery power source setting on a device that does not have a battery." >&2 exit 1 fi # Warn if trying to set battery power source on a device without a battery if [[ $_arg_powerSourceSetting == "Both" && -z $batteryOptions ]]; then echo "[Warning] Cannot set battery power source setting on a device that does not have a battery. Ignoring battery power source settings." fi # Validate screen timeout argument if [[ -n $_arg_screenTimeout && (! $_arg_screenTimeout =~ [0-9]+ || $_arg_screenTimeout -lt 0) ]]; then echo "[Error] An invalid screen timeout argument was given '$_arg_screenTimeout'. Please specify a positive number representing the desired timeout in minutes." >&2 exit 1 fi # Validate sleep timeout argument if [[ -n $_arg_sleepTimeout && (! $_arg_sleepTimeout =~ [0-9]+ || $_arg_sleepTimeout -lt 0) ]]; then echo "[Error] An invalid sleep timeout argument was given '$_arg_sleepTimeout'. Please specify a positive number representing the desired timeout in minutes." >&2 exit 1 fi # Validate disk timeout argument if [[ -n $_arg_diskTimeout && (! $_arg_diskTimeout =~ [0-9]+ || $_arg_diskTimeout -lt 0) ]]; then echo "[Error] An invalid disk timeout argument was given '$_arg_diskTimeout'. Please specify a positive number representing the desired timeout in minutes." >&2 exit 1 fi # Validate powernap setting if [[ -n $_arg_powernap && $_arg_powernap != "Disable" && $_arg_powernap != "Enable" ]]; then echo "[Error] An invalid power nap setting was given '$_arg_powernap'. The only valid options are 'Disable' or 'Enable'." >&2 exit 1 fi # Validate terminal keep alive setting if [[ -n $_arg_terminalKeepAlive && $_arg_terminalKeepAlive != "Disable" && $_arg_terminalKeepAlive != "Enable" ]]; then echo "[Error] An invalid terminal keep alive setting was given '$_arg_terminalKeepAlive'. The only valid options are 'Disable' or 'Enable'." >&2 exit 1 fi # Validate dim on battery setting if [[ -n $_arg_dimOnBattery && $_arg_dimOnBattery != "Disable" && $_arg_dimOnBattery != "Enable" ]]; then echo "[Error] An invalid dim on battery setting was given '$_arg_dimOnBattery'. The only valid options are 'Disable' or 'Enable'." >&2 exit 1 fi # Validate low power mode setting if [[ -n $_arg_lowPowerMode && $_arg_lowPowerMode != "Disable" && $_arg_lowPowerMode != "Enable" ]]; then echo "[Error] An invalid low power mode setting was given '$_arg_lowPowerMode'. The only valid options are 'Disable' or 'Enable'." >&2 exit 1 fi # Validate TCP keep alive setting if [[ -n $_arg_tcpKeepAlive && $_arg_tcpKeepAlive != "Disable" && $_arg_tcpKeepAlive != "Enable" ]]; then echo "[Error] An invalid tcp keep alive setting was given '$_arg_tcpKeepAlive'. The only valid options are 'Disable' or 'Enable'." >&2 exit 1 fi # Validate wake on network setting if [[ -n $_arg_wakeOneNetwork && $_arg_wakeOneNetwork != "Never" && $_arg_wakeOneNetwork != "Always" && $_arg_wakeOneNetwork != "Only on Power Adapter" ]]; then echo "[Error] An invalid wake one network setting was given '$_arg_wakeOneNetwork'. The only valid options are 'Never', 'Always' and 'Only on Power Adapter'." >&2 exit 1 fi # Set screen timeout if provided if [[ -n $_arg_screenTimeout ]]; then timeoutError="Failed to set screen timeout of '$_arg_screenTimeout' minutes" timeoutSuccess="Successfully set screen timeout of '$_arg_screenTimeout' minutes" # Call the function to set the power setting for screen timeout set_powersetting --setting "displaysleep" --value "$_arg_screenTimeout" --errorMessage "$timeoutError" --successMessage "$timeoutSuccess" fi # Set sleep timeout if provided if [[ -n $_arg_sleepTimeout ]]; then sleepError="Failed to set sleep timeout of '$_arg_sleepTimeout' minutes" sleepSuccess="Successfully set sleep timeout of '$_arg_sleepTimeout' minutes" # Call the function to set the power setting for sleep timeout set_powersetting --setting "sleep" --value "$_arg_sleepTimeout" --errorMessage "$sleepError" --successMessage "$sleepSuccess" fi # Set disk timeout if provided if [[ -n $_arg_diskTimeout ]]; then diskError="Failed to set disk timeout of '$_arg_diskTimeout' minutes" diskSuccess="Successfully set disk timeout of '$_arg_diskTimeout' minutes" # Call the function to set the power setting for disk timeout set_powersetting --setting "disksleep" --value "$_arg_diskTimeout" --errorMessage "$diskError" --successMessage "$diskSuccess" fi # Set power nap setting if provided if [[ -n $_arg_powernap ]]; then if [[ $_arg_powernap == "Enable" ]]; then _powernap_setting=1 napError="Failed to enable powernap" napSuccess="Successfully enabled powernap" else _powernap_setting=0 napError="Failed to disable powernap" napSuccess="Successfully disabled powernap" fi # Call the function to set the power setting for power nap set_powersetting --setting "powernap" --value "$_powernap_setting" --errorMessage "$napError" --successMessage "$napSuccess" fi # Set terminal keep alive setting if provided if [[ -n $_arg_terminalKeepAlive ]]; then if [[ $_arg_terminalKeepAlive == "Enable" ]]; then _terminal_setting=1 terminalError="Failed to enable terminal keep alive" terminalSuccess="Successfully terminal keep alive" else _terminal_setting=0 terminalError="Failed to disable terminal keep alive" terminalSuccess="Successfully disabled terminal keep alive" fi # Call the function to set the power setting for terminal keep alive set_powersetting --setting "ttyskeepawake" --value "$_terminal_setting" --errorMessage "$terminalError" --successMessage "$terminalSuccess" fi # Set the "dim on battery" setting if the argument is provided if [[ -n $_arg_dimOnBattery ]]; then if [[ $_arg_dimOnBattery == "Enable" ]]; then _dim_setting=1 dimError="Failed to enable the setting 'dim on battery'." dimSuccess="Successfully enabled the setting 'dim on battery'." else _dim_setting=0 dimError="Failed to disable the setting 'dim on battery'." dimSuccess="Successfully disabled the setting 'dim on battery'." fi # Call the function to set the power setting for dim on battery set_powersetting --setting "lessbright" --value "$_dim_setting" --errorMessage "$dimError" --successMessage "$dimSuccess" --singleSetting fi # Set the low power mode setting if the argument is provided if [[ -n $_arg_lowPowerMode ]]; then if [[ $_arg_lowPowerMode == "Enable" ]]; then _lpm_setting=1 lpmError="Failed to enable low power mode" lpmSuccess="Successfully enabled low power mode" else _lpm_setting=0 lpmError="Failed to disable low power mode" lpmSuccess="Successfully disabled low power mode" fi # Call the function to set the power setting for low power mode set_powersetting --setting "lowpowermode" --value "$_lpm_setting" --errorMessage "$lpmError" --successMessage "$lpmSuccess" fi # Set the TCP keep alive setting if the argument is provided if [[ -n $_arg_tcpKeepAlive ]]; then if [[ $_arg_tcpKeepAlive == "Enable" ]]; then _tcp_setting=1 tcpError="Failed to enable tcp keep alive" tcpSuccess="Successfully enabled tcp keep alive" else _tcp_setting=0 tcpError="Failed to disable tcp keep alive" tcpSuccess="Successfully disabled tcp keep alive" fi # Call the function to set the power setting for TCP keep alive set_powersetting --setting "tcpkeepalive" --value "$_tcp_setting" --errorMessage "$tcpError" --successMessage "$tcpSuccess" fi # Set the wake on network setting if the argument is provided if [[ -n $_arg_wakeOneNetwork ]]; then case $_arg_wakeOneNetwork in "Never") pmset -a womp 0 ;; "Always") pmset -a womp 1 ;; "Only on Power Adapter") if [[ -n $batteryOptions ]]; then pmset -b womp 0 fi pmset -c womp 1 ;; esac # Retrieve and verify the new value of the wake on network setting newvalue=$(pmset -g custom | grep -w "womp") if [[ -z $batteryOptions ]]; then # Get the new value for the "Plugged In" policy if no battery options newvalueAC=$(echo "$newvalue" | xargs | cut -f2 -d' ') else # Get the new values for both "Battery" and "Plugged In" policies newvalueDC=$(echo "$newvalue" | xargs | cut -f2 -d' ') newvalueAC=$(echo "$newvalue" | xargs | cut -f4 -d' ') fi # Check and report the new value for "Always" and "Only on Power Adapter" settings if [[ $_arg_wakeOneNetwork == "Always" || $_arg_wakeOneNetwork == "Only on Power Adapter" ]]; then if [[ $newvalueAC != 1 ]]; then echo "[Error] Unable to enable wake on network on the 'Plugged In' Policy." >&2 EXITCODE=1 else echo "Successfully enabled wake on network on the 'Plugged In' Policy." fi fi # Check and report the new value for "Never" setting if [[ $_arg_wakeOneNetwork == "Never" ]]; then if [[ $newvalueAC != 0 ]]; then echo "[Error] Unable to disable wake on network on the 'Plugged In' Policy." >&2 EXITCODE=1 else echo "Successfully disabled wake on network on the 'Plugged In' Policy." fi fi # Check and report the new value for "Always" setting on battery if [[ -n $batteryOptions && $_arg_wakeOneNetwork == "Always" ]]; then if [[ $newvalueDC != 1 ]]; then echo "[Error] Unable to enable wake on network on the 'Battery' Policy." >&2 EXITCODE=1 else echo "Successfully enabled wake on network on the 'Battery' Policy." fi fi # Check and report the new value for "Never" and "Only on Power Adapter" settings on battery if [[ -n $batteryOptions && ($_arg_wakeOneNetwork == "Never" || $_arg_wakeOneNetwork == "Only on Power Adapter") ]]; then if [[ $newvalueDC != 0 ]]; then echo "[Error] Unable to disable wake on network on the 'Battery' Policy." >&2 EXITCODE=1 else echo "Successfully disabled wake on network on the 'Battery' Policy." fi fi fi # Exit the script with the set exit code if it is defined if [[ -n $EXITCODE ]]; then exit "$EXITCODE" fi
Gagnez du temps grâce à plus de 300 scripts du Dojo NinjaOne.
Ce que fait ce script
Ce script est un outil complet pour gérer les paramètres d’alimentation de macOS. Il prend en charge des options pour les modes batterie et branché, en tenant compte de paramètres tels que :
- Délai de mise en veille de l’écran : délai avant que l’écran ne s’éteigne.
- Délai de mise en veille du système : délai avant que le système ne passe en mode veille.
- Ralentissement du disque : délai avant que le disque ne s’éteigne en cas d’inactivité.
- Power Nap : met à jour les données du système pendant la veille.
- Mode basse consommation : réduit la consommation d’énergie et prolonge la durée de vie de la batterie.
- TCP Keepalive : maintient les connexions réseau actives.
- Réactivation sur le réseau : réactive l’appareil à la réception d’un paquet réseau.
Le script utilise la commande pmset pour configurer ces paramètres, avec une gestion des erreurs et une vérification améliorées pour garantir une application réussie.
Comment fonctionne le script
Analyse des arguments
Le script accepte divers arguments de ligne de commande, tels que –screenTimeout, –sleepTimeout et –lowPowerMode. Chaque paramètre peut être adapté pour appliquer des réglages à l’alimentation par batterie, au mode branché ou aux deux. Si des arguments incorrects ou non valides sont fournis, le script affiche des messages d’erreur détaillés.
Validation des paramètres
Avant d’appliquer les modifications, le script valide les valeurs saisies pour garantir la compatibilité avec macOS. Par exemple :
- Les valeurs des délais de mise en veille de l’écran doivent être des nombres entiers non négatifs.
- Les seuls paramètres disponibles pour Power Nap “Activer” ou “Désactiver.”
- Le comportement de réactivation sur le réseau doit s’aligner sur les capacités de macOS.
Cas d’utilisation hypothétique
Imaginez un administrateur informatique gérant un parc d’ordinateurs portables macOS dans un environnement d’entreprise. Il veut s’assurer que :
- les écrans s’éteignent après 5 minutes d’inactivité pour économiser de l’énergie.
- Les appareils passent en mode veille au bout de 10 minutes lorsqu’ils fonctionnent sur batterie.
- La fonction Power Nap soit désactivée afin de préserver les ressources pendant les heures creuses.
Cela garantit la cohérence et réduit le temps de configuration manuelle.
Comparaison du script avec d’autres méthodes
Bien que la commande macOS pmset soit puissante, elle nécessite une saisie manuelle et ne valide pas les paramètres. Les outils basés sur une interface graphique, tels que les préférences système, proposent une automatisation limitée et ne prennent pas en charge la configuration par lots. Ce script comble le fossé en combinant la flexibilité de pmset avec la facilité de l’automatisation.
Questions fréquentes sur le script pour les paramètres d’alimentation macOS
Ce script peut-il fonctionner sur des appareils sans batterie ?
Oui, mais les paramètres spécifiques à la batterie seront ignorés. Le script détecte la disponibilité de la batterie et émet des avertissements si des options liées à la batterie sont utilisées sur des appareils qui en sont dépourvus.
Que se passe-t-il si je fournis des arguments non valables ?
Le script valide toutes les entrées et fournit des messages d’erreur détaillés si un argument est incorrect.
L’utilisation de Power Nap est-elle sans danger ?
Power Nap peut consommer des ressources supplémentaires pendant la veille. Il est recommandé de le désactiver dans les environnements nécessitant une efficacité énergétique maximale.
Implications plus larges pour la sécurité et l’efficacité des technologies de l’information
La gestion automatisée de l’alimentation contribue à l’efficacité énergétique et réduit l’empreinte carbone, conformément aux objectifs de développement durable des entreprises. De plus, des fonctions telles que la désactivation de Power Nap peuvent renforcer la sécurité en réduisant les processus d’arrière-plan et les vecteurs d’attaque potentiels.
Bonnes pratiques de l’utilisation du script
- Configurations des tests : exécutez le script sur un appareil de test avant de le déployer pour vous assurer que les paramètres se comportent comme prévu.
- Modifications du document : conservez un journal des paramètres appliqués à des fins de dépannage et de conformité.
- Contrôle des performances : vérifiez régulièrement que les appareils respectent les paramètres d’alimentation configurés.
Conclusion
La gestion de l’alimentation est un aspect essentiel de l’administration du système macOS. Ce script simplifie le processus, permettant aux professionnels de l’informatique de configurer et d’appliquer efficacement les paramètres d’alimentation. Pour les MSP et les entreprises qui cherchent à rationaliser la gestion des appareils macOS, des solutions telles que NinjaOne proposent des outils de surveillance et d’automatisation avancés, garantissant que les appareils restent optimisés et sécurisés.