For detailed descriptions of the parameters see:
    https://msdn.microsoft.com/en-us/PowerShell/DSC/metaConfig

    config_mode (str): How the LCM applies the configuration. Valid values
        are:

        - ApplyOnly
        - ApplyAndMonitor
        - ApplyAndAutoCorrect

    config_mode_freq (int): How often, in minutes, the current configuration
        is checked and applied. Ignored if config_mode is set to ApplyOnly.
        Default is 15.

    refresh_mode (str): How the LCM gets configurations. Valid values are:

        - Disabled
        - Push
        - Pull

    refresh_freq (int): How often, in minutes, the LCM checks for updated
        configurations. (pull mode only) Default is 30.

    reboot_if_needed (bool): Reboot the machine if needed after a
        configuration is applied. Default is False.

    action_after_reboot (str): Action to take after reboot. Valid values
        are:

        - ContinueConfiguration
        - StopConfiguration

    certificate_id (guid): A GUID that specifies a certificate used to
        access the configuration: (pull mode)

    configuration_id (guid): A GUID that identifies the config file to get
        from a pull server. (pull mode)

    allow_module_overwrite (bool): New configs are allowed to overwrite old
        ones on the target node.

    debug_mode (str): Sets the debug level. Valid values are:

        - None
        - ForceModuleImport
        - All

    status_retention_days (int): Number of days to keep status of the
        current config.

    .. note::
        Either ``config_mode_freq`` or ``refresh_freq`` needs to be a
        multiple of the other. See documentation on MSDN for more details.

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' dsc.set_lcm_config ApplyOnly

def set_lcm_config(config_mode=None,
                   config_mode_freq=None,
                   refresh_freq=None,
                   reboot_if_needed=None,
                   action_after_reboot=None,
                   refresh_mode=None,
                   certificate_id=None,
                   configuration_id=None,
                   allow_module_overwrite=None,
                   debug_mode=False,
                   status_retention_days=None):
    '''
    For detailed descriptions of the parameters see:
    https://msdn.microsoft.com/en-us/PowerShell/DSC/metaConfig

    config_mode (str): How the LCM applies the configuration. Valid values
        are:

        - ApplyOnly
        - ApplyAndMonitor
        - ApplyAndAutoCorrect

    config_mode_freq (int): How often, in minutes, the current configuration
        is checked and applied. Ignored if config_mode is set to ApplyOnly.
        Default is 15.

    refresh_mode (str): How the LCM gets configurations. Valid values are:

        - Disabled
        - Push
        - Pull

    refresh_freq (int): How often, in minutes, the LCM checks for updated
        configurations. (pull mode only) Default is 30.

    reboot_if_needed (bool): Reboot the machine if needed after a
        configuration is applied. Default is False.

    action_after_reboot (str): Action to take after reboot. Valid values
        are:

        - ContinueConfiguration
        - StopConfiguration

    certificate_id (guid): A GUID that specifies a certificate used to
        access the configuration: (pull mode)

    configuration_id (guid): A GUID that identifies the config file to get
        from a pull server. (pull mode)

    allow_module_overwrite (bool): New configs are allowed to overwrite old
        ones on the target node.

    debug_mode (str): Sets the debug level. Valid values are:

        - None
        - ForceModuleImport
        - All

    status_retention_days (int): Number of days to keep status of the
        current config.

    .. note::
        Either ``config_mode_freq`` or ``refresh_freq`` needs to be a
        multiple of the other. See documentation on MSDN for more details.

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' dsc.set_lcm_config ApplyOnly
    '''
    temp_dir = os.getenv('TEMP', '{0}\\temp'.format(os.getenv('WINDIR')))
    cmd = 'Configuration SaltConfig {'
    cmd += '    Node localhost {'
    cmd += '        LocalConfigurationManager {'
    if config_mode:
        if config_mode not in ('ApplyOnly', 'ApplyAndMonitor',
                               'ApplyAndAutoCorrect'):
            error = 'config_mode must be one of ApplyOnly, ApplyAndMonitor, ' \
                    'or ApplyAndAutoCorrect. Passed {0}'.format(config_mode)
            raise SaltInvocationError(error)
        cmd += '            ConfigurationMode = "{0}";'.format(config_mode)
    if config_mode_freq:
        if not isinstance(config_mode_freq, int):
            error = 'config_mode_freq must be an integer. Passed {0}'.format(
                config_mode_freq
            )
            raise SaltInvocationError(error)
        cmd += '            ConfigurationModeFrequencyMins = {0};'.format(config_mode_freq)
    if refresh_mode:
        if refresh_mode not in ('Disabled', 'Push', 'Pull'):
            raise SaltInvocationError(
                'refresh_mode must be one of Disabled, Push, or Pull'
            )
        cmd += '            RefreshMode = "{0}";'.format(refresh_mode)
    if refresh_freq:
        if not isinstance(refresh_freq, int):
            raise SaltInvocationError('refresh_freq must be an integer')
        cmd += '            RefreshFrequencyMins = {0};'.format(refresh_freq)
    if reboot_if_needed is not None:
        if not isinstance(reboot_if_needed, bool):
            raise SaltInvocationError('reboot_if_needed must be a boolean value')
        if reboot_if_needed:
            reboot_if_needed = '$true'
        else:
            reboot_if_needed = '$false'
        cmd += '            RebootNodeIfNeeded = {0};'.format(reboot_if_needed)
    if action_after_reboot:
        if action_after_reboot not in ('ContinueConfiguration',
                                       'StopConfiguration'):
            raise SaltInvocationError(
                'action_after_reboot must be one of '
                'ContinueConfiguration or StopConfiguration'
            )
        cmd += '            ActionAfterReboot = "{0}"'.format(action_after_reboot)
    if certificate_id is not None:
        if certificate_id == '':
            certificate_id = None
        cmd += '            CertificateID = "{0}";'.format(certificate_id)
    if configuration_id is not None:
        if configuration_id == '':
            configuration_id = None
        cmd += '            ConfigurationID = "{0}";'.format(configuration_id)
    if allow_module_overwrite is not None:
        if not isinstance(allow_module_overwrite, bool):
            raise SaltInvocationError('allow_module_overwrite must be a boolean value')
        if allow_module_overwrite:
            allow_module_overwrite = '$true'
        else:
            allow_module_overwrite = '$false'
        cmd += '            AllowModuleOverwrite = {0};'.format(allow_module_overwrite)
    if debug_mode is not False:
        if debug_mode is None:
            debug_mode = 'None'
        if debug_mode not in ('None', 'ForceModuleImport', 'All'):
            raise SaltInvocationError(
                'debug_mode must be one of None, ForceModuleImport, '
                'ResourceScriptBreakAll, or All'
            )
        cmd += '            DebugMode = "{0}";'.format(debug_mode)
    if status_retention_days:
        if not isinstance(status_retention_days, int):
            raise SaltInvocationError('status_retention_days must be an integer')
        cmd += '            StatusRetentionTimeInDays = {0};'.format(status_retention_days)
    cmd += '        }}};'
    cmd += r'SaltConfig -OutputPath "{0}\SaltConfig"'.format(temp_dir)

    # Execute Config to create the .mof
    _pshell(cmd)

    # Apply the config
    cmd = r'Set-DscLocalConfigurationManager -Path "{0}\SaltConfig"' \
          r''.format(temp_dir)
    ret = __salt__['cmd.run_all'](cmd, shell='powershell', python_shell=True)
    __salt__['file.remove'](r'{0}\SaltConfig'.format(temp_dir))
    if not ret['retcode']:
        log.info('DSC: LCM config applied successfully')
        return True
    else:
        log.error('DSC: Failed to apply LCM config. Error %s', ret)
        return False