from typing import Union


def wmi_object_2_list_of_dict(wmi_objects, depth: int = 1, root: bool = True) -> Union[dict, list]:
    """
    Return a WMI object as a list of dicts, accepts multiple depth
    Example for Win32_LoggedOnUser().Antecedent.AccountType return is [{'Antecedent': {'AccountType': 512}}]
    Hence
    wmi_handle.Win32_LoggedOnUser()[0].Antecedent.AccountType is equivalent of
    res = wmi_object_2_list_of_dict(wmi_handle.Win32_LoggedOnUser(), 2)
    res[0]['Antecedent']['AccountType']
    """
    
    result = []

    if root is False:
        dictionary = {}
        try:
            for attribute in wmi_objects.properties:
                try:
                    if depth > 1:
                        dictionary[attribute] = wmi_object_2_list_of_dict(
                            getattr(wmi_objects, attribute), (depth - 1), root=False)
                    else:
                        dictionary[attribute] = getattr(wmi_objects, attribute)
                except TypeError:
                    dictionary[attribute] = None
            return dictionary
        # wmi_object.properties might just be a string depending on the depth. Just return as is in that case
        except AttributeError:
            return wmi_objects

    for wmi_object in wmi_objects:
        dictionary = {}
        for key in wmi_object.properties.keys():
            if depth <= 1:
                try:
                    dictionary[key] = wmi_object.Properties_(key).Value
                except TypeError:
                    dictionary[key] = None
            else:
                # noinspection PyBroadException
                try:
                    dictionary[key] = wmi_object_2_list_of_dict(getattr(wmi_object, key), (depth - 1), root=False)
                # Some keys won't have attributes and trigger pywintypes.com_error and others. Need for bare except
                except Exception:
                    pass
        result.append(dictionary)
    return result