r'''
    Create a task based on XML. Source can be a file or a string of XML.

    :param str name: The name of the task. This will be displayed in the task
        scheduler.

    :param str location: A string value representing the location in which to
        create the task. Default is '\\' which is the root for the task
        scheduler (C:\Windows\System32\tasks).

    :param str xml_text: A string of xml representing the task to be created.
        This will be overridden by `xml_path` if passed.

    :param str xml_path: The path to an XML file on the local system containing
        the xml that defines the task. This will override `xml_text`

    :param str user_name: The user account under which to run the task. To
        specify the 'System' account, use 'System'. The password will be
        ignored.

    :param str password: The password to use for authentication. This should set
        the task to run whether the user is logged in or not, but is currently
        not working.

    :return: True if successful, False if unsuccessful, A string with the error message if there is an error
    :rtype: bool
    :raises: CommandExecutionError

    CLI Example:

    .. code-block:: bash

        salt 'minion-id' task.create_task_from_xml <task_name> xml_path=C:\task.xml

def create_task_from_xml(name,
                         location='\\',
                         xml_text=None,
                         xml_path=None,
                         user_name='System',
                         password=None):
    r'''
    Create a task based on XML. Source can be a file or a string of XML.

    :param str name: The name of the task. This will be displayed in the task
        scheduler.

    :param str location: A string value representing the location in which to
        create the task. Default is '\\' which is the root for the task
        scheduler (C:\Windows\System32\tasks).

    :param str xml_text: A string of xml representing the task to be created.
        This will be overridden by `xml_path` if passed.

    :param str xml_path: The path to an XML file on the local system containing
        the xml that defines the task. This will override `xml_text`

    :param str user_name: The user account under which to run the task. To
        specify the 'System' account, use 'System'. The password will be
        ignored.

    :param str password: The password to use for authentication. This should set
        the task to run whether the user is logged in or not, but is currently
        not working.

    :return: True if successful, False if unsuccessful, A string with the error message if there is an error
    :rtype: bool
    :raises: CommandExecutionError

    CLI Example:

    .. code-block:: bash

        salt 'minion-id' task.create_task_from_xml <task_name> xml_path=C:\task.xml
    '''
    # Check for existing task
    if name in list_tasks(location):
        # Connect to an existing task definition
        return '{0} already exists'.format(name)

    if not xml_text and not xml_path:
        raise ArgumentValueError('Must specify either xml_text or xml_path')

    # Create the task service object
    with salt.utils.winapi.Com():
        task_service = win32com.client.Dispatch("Schedule.Service")
    task_service.Connect()

    # Load xml from file, overrides xml_text
    # Need to figure out how to load contents of xml
    if xml_path:
        xml_text = xml_path

    # Get the folder to list folders from
    task_folder = task_service.GetFolder(location)

    # Determine logon type
    if user_name:
        if user_name.lower() == 'system':
            logon_type = TASK_LOGON_SERVICE_ACCOUNT
            user_name = 'SYSTEM'
            password = None
        else:
            if password:
                logon_type = TASK_LOGON_PASSWORD
            else:
                logon_type = TASK_LOGON_INTERACTIVE_TOKEN
    else:
        password = None
        logon_type = TASK_LOGON_NONE

    # Save the task
    try:
        task_folder.RegisterTask(name,
                                 xml_text,
                                 TASK_CREATE,
                                 user_name,
                                 password,
                                 logon_type)

    except pythoncom.com_error as error:
        hr, msg, exc, arg = error.args  # pylint: disable=W0633
        error_code = hex(exc[5] + 2**32)
        failure_code = error_code
        fc = {'0x80041319L': 'Required element or attribute missing',
              '0x80041318L': 'Value incorrectly formatted or out of range',
              '0x80020005L': 'Access denied',
              '0x80041309L': "A task's trigger is not found",
              '0x8004130aL': "One or more of the properties required to run this task have not been set",
              '0x8004130cL': "The Task Scheduler service is not installed on this computer",
              '0x8004130dL': "The task object could not be opened",
              '0x8004130eL': "The object is either an invalid task object or is not a task object",
              '0x8004130fL': "No account information could be found in the Task Scheduler security database for the task indicated",
              '0x80041310L': "Unable to establish existence of the account specified",
              '0x80041311L': "Corruption was detected in the Task Scheduler security database; the database has been reset",
              '0x80041313L': "The task object version is either unsupported or invalid",
              '0x80041314L': "The task has been configured with an unsupported combination of account settings and run time options",
              '0x80041315L': "The Task Scheduler Service is not running",
              '0x80041316L': "The task XML contains an unexpected node",
              '0x80041317L': "The task XML contains an element or attribute from an unexpected namespace",
              '0x8004131aL': "The task XML is malformed",
              '0x0004131cL': "The task is registered, but may fail to start. Batch logon privilege needs to be enabled for the task principal",
              '0x8004131dL': "The task XML contains too many nodes of the same type",
              }
        try:
            failure_code = fc[error_code]
        except KeyError:
            failure_code = 'Unknown Failure: {0}'.format(error_code)
        finally:
            log.debug('Failed to create task: %s', failure_code)
        raise CommandExecutionError(failure_code)

    # Verify creation
    return name in list_tasks(location)