{"id":392562,"date":"2024-12-16T14:29:56","date_gmt":"2024-12-16T14:29:56","guid":{"rendered":"https:\/\/www.ninjaone.com\/?post_type=script_hub&#038;p=392562"},"modified":"2024-12-16T14:29:56","modified_gmt":"2024-12-16T14:29:56","slug":"gestion-des-parametres-d-alimentation-macos","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/fr\/script-hub\/gestion-des-parametres-d-alimentation-macos\/","title":{"rendered":"Gestion des param\u00e8tres d&rsquo;alimentation macOS \u00e0 l&rsquo;aide d&rsquo;un script Bash personnalis\u00e9\u00a0"},"content":{"rendered":"<p>Une gestion efficace de l&rsquo;\u00e9nergie est essentielle pour les professionnels de l&rsquo;informatique et les <a href=\"https:\/\/www.ninjaone.com\/fr\/quest-ce-quun-msp\">fournisseurs de services g\u00e9r\u00e9s (MSP)<\/a> charg\u00e9s de la maintenance des syst\u00e8mes macOS. Ce script Bash personnalis\u00e9 simplifie le processus de gestion des <strong>param\u00e8tres d&rsquo;alimentation macOS<\/strong>, permettant aux utilisateurs d&rsquo;ajuster des param\u00e8tres tels que la minuterie de veille, le d\u00e9lai pour la mise en veille et les fonctions d&rsquo;\u00e9conomie d&rsquo;\u00e9nergie. Cet article plonge dans les fonctionnalit\u00e9s, les cas d&rsquo;utilisation et les implications de l&rsquo;utilisation de ce script.<\/p>\n<h2>Comprendre la gestion de l&rsquo;\u00e9nergie dans macOS<\/h2>\n<p>La gestion de l&rsquo;alimentation permet aux syst\u00e8mes d&rsquo;\u00e9quilibrer les performances et l&rsquo;efficacit\u00e9 \u00e9nerg\u00e9tique, en prolongeant la dur\u00e9e de vie de la batterie et en minimisant l&rsquo;utilisation des ressources inutiles. Bien que macOS propose des outils natifs tels que la commande pmset, la configuration manuelle de ces param\u00e8tres peut s&rsquo;av\u00e9rer longue et sujette \u00e0 des erreurs, en particulier dans les environnements comportant de nombreux appareils. Ce script permet une approche rationalis\u00e9e de la gestion des param\u00e8tres d&rsquo;alimentation, en fournissant un contr\u00f4le pr\u00e9cis \u00a4sur diff\u00e9rents param\u00e8tres.<\/p>\n<h2>Le script\u00a0:<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">#!\/usr\/bin\/env bash\r\n#\r\n# 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.\r\n#   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.\r\n#   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. \r\n#   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. \r\n#   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. \r\n#   Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, 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. \r\n#   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. \r\n#   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. \r\n#   EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n#\r\n# Preset Parameter: --powerSourceSetting \"Both\"\r\n#   Should these changes apply when the device is plugged in, on battery, or both?\r\n#\r\n# Preset Parameter: --screenTimeout \"10\"\r\n#\t\tTime in minutes to wait before turning off the screen. (0 to disable)\r\n#\r\n# Preset Parameter: --sleepTimeout \"10\"\r\n#\t\tTime in minutes to wait before the computer sleeps. (0 to disable)\r\n#\r\n# Preset Parameter: --diskTimeout \"10\"\r\n#\t\tTime in minutes for the disk to spin down when idle. (0 to disable)\r\n#\r\n# Preset Parameter: --powernap \"Disable\"\r\n#\t\tWhen the Mac goes to sleep, Power Nap activates periodically to update information such as Mail, Calendar, and other iCloud events.\r\n#\r\n# Preset Parameter: --terminalKeepAlive \"Enable\"\r\n#\t\tPrevents 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.\r\n#\r\n# Preset Parameter: --dimOnBattery \"Enable\"\r\n#\t\tSlightly turn down the display brightness when switching to battery power.\r\n#\r\n# Preset Parameter: --lowPowerMode \"Enable\"\r\n#\t\tReduces energy consumption by lowering the system performance and brightness, stopping background processes, and adjusting system settings to extend battery life.\r\n#\r\n# Preset Parameter: --tcpKeepAlive \"Enable\"\r\n#\t\tEnsures that a network connection remains active by periodically sending keepalive packets to prevent the connection from being dropped due to inactivity.\r\n#\r\n# Preset Parameter: --wakeOnNetwork \"Only on Power Adapter\"\r\n#\t\tWake when an Ethernet magic packet is received.\r\n#\r\n# Preset Parameter: --help\r\n#\t\tDisplays some help text.\r\n#\r\n# Release Notes: Initial Release\r\n\r\n# Define initial argument variables with default values\r\n_arg_powerSourceSetting=\"Both\"\r\n_arg_screenTimeout=\r\n_arg_sleepTimeout=\r\n_arg_diskTimeout=\r\n_arg_powernap=\r\n_arg_terminalKeepAlive=\r\n_arg_dimOnBattery=\r\n_arg_wakeOneNetwork=\r\n_arg_lowPowerMode=\r\n_arg_tcpKeepAlive=\r\n\r\n# Function to display help menu\r\nprint_help() {\r\n  printf '\\n\\n%s\\n\\n' 'Usage: [--powerSourceSetting|-p &lt;arg&gt;] [--someSwitch|-s] [--help|-h]'\r\n  printf '%s\\n' 'Preset Parameter: --powerSourceSetting \"Both\"'\r\n  printf '\\t%s\\n' \"Should these changes apply when the device is plugged in, on battery, or both?\"\r\n  printf '%s\\n' 'Preset Parameter: --screenTimeout \"10\"'\r\n  printf '\\t%s\\n' \"Time in minutes to wait before turning off the screen. (0 to disable)\"\r\n  printf '%s\\n' 'Preset Parameter: --sleepTimeout \"10\"'\r\n  printf '\\t%s\\n' \"Time in minutes to wait before the computer sleeps. (0 to disable)\"\r\n  printf '%s\\n' 'Preset Parameter: --diskTimeout \"10\"'\r\n  printf '\\t%s\\n' \"Time in minutes for the disk to spin down when idle. (0 to disable)\"\r\n  printf '%s\\n' 'Preset Parameter: --powernap \"Disable\"'\r\n  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.\"\r\n  printf '%s\\n' 'Preset Parameter: --terminalKeepAlive \"Enable\"'\r\n  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.\"\r\n  printf '%s\\n' 'Preset Parameter: --dimOnBattery \"Enable\"'\r\n  printf '\\t%s\\n' \"Slightly turn down the display brightness when switching to battery power.\"\r\n  printf '%s\\n' 'Preset Parameter: --lowPowerMode \"Enable\"'\r\n  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.\"\r\n  printf '%s\\n' 'Preset Parameter: --tcpKeepAlive \"Enable\"'\r\n  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.\"\r\n  printf '%s\\n' 'Preset Parameter: --wakeOnNetwork \"Only on Power Adapter\"'\r\n  printf '\\t%s\\n' \"Wake when an Ethernet magic packet is received.\"\r\n  printf '\\n%s\\n' 'Preset Parameter: --help'\r\n  printf '\\t%s\\n' \"Displays this help menu.\"\r\n}\r\n\r\n# Function to display error message and exit the script\r\ndie() {\r\n  local _ret=\"${2:-1}\"\r\n  echo \"$1\" &gt;&amp;2\r\n  test \"${_PRINT_HELP:-no}\" = yes &amp;&amp; print_help &gt;&amp;2\r\n  exit \"${_ret}\"\r\n}\r\n\r\n# Function to parse command-line arguments\r\nparse_commandline() {\r\n  while test $# -gt 0; do\r\n    _key=\"$1\"\r\n    case \"$_key\" in\r\n    --powerSourceSetting | --powersourcesetting | --powersource)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_powerSourceSetting=$2\r\n      shift\r\n      ;;\r\n    --powerSourceSetting=*)\r\n      _arg_powerSourceSetting=\"${_key##--powerSourceSetting=}\"\r\n      ;;\r\n    --screenTimeout | --screentimeout | --screen)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_screenTimeout=$2\r\n      shift\r\n      ;;\r\n    --screenTimeout=*)\r\n      _arg_screenTimeout=\"${_key##--screenTimeout=}\"\r\n      ;;\r\n    --sleepTimeout | --sleeptimeout | --sleep)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_sleepTimeout=$2\r\n      shift\r\n      ;;\r\n    --sleepTimeout=*)\r\n      _arg_sleepTimeout=\"${_key##--sleepTimeout=}\"\r\n      ;;\r\n    --diskTimeout | --disktimeout | --disk)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_diskTimeout=$2\r\n      shift\r\n      ;;\r\n    --diskTimeout=*)\r\n      _arg_diskTimeout=\"${_key##--diskTimeout=}\"\r\n      ;;\r\n    --powernap | --powerNap)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_powernap=$2\r\n      shift\r\n      ;;\r\n    --powernap=*)\r\n      _arg_powernap=\"${_key##--powernap=}\"\r\n      ;;\r\n    --terminalKeepAlive | --terminalkeepawake | --terminalkeepalive)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_terminalKeepAlive=$2\r\n      shift\r\n      ;;\r\n    --terminalKeepAlive=*)\r\n      _arg_terminalKeepAlive=\"${_key##--terminalKeepAlive=}\"\r\n      ;;\r\n    --dimOnBattery | --dimonbattery | --dim)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_dimOnBattery=$2\r\n      shift\r\n      ;;\r\n    --dimOnBattery=*)\r\n      _arg_dimOnBattery=\"${_key##--dimOnBattery=}\"\r\n      ;;\r\n    --lowPowerMode | --lowpowermode | --lpm)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_lowPowerMode=$2\r\n      shift\r\n      ;;\r\n    --lowPowerMode=*)\r\n      _arg_lowPowerMode=\"${_key##--lowPowerMode=}\"\r\n      ;;\r\n    --tcpKeepAlive | --tcpkeepalive | --tcp)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_tcpKeepAlive=$2\r\n      shift\r\n      ;;\r\n    --tcpKeepAlive=*)\r\n      _arg_tcpKeepAlive=\"${_key##--tcpKeepAlive=}\"\r\n      ;;\r\n    --wakeOnNetwork | --wakeonnetwork | --won)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the optional argument '$_key'.\" 1\r\n      _arg_wakeOneNetwork=$2\r\n      shift\r\n      ;;\r\n    --wakeOnNetwork=*)\r\n      _arg_wakeOneNetwork=\"${_key##--wakeOnNetwork=}\"\r\n      ;;\r\n    --help | -h)\r\n      _PRINT_HELP=yes die 0\r\n      ;;\r\n    *)\r\n      _PRINT_HELP=yes die \"FATAL ERROR: Got an unexpected argument '$1'\"1\r\n      ;;\r\n    esac\r\n    shift\r\n  done\r\n}\r\n\r\n# Function to set power settings\r\nset_powersetting() {\r\n  while test $# -gt 0; do\r\n    local _key=\"$1\"\r\n    case \"$_key\" in\r\n    --setting)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the argument '$_key'.\"1\r\n      local _setting=$2\r\n      shift\r\n      ;;\r\n    --value)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the argument '$_key'.\"1\r\n      local _value=$2\r\n      shift\r\n      ;;\r\n    --errorMessage)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the argument '$_key'.\"1\r\n      local _errorMessage=$2\r\n      shift\r\n      ;;\r\n    --successMessage)\r\n      test $# -lt 2 &amp;&amp; die \"Missing value for the argument '$_key'.\"1\r\n      local _successMessage=$2\r\n      shift\r\n      ;;\r\n    --singleSetting)\r\n      local _singleSetting=\"on\"\r\n      ;;\r\n    *)\r\n      _PRINT_HELP=yes die \"FATAL ERROR: Got an unexpected argument '$1'\"1\r\n      ;;\r\n    esac\r\n    shift\r\n  done\r\n\r\n  # Check if required parameters are provided\r\n  if [[ -z $_setting || -z $_value || -z $_errorMessage || -z $_successMessage ]]; then\r\n    echo \"[Error] One of the required parameters was not provided.\" &gt;&amp;2\r\n    exit 1\r\n  fi\r\n\r\n  # Apply setting for \"Plugged In\" if applicable\r\n  if [[ ($_arg_powerSourceSetting == \"Both\" || $_arg_powerSourceSetting == \"Plugged In\") &amp;&amp; $_singleSetting != \"on\" ]]; then\r\n    pmset -c \"$_setting\" \"$_value\"\r\n  fi\r\n\r\n  # Apply setting for \"On Battery\" if applicable\r\n  if [[ ($_arg_powerSourceSetting == \"Both\" || $_arg_powerSourceSetting == \"On Battery\") &amp;&amp; $_singleSetting != \"on\" &amp;&amp; -n $batteryOptions ]]; then\r\n    pmset -b \"$_setting\" \"$_value\"\r\n  fi\r\n\r\n  # Apply setting for all power sources if single setting is enabled\r\n  if [[ $_singleSetting == \"on\" ]]; then\r\n    pmset -a \"$_setting\" \"$_value\"\r\n  fi\r\n\r\n  # Retrieve and verify the new value of the setting\r\n  newvalue=$(pmset -g custom | grep -w \"$_setting\")\r\n  if [[ -z $batteryOptions &amp;&amp; $_singleSetting != \"on\" ]]; then\r\n    newvalueAC=$(echo \"$newvalue\" | xargs | cut -f2 -d' ')\r\n  elif [[ $_singleSetting != \"on\" ]]; then\r\n    newvalueDC=$(echo \"$newvalue\" | xargs | cut -f2 -d' ')\r\n    newvalueAC=$(echo \"$newvalue\" | xargs | cut -f4 -d' ')\r\n  else\r\n    newvalue=$(echo \"$newvalue\" | xargs | cut -f2 -d' ')\r\n  fi\r\n\r\n  # Warn if unable to verify the setting change\r\n  if [[ -z $newvalue ]]; then\r\n    echo \"Warning: Unable to verify setting change for '$_setting'.\"\r\n  fi\r\n\r\n  # Check and report the new value for \"Plugged In\" setting\r\n  if [[ ($_arg_powerSourceSetting == \"Both\" || $_arg_powerSourceSetting == \"Plugged In\") &amp;&amp; $_singleSetting != \"on\" &amp;&amp; -n $newvalue ]]; then\r\n    if [[ $newvalueAC != \"$_value\" ]]; then\r\n      echo \"[Error] $_errorMessage on the 'Plugged In' Policy.\" &gt;&amp;2\r\n      EXITCODE=1\r\n    else\r\n      echo \"$_successMessage on the 'Plugged In' Policy.\"\r\n    fi\r\n  fi\r\n\r\n  # Check and report the new value for \"On Battery\" setting\r\n  if [[ ($_arg_powerSourceSetting == \"Both\" || $_arg_powerSourceSetting == \"On Battery\") &amp;&amp; $_singleSetting != \"on\" &amp;&amp; -n $newvalue &amp;&amp; -n $batteryOptions ]]; then\r\n    if [[ $newvalueDC != \"$_value\" ]]; then\r\n      echo \"[Error] $_errorMessage on the 'Battery' Policy.\" &gt;&amp;2\r\n      EXITCODE=1\r\n    else\r\n      echo \"$_successMessage on the 'Battery' Policy.\"\r\n    fi\r\n  fi\r\n\r\n  # Check and report the new value for single setting\r\n  if [[ $_singleSetting == \"on\" &amp;&amp; -n $newvalue ]]; then\r\n    if [[ $newvalue != \"$_value\" ]]; then\r\n      echo \"[Error] $_errorMessage\" &gt;&amp;2\r\n      EXITCODE=1\r\n    else\r\n      echo \"$_successMessage\"\r\n    fi\r\n  fi\r\n\r\n  echo \"\"\r\n}\r\n\r\n# Call the function to parse command-line arguments\r\nparse_commandline \"$@\"\r\n\r\n# If script form values are set, replace the command-line parameters with these values\r\nif [[ -n $powerSourceSetting ]]; then\r\n  _arg_powerSourceSetting=\"$powerSourceSetting\"\r\nfi\r\nif [[ -n $screenTimeoutInMinutes ]]; then\r\n  _arg_screenTimeout=\"$screenTimeoutInMinutes\"\r\nfi\r\nif [[ -n $sleepTimeoutInMinutes ]]; then\r\n  _arg_sleepTimeout=\"$sleepTimeoutInMinutes\"\r\nfi\r\nif [[ -n $diskTimeoutInMinutes ]]; then\r\n  _arg_diskTimeout=\"$diskTimeoutInMinutes\"\r\nfi\r\nif [[ -n $powerNap ]]; then\r\n  _arg_powernap=\"$powerNap\"\r\nfi\r\nif [[ -n $terminalKeepAlive ]]; then\r\n  _arg_terminalKeepAlive=\"$terminalKeepAlive\"\r\nfi\r\nif [[ -n $dimTheDisplayOnBattery ]]; then\r\n  _arg_dimOnBattery=\"$dimTheDisplayOnBattery\"\r\nfi\r\nif [[ -n $lowPowerMode ]]; then\r\n  _arg_lowPowerMode=\"$lowPowerMode\"\r\nfi\r\nif [[ -n $tcpKeepAlive ]]; then\r\n  _arg_tcpKeepAlive=\"$tcpKeepAlive\"\r\nfi\r\nif [[ -n $wakeOnNetwork ]]; then\r\n  _arg_wakeOneNetwork=\"$wakeOnNetwork\"\r\nfi\r\n\r\n# Check if the device has battery options available\r\nbatteryOptions=$(pmset -g custom | grep \"Battery Power\")\r\n\r\n# Validate the power source setting\r\nif [[ -z $_arg_powerSourceSetting || ($_arg_powerSourceSetting != \"Both\" &amp;&amp; $_arg_powerSourceSetting != \"Plugged In\" &amp;&amp; $_arg_powerSourceSetting != \"On Battery\") ]]; then\r\n  echo \"[Error] An invalid power source was given '$_arg_powerSourceSetting'. The only valid options are 'Both', 'Plugged In', and 'On Battery'.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\nif [[ -z $_arg_screenTimeout &amp;&amp; -z $_arg_sleepTimeout &amp;&amp; -z $_arg_diskTimeout &amp;&amp; -z $_arg_powernap &amp;&amp; -z $_arg_terminalKeepAlive &amp;&amp; -z $_arg_dimOnBattery &amp;&amp; -z $_arg_wakeOneNetwork &amp;&amp; -z $_arg_lowPowerMode &amp;&amp; -z $_arg_tcpKeepAlive ]]; then\r\n  PRINT_HELP=yes die \"[Error] No action given to take. Please specify a power setting to set.\" 1\r\nfi\r\n\r\n# Check if setting battery power source on a device without a battery\r\nif [[ $_arg_powerSourceSetting == \"On Battery\" &amp;&amp; -z $batteryOptions ]]; then\r\n  echo \"[Error] Cannot set battery power source setting on a device that does not have a battery.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Warn if trying to set battery power source on a device without a battery\r\nif [[ $_arg_powerSourceSetting == \"Both\" &amp;&amp; -z $batteryOptions ]]; then\r\n  echo \"[Warning] Cannot set battery power source setting on a device that does not have a battery. Ignoring battery power source settings.\"\r\nfi\r\n\r\n# Validate screen timeout argument\r\nif [[ -n $_arg_screenTimeout &amp;&amp; (! $_arg_screenTimeout =~ [0-9]+ || $_arg_screenTimeout -lt 0) ]]; then\r\n  echo \"[Error] An invalid screen timeout argument was given '$_arg_screenTimeout'. Please specify a positive number representing the desired timeout in minutes.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate sleep timeout argument\r\nif [[ -n $_arg_sleepTimeout &amp;&amp; (! $_arg_sleepTimeout =~ [0-9]+ || $_arg_sleepTimeout -lt 0) ]]; then\r\n  echo \"[Error] An invalid sleep timeout argument was given '$_arg_sleepTimeout'. Please specify a positive number representing the desired timeout in minutes.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate disk timeout argument\r\nif [[ -n $_arg_diskTimeout &amp;&amp; (! $_arg_diskTimeout =~ [0-9]+ || $_arg_diskTimeout -lt 0) ]]; then\r\n  echo \"[Error] An invalid disk timeout argument was given '$_arg_diskTimeout'. Please specify a positive number representing the desired timeout in minutes.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate powernap setting\r\nif [[ -n $_arg_powernap &amp;&amp; $_arg_powernap != \"Disable\" &amp;&amp; $_arg_powernap != \"Enable\" ]]; then\r\n  echo \"[Error] An invalid power nap setting was given '$_arg_powernap'. The only valid options are 'Disable' or 'Enable'.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate terminal keep alive setting\r\nif [[ -n $_arg_terminalKeepAlive &amp;&amp; $_arg_terminalKeepAlive != \"Disable\" &amp;&amp; $_arg_terminalKeepAlive != \"Enable\" ]]; then\r\n  echo \"[Error] An invalid terminal keep alive setting was given '$_arg_terminalKeepAlive'. The only valid options are 'Disable' or 'Enable'.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate dim on battery setting\r\nif [[ -n $_arg_dimOnBattery &amp;&amp; $_arg_dimOnBattery != \"Disable\" &amp;&amp; $_arg_dimOnBattery != \"Enable\" ]]; then\r\n  echo \"[Error] An invalid dim on battery setting was given '$_arg_dimOnBattery'. The only valid options are 'Disable' or 'Enable'.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate low power mode setting\r\nif [[ -n $_arg_lowPowerMode &amp;&amp; $_arg_lowPowerMode != \"Disable\" &amp;&amp; $_arg_lowPowerMode != \"Enable\" ]]; then\r\n  echo \"[Error] An invalid low power mode setting was given '$_arg_lowPowerMode'. The only valid options are 'Disable' or 'Enable'.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate TCP keep alive setting\r\nif [[ -n $_arg_tcpKeepAlive &amp;&amp; $_arg_tcpKeepAlive != \"Disable\" &amp;&amp; $_arg_tcpKeepAlive != \"Enable\" ]]; then\r\n  echo \"[Error] An invalid tcp keep alive setting was given '$_arg_tcpKeepAlive'. The only valid options are 'Disable' or 'Enable'.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Validate wake on network setting\r\nif [[ -n $_arg_wakeOneNetwork &amp;&amp; $_arg_wakeOneNetwork != \"Never\" &amp;&amp; $_arg_wakeOneNetwork != \"Always\" &amp;&amp; $_arg_wakeOneNetwork != \"Only on Power Adapter\" ]]; then\r\n  echo \"[Error] An invalid wake one network setting was given '$_arg_wakeOneNetwork'. The only valid options are 'Never', 'Always' and 'Only on Power Adapter'.\" &gt;&amp;2\r\n  exit 1\r\nfi\r\n\r\n# Set screen timeout if provided\r\nif [[ -n $_arg_screenTimeout ]]; then\r\n  timeoutError=\"Failed to set screen timeout of '$_arg_screenTimeout' minutes\"\r\n  timeoutSuccess=\"Successfully set screen timeout of '$_arg_screenTimeout' minutes\"\r\n\r\n  # Call the function to set the power setting for screen timeout\r\n  set_powersetting --setting \"displaysleep\" --value \"$_arg_screenTimeout\" --errorMessage \"$timeoutError\" --successMessage \"$timeoutSuccess\"\r\nfi\r\n\r\n# Set sleep timeout if provided\r\nif [[ -n $_arg_sleepTimeout ]]; then\r\n  sleepError=\"Failed to set sleep timeout of '$_arg_sleepTimeout' minutes\"\r\n  sleepSuccess=\"Successfully set sleep timeout of '$_arg_sleepTimeout' minutes\"\r\n\r\n  # Call the function to set the power setting for sleep timeout\r\n  set_powersetting --setting \"sleep\" --value \"$_arg_sleepTimeout\" --errorMessage \"$sleepError\" --successMessage \"$sleepSuccess\"\r\nfi\r\n\r\n# Set disk timeout if provided\r\nif [[ -n $_arg_diskTimeout ]]; then\r\n  diskError=\"Failed to set disk timeout of '$_arg_diskTimeout' minutes\"\r\n  diskSuccess=\"Successfully set disk timeout of '$_arg_diskTimeout' minutes\"\r\n\r\n  # Call the function to set the power setting for disk timeout\r\n  set_powersetting --setting \"disksleep\" --value \"$_arg_diskTimeout\" --errorMessage \"$diskError\" --successMessage \"$diskSuccess\"\r\nfi\r\n\r\n# Set power nap setting if provided\r\nif [[ -n $_arg_powernap ]]; then\r\n  if [[ $_arg_powernap == \"Enable\" ]]; then\r\n    _powernap_setting=1\r\n    napError=\"Failed to enable powernap\"\r\n    napSuccess=\"Successfully enabled powernap\"\r\n  else\r\n    _powernap_setting=0\r\n    napError=\"Failed to disable powernap\"\r\n    napSuccess=\"Successfully disabled powernap\"\r\n  fi\r\n\r\n  # Call the function to set the power setting for power nap\r\n  set_powersetting --setting \"powernap\" --value \"$_powernap_setting\" --errorMessage \"$napError\" --successMessage \"$napSuccess\"\r\nfi\r\n\r\n# Set terminal keep alive setting if provided\r\nif [[ -n $_arg_terminalKeepAlive ]]; then\r\n  if [[ $_arg_terminalKeepAlive == \"Enable\" ]]; then\r\n    _terminal_setting=1\r\n    terminalError=\"Failed to enable terminal keep alive\"\r\n    terminalSuccess=\"Successfully terminal keep alive\"\r\n  else\r\n    _terminal_setting=0\r\n    terminalError=\"Failed to disable terminal keep alive\"\r\n    terminalSuccess=\"Successfully disabled terminal keep alive\"\r\n  fi\r\n\r\n  # Call the function to set the power setting for terminal keep alive\r\n  set_powersetting --setting \"ttyskeepawake\" --value \"$_terminal_setting\" --errorMessage \"$terminalError\" --successMessage \"$terminalSuccess\"\r\nfi\r\n\r\n# Set the \"dim on battery\" setting if the argument is provided\r\nif [[ -n $_arg_dimOnBattery ]]; then\r\n  if [[ $_arg_dimOnBattery == \"Enable\" ]]; then\r\n    _dim_setting=1\r\n    dimError=\"Failed to enable the setting 'dim on battery'.\"\r\n    dimSuccess=\"Successfully enabled the setting 'dim on battery'.\"\r\n  else\r\n    _dim_setting=0\r\n    dimError=\"Failed to disable the setting 'dim on battery'.\"\r\n    dimSuccess=\"Successfully disabled the setting 'dim on battery'.\"\r\n  fi\r\n\r\n  # Call the function to set the power setting for dim on battery\r\n  set_powersetting --setting \"lessbright\" --value \"$_dim_setting\" --errorMessage \"$dimError\" --successMessage \"$dimSuccess\" --singleSetting\r\nfi\r\n\r\n# Set the low power mode setting if the argument is provided\r\nif [[ -n $_arg_lowPowerMode ]]; then\r\n  if [[ $_arg_lowPowerMode == \"Enable\" ]]; then\r\n    _lpm_setting=1\r\n    lpmError=\"Failed to enable low power mode\"\r\n    lpmSuccess=\"Successfully enabled low power mode\"\r\n  else\r\n    _lpm_setting=0\r\n    lpmError=\"Failed to disable low power mode\"\r\n    lpmSuccess=\"Successfully disabled low power mode\"\r\n  fi\r\n\r\n  # Call the function to set the power setting for low power mode\r\n  set_powersetting --setting \"lowpowermode\" --value \"$_lpm_setting\" --errorMessage \"$lpmError\" --successMessage \"$lpmSuccess\"\r\nfi\r\n\r\n# Set the TCP keep alive setting if the argument is provided\r\nif [[ -n $_arg_tcpKeepAlive ]]; then\r\n  if [[ $_arg_tcpKeepAlive == \"Enable\" ]]; then\r\n    _tcp_setting=1\r\n    tcpError=\"Failed to enable tcp keep alive\"\r\n    tcpSuccess=\"Successfully enabled tcp keep alive\"\r\n  else\r\n    _tcp_setting=0\r\n    tcpError=\"Failed to disable tcp keep alive\"\r\n    tcpSuccess=\"Successfully disabled tcp keep alive\"\r\n  fi\r\n\r\n  # Call the function to set the power setting for TCP keep alive\r\n  set_powersetting --setting \"tcpkeepalive\" --value \"$_tcp_setting\" --errorMessage \"$tcpError\" --successMessage \"$tcpSuccess\"\r\nfi\r\n\r\n# Set the wake on network setting if the argument is provided\r\nif [[ -n $_arg_wakeOneNetwork ]]; then\r\n  case $_arg_wakeOneNetwork in\r\n  \"Never\")\r\n    pmset -a womp 0\r\n    ;;\r\n  \"Always\")\r\n    pmset -a womp 1\r\n    ;;\r\n  \"Only on Power Adapter\")\r\n    if [[ -n $batteryOptions ]]; then\r\n      pmset -b womp 0\r\n    fi\r\n    pmset -c womp 1\r\n    ;;\r\n  esac\r\n\r\n  # Retrieve and verify the new value of the wake on network setting\r\n  newvalue=$(pmset -g custom | grep -w \"womp\")\r\n  if [[ -z $batteryOptions ]]; then\r\n    # Get the new value for the \"Plugged In\" policy if no battery options\r\n    newvalueAC=$(echo \"$newvalue\" | xargs | cut -f2 -d' ')\r\n  else\r\n    # Get the new values for both \"Battery\" and \"Plugged In\" policies\r\n    newvalueDC=$(echo \"$newvalue\" | xargs | cut -f2 -d' ')\r\n    newvalueAC=$(echo \"$newvalue\" | xargs | cut -f4 -d' ')\r\n  fi\r\n\r\n  # Check and report the new value for \"Always\" and \"Only on Power Adapter\" settings\r\n  if [[ $_arg_wakeOneNetwork == \"Always\" || $_arg_wakeOneNetwork == \"Only on Power Adapter\" ]]; then\r\n    if [[ $newvalueAC != 1 ]]; then\r\n      echo \"[Error] Unable to enable wake on network on the 'Plugged In' Policy.\" &gt;&amp;2\r\n      EXITCODE=1\r\n    else\r\n      echo \"Successfully enabled wake on network on the 'Plugged In' Policy.\"\r\n    fi\r\n  fi\r\n\r\n  # Check and report the new value for \"Never\" setting\r\n  if [[ $_arg_wakeOneNetwork == \"Never\" ]]; then\r\n    if [[ $newvalueAC != 0 ]]; then\r\n      echo \"[Error] Unable to disable wake on network on the 'Plugged In' Policy.\" &gt;&amp;2\r\n      EXITCODE=1\r\n    else\r\n      echo \"Successfully disabled wake on network on the 'Plugged In' Policy.\"\r\n    fi\r\n  fi\r\n\r\n  # Check and report the new value for \"Always\" setting on battery\r\n  if [[ -n $batteryOptions &amp;&amp; $_arg_wakeOneNetwork == \"Always\" ]]; then\r\n    if [[ $newvalueDC != 1 ]]; then\r\n      echo \"[Error] Unable to enable wake on network on the 'Battery' Policy.\" &gt;&amp;2\r\n      EXITCODE=1\r\n    else\r\n      echo \"Successfully enabled wake on network on the 'Battery' Policy.\"\r\n    fi\r\n  fi\r\n\r\n  # Check and report the new value for \"Never\" and \"Only on Power Adapter\" settings on battery\r\n  if [[ -n $batteryOptions &amp;&amp; ($_arg_wakeOneNetwork == \"Never\" || $_arg_wakeOneNetwork == \"Only on Power Adapter\") ]]; then\r\n    if [[ $newvalueDC != 0 ]]; then\r\n      echo \"[Error] Unable to disable wake on network on the 'Battery' Policy.\" &gt;&amp;2\r\n      EXITCODE=1\r\n    else\r\n      echo \"Successfully disabled wake on network on the 'Battery' Policy.\"\r\n    fi\r\n  fi\r\nfi\r\n\r\n# Exit the script with the set exit code if it is defined\r\nif [[ -n $EXITCODE ]]; then\r\n  exit \"$EXITCODE\"\r\nfi\r\n<\/pre>\n<p>&nbsp;<\/p>\n\n<div class=\"in-context-cta\"><p>Gagnez du temps gr\u00e2ce \u00e0 plus de 300 scripts du Dojo NinjaOne.<\/p>\n<p>\u2192 <a class=\"waffle-rich-text-link\" href=\"https:\/\/www.ninjaone.com\/fr\/phase-de-test-gratuit\/\">Obtenir l&rsquo;acc\u00e8s<\/a><\/p>\n<\/div>\n<h2>Ce que fait ce script<\/h2>\n<p>Ce script est un outil complet pour g\u00e9rer les param\u00e8tres d&rsquo;alimentation de macOS. Il prend en charge des options pour les modes batterie et branch\u00e9, en tenant compte de param\u00e8tres tels que\u00a0:<\/p>\n<ul>\n<li><strong>D\u00e9lai de mise en veille de l&rsquo;\u00e9cran<\/strong>\u00a0: d\u00e9lai avant que l&rsquo;\u00e9cran ne s&rsquo;\u00e9teigne.<\/li>\n<li><strong>D\u00e9lai de mise en veille du syst\u00e8me<\/strong>\u00a0: d\u00e9lai avant que le syst\u00e8me ne passe en mode veille.<\/li>\n<li><strong>Ralentissement du disque<\/strong> : d\u00e9lai avant que le disque ne s&rsquo;\u00e9teigne en cas d&rsquo;inactivit\u00e9.<\/li>\n<li><strong>Power Nap<\/strong> : met \u00e0 jour les donn\u00e9es du syst\u00e8me pendant la veille.<\/li>\n<li><strong>Mode basse consommation<\/strong> : r\u00e9duit la consommation d&rsquo;\u00e9nergie et prolonge la dur\u00e9e de vie de la batterie.<\/li>\n<li><strong>TCP Keepalive<\/strong> : maintient les connexions r\u00e9seau actives.<\/li>\n<li><strong>R\u00e9activation sur le r\u00e9seau<\/strong> : r\u00e9active l&rsquo;appareil \u00e0 la r\u00e9ception d&rsquo;un paquet r\u00e9seau.<\/li>\n<\/ul>\n<p>Le script utilise la commande pmset pour configurer ces param\u00e8tres, avec une gestion des erreurs et une v\u00e9rification am\u00e9lior\u00e9es pour garantir une application r\u00e9ussie.<\/p>\n<h2>Comment fonctionne le script<\/h2>\n<h3>Analyse des arguments<\/h3>\n<p>Le script accepte divers arguments de ligne de commande, tels que &#8211;screenTimeout, &#8211;sleepTimeout et &#8211;lowPowerMode. Chaque param\u00e8tre peut \u00eatre adapt\u00e9 pour appliquer des r\u00e9glages \u00e0 l&rsquo;alimentation par batterie, au mode branch\u00e9 ou aux deux. Si des arguments incorrects ou non valides sont fournis, le script affiche des messages d&rsquo;erreur d\u00e9taill\u00e9s.<\/p>\n<h3>Validation des param\u00e8tres<\/h3>\n<p>Avant d&rsquo;appliquer les modifications, le script valide les valeurs saisies pour garantir la compatibilit\u00e9 avec macOS. Par exemple\u00a0:<\/p>\n<ul>\n<li>Les valeurs des d\u00e9lais de mise en veille de l&rsquo;\u00e9cran doivent \u00eatre des nombres entiers non n\u00e9gatifs.<\/li>\n<li>Les seuls param\u00e8tres disponibles pour Power Nap \u00ab\u00a0Activer\u00a0\u00bb ou \u00ab\u00a0D\u00e9sactiver.\u00a0\u00bb<\/li>\n<li>Le comportement de r\u00e9activation sur le r\u00e9seau doit s&rsquo;aligner sur les capacit\u00e9s de macOS.<\/li>\n<\/ul>\n<h3>Cas d&rsquo;utilisation hypoth\u00e9tique<\/h3>\n<p>Imaginez un administrateur informatique g\u00e9rant un parc d&rsquo;ordinateurs portables macOS dans un environnement d&rsquo;entreprise. Il veut s&rsquo;assurer que\u00a0:<\/p>\n<ul>\n<li>les \u00e9crans s&rsquo;\u00e9teignent apr\u00e8s 5 minutes d&rsquo;inactivit\u00e9 pour \u00e9conomiser de l&rsquo;\u00e9nergie.<\/li>\n<li>Les appareils passent en mode veille au bout de 10 minutes lorsqu&rsquo;ils fonctionnent sur batterie.<\/li>\n<li>La fonction Power Nap soit d\u00e9sactiv\u00e9e afin de pr\u00e9server les ressources pendant les heures creuses.<\/li>\n<\/ul>\n<p>Cela garantit la coh\u00e9rence et r\u00e9duit le temps de configuration manuelle.<\/p>\n<h2>Comparaison du script avec d&rsquo;autres m\u00e9thodes<\/h2>\n<p>Bien que la commande macOS pmset soit puissante, elle n\u00e9cessite une saisie manuelle et ne valide pas les param\u00e8tres. Les outils bas\u00e9s sur une interface graphique, tels que les pr\u00e9f\u00e9rences syst\u00e8me, proposent une automatisation limit\u00e9e et ne prennent pas en charge la configuration par lots. Ce script comble le foss\u00e9 en combinant la flexibilit\u00e9 de pmset avec la facilit\u00e9 de l&rsquo;automatisation.<\/p>\n<h2>Questions fr\u00e9quentes sur le script pour les param\u00e8tres d&rsquo;alimentation macOS<\/h2>\n<h3>Ce script peut-il fonctionner sur des appareils sans batterie\u00a0?<\/h3>\n<p>Oui, mais les param\u00e8tres sp\u00e9cifiques \u00e0 la batterie seront ignor\u00e9s. Le script d\u00e9tecte la disponibilit\u00e9 de la batterie et \u00e9met des avertissements si des options li\u00e9es \u00e0 la batterie sont utilis\u00e9es sur des appareils qui en sont d\u00e9pourvus.<\/p>\n<h3>Que se passe-t-il si je fournis des arguments non valables\u00a0?<\/h3>\n<p>Le script valide toutes les entr\u00e9es et fournit des messages d&rsquo;erreur d\u00e9taill\u00e9s si un argument est incorrect.<\/p>\n<h3>L&rsquo;utilisation de Power Nap est-elle sans danger\u00a0?<\/h3>\n<p>Power Nap peut consommer des ressources suppl\u00e9mentaires pendant la veille. Il est recommand\u00e9 de le d\u00e9sactiver dans les environnements n\u00e9cessitant une efficacit\u00e9 \u00e9nerg\u00e9tique maximale.<\/p>\n<h2>Implications plus larges pour la s\u00e9curit\u00e9 et l&rsquo;efficacit\u00e9 des technologies de l&rsquo;information<\/h2>\n<p>La gestion automatis\u00e9e de l&rsquo;alimentation contribue \u00e0 l&rsquo;efficacit\u00e9 \u00e9nerg\u00e9tique et r\u00e9duit l&#8217;empreinte carbone, conform\u00e9ment aux objectifs de d\u00e9veloppement durable des entreprises. De plus, des fonctions telles que la d\u00e9sactivation de Power Nap peuvent renforcer la s\u00e9curit\u00e9 en r\u00e9duisant les processus d&rsquo;arri\u00e8re-plan et les vecteurs d&rsquo;attaque potentiels.<\/p>\n<h2>Bonnes pratiques de l&rsquo;utilisation du script<\/h2>\n<ol>\n<li><strong>Configurations des tests<\/strong>\u00a0: ex\u00e9cutez le script sur un appareil de test avant de le d\u00e9ployer pour vous assurer que les param\u00e8tres se comportent comme pr\u00e9vu.<\/li>\n<li><strong>Modifications du document<\/strong>\u00a0: conservez un journal des param\u00e8tres appliqu\u00e9s \u00e0 des fins de d\u00e9pannage et de conformit\u00e9.<\/li>\n<li><strong>Contr\u00f4le des performances<\/strong>\u00a0: v\u00e9rifiez r\u00e9guli\u00e8rement que les appareils respectent les param\u00e8tres d&rsquo;alimentation configur\u00e9s.<\/li>\n<\/ol>\n<h2>Conclusion<\/h2>\n<p>La gestion de l&rsquo;alimentation est un aspect essentiel de l&rsquo;administration du syst\u00e8me macOS. Ce script simplifie le processus, permettant aux professionnels de l&rsquo;informatique de configurer et d&rsquo;appliquer efficacement les param\u00e8tres d&rsquo;alimentation. Pour les MSP et les entreprises qui cherchent \u00e0 rationaliser la gestion des appareils macOS, des solutions telles que\u00a0<strong>NinjaOne<\/strong>\u00a0proposent des outils de surveillance et d&rsquo;automatisation avanc\u00e9s, garantissant que les appareils restent optimis\u00e9s et s\u00e9curis\u00e9s.<\/p>\n","protected":false},"author":35,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4210],"use_cases":[4281],"class_list":["post-392562","script_hub","type-script_hub","status-publish","hentry","script_hub_category-macos","use_cases-configuration-generale"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub\/392562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/comments?post=392562"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/media?parent=392562"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/operating_system?post=392562"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/use_cases?post=392562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}