# Buggy code
def info(name):
    """
    Get information about a service on the system

    Args:
        name (str): The name of the service. This is not the display name. Use
            ``get_service_name`` to find the service name.

    Returns:
        dict: A dictionary containing information about the service.

    CLI Example:

    .. code-block:: bash

        salt '*' service.info spooler
    """
    try:
        handle_scm = win32service.OpenSCManager(
            None, None, win32service.SC_MANAGER_CONNECT
        )
    except pywintypes.error as exc:
        raise CommandExecutionError("Failed to connect to the SCM: {0}".format(exc[2]))

    try:
        handle_svc = win32service.OpenService(
            handle_scm,
            name,
            win32service.SERVICE_ENUMERATE_DEPENDENTS
            | win32service.SERVICE_INTERROGATE
            | win32service.SERVICE_QUERY_CONFIG
            | win32service.SERVICE_QUERY_STATUS,
        )
    except pywintypes.error as exc:
        raise CommandExecutionError("Failed To Open {0}: {1}".format(name, exc[2]))

    try:
        config_info = win32service.QueryServiceConfig(handle_svc)
        status_info = win32service.QueryServiceStatusEx(handle_svc)

        try:
            description = win32service.QueryServiceConfig2(
                handle_svc, win32service.SERVICE_CONFIG_DESCRIPTION
            )
        except pywintypes.error:
            description = "Failed to get description"

        delayed_start = win32service.QueryServiceConfig2(
            handle_svc, win32service.SERVICE_CONFIG_DELAYED_AUTO_START_INFO
        )
    finally:
        win32service.CloseServiceHandle(handle_scm)
        win32service.CloseServiceHandle(handle_svc)

    ret = dict()
    try:
        sid = win32security.LookupAccountName("", "NT Service\\{0}".format(name))[0]
        ret["sid"] = win32security.ConvertSidToStringSid(sid)
    except pywintypes.error:
        ret["sid"] = "Failed to get SID"

    ret["BinaryPath"] = config_info[3]
    ret["LoadOrderGroup"] = config_info[4]
    ret["TagID"] = config_info[5]
    ret["Dependencies"] = config_info[6]
    ret["ServiceAccount"] = config_info[7]
    ret["DisplayName"] = config_info[8]
    ret["Description"] = description
    ret["Status_ServiceCode"] = status_info["ServiceSpecificExitCode"]
    ret["Status_CheckPoint"] = status_info["CheckPoint"]
    ret["Status_WaitHint"] = status_info["WaitHint"]
    ret["StartTypeDelayed"] = delayed_start

    flags = list()
    for bit in SERVICE_TYPE:
        if isinstance(bit, int):
            if config_info[0] & bit:
                flags.append(SERVICE_TYPE[bit])

    ret["ServiceType"] = flags if flags else config_info[0]

    flags = list()
    for bit in SERVICE_CONTROLS:
        if status_info["ControlsAccepted"] & bit:
            flags.append(SERVICE_CONTROLS[bit])

    ret["ControlsAccepted"] = flags if flags else status_info["ControlsAccepted"]

    try:
        ret["Status_ExitCode"] = SERVICE_ERRORS[status_info["Win32ExitCode"]]
    except KeyError:
        ret["Status_ExitCode"] = status_info["Win32ExitCode"]

    try:
        ret["StartType"] = SERVICE_START_TYPE[config_info[1]]
    except KeyError:
        ret["StartType"] = config_info[1]

    try:
        ret["ErrorControl"] = SERVICE_ERROR_CONTROL[config_info[2]]
    except KeyError:
        ret["ErrorControl"] = config_info[2]

    try:
        ret["Status"] = SERVICE_STATE[status_info["CurrentState"]]
    except KeyError:
        ret["Status"] = status_info["CurrentState"]

    return ret


# Traceback (TypeError)
c:\salt\salt-call -l debug --local service.restart postgresql-x64-9.6
winion-0:
[DEBUG   ] Reading configuration from c:\salt\conf\minion
[DEBUG   ] Including configuration from 'c:\salt\conf\minion.d\_schedule.conf'
[DEBUG   ] Reading configuration from c:\salt\conf\minion.d\_schedule.conf
[DEBUG   ] Using cached minion ID from c:\salt\conf\minion_id: winion-0
[DEBUG   ] Configuration file path: c:\salt\conf\minion
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[DEBUG   ] Grains refresh requested. Refreshing grains.
[DEBUG   ] Reading configuration from c:\salt\conf\minion
[DEBUG   ] Including configuration from 'c:\salt\conf\minion.d\_schedule.conf'
[DEBUG   ] Reading configuration from c:\salt\conf\minion.d\_schedule.conf
[DEBUG   ] Determining pillar cache
[DEBUG   ] LazyLoaded jinja.render
[DEBUG   ] LazyLoaded yaml.render
[DEBUG   ] LazyLoaded jinja.render
[DEBUG   ] LazyLoaded yaml.render
[DEBUG   ] LazyLoaded service.restart
[DEBUG   ] LazyLoaded cmd.run
[INFO    ] Executing command ['net', 'stop', '/y', 'postgresql-x64-9.6'] in directory 'C:\Windows\system32\config\systemprofile'
[ERROR   ] Command '['net', 'stop', '/y', 'postgresql-x64-9.6']' failed with return code: 2
[ERROR   ] stdout: The postgresql-x64-9.6 service is not started.



More help is available by typing NET HELPMSG 3521.
[ERROR   ] retcode: 2
[ERROR   ] Command '['net', 'stop', '/y', 'postgresql-x64-9.6']' failed with return code: 2
[ERROR   ] output: The postgresql-x64-9.6 service is not started.



More help is available by typing NET HELPMSG 3521.

Passed invalid arguments: 'error' object does not support indexing.

Usage:
Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 449, in stop
win32serviceutil.StopService(name)
File "c:\salt\bin\lib\site-packages\win32\lib\win32serviceutil.py", line 409, in StopService
return ControlService(serviceName, win32service.SERVICE_CONTROL_STOP, machine)
File "c:\salt\bin\lib\site-packages\win32\lib\win32serviceutil.py", line 320, in ControlService
status = win32service.ControlService(hs, code)
pywintypes.error: (1062, 'ControlService', 'The service has not been started.')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\cli\caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 488, in restart
return stop(name) and start(name)
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 451, in stop
if exc[0] != 1062:
TypeError: 'error' object does not support indexing

Restart the named service. This issues a stop command followed by a start.

Args:
name: The name of the service to restart.

.. note::
If the name passed is ``salt-minion`` a scheduled task is created and
executed to restart the salt-minion service.

Returns:
bool: ``True`` if successful, ``False`` otherwise

CLI Example:

.. code-block:: bash

salt '*' service.restart <service name>

# Fixed code
def info(name):
    """
    Get information about a service on the system

    Args:
        name (str): The name of the service. This is not the display name. Use
            ``get_service_name`` to find the service name.

    Returns:
        dict: A dictionary containing information about the service.

    CLI Example:

    .. code-block:: bash

        salt '*' service.info spooler
    """
    try:
        handle_scm = win32service.OpenSCManager(
            None, None, win32service.SC_MANAGER_CONNECT
        )
    except pywintypes.error as exc:
        raise CommandExecutionError(
            "Failed to connect to the SCM: {0}".format(exc.strerror)
        )

    try:
        handle_svc = win32service.OpenService(
            handle_scm,
            name,
            win32service.SERVICE_ENUMERATE_DEPENDENTS
            | win32service.SERVICE_INTERROGATE
            | win32service.SERVICE_QUERY_CONFIG
            | win32service.SERVICE_QUERY_STATUS,
        )
    except pywintypes.error as exc:
        raise CommandExecutionError(
            "Failed To Open {0}: {1}".format(name, exc.strerror)
        )

    try:
        config_info = win32service.QueryServiceConfig(handle_svc)
        status_info = win32service.QueryServiceStatusEx(handle_svc)

        try:
            description = win32service.QueryServiceConfig2(
                handle_svc, win32service.SERVICE_CONFIG_DESCRIPTION
            )
        except pywintypes.error:
            description = "Failed to get description"

        delayed_start = win32service.QueryServiceConfig2(
            handle_svc, win32service.SERVICE_CONFIG_DELAYED_AUTO_START_INFO
        )
    finally:
        win32service.CloseServiceHandle(handle_scm)
        win32service.CloseServiceHandle(handle_svc)

    ret = dict()
    try:
        sid = win32security.LookupAccountName("", "NT Service\\{0}".format(name))[0]
        ret["sid"] = win32security.ConvertSidToStringSid(sid)
    except pywintypes.error:
        ret["sid"] = "Failed to get SID"

    ret["BinaryPath"] = config_info[3]
    ret["LoadOrderGroup"] = config_info[4]
    ret["TagID"] = config_info[5]
    ret["Dependencies"] = config_info[6]
    ret["ServiceAccount"] = config_info[7]
    ret["DisplayName"] = config_info[8]
    ret["Description"] = description
    ret["Status_ServiceCode"] = status_info["ServiceSpecificExitCode"]
    ret["Status_CheckPoint"] = status_info["CheckPoint"]
    ret["Status_WaitHint"] = status_info["WaitHint"]
    ret["StartTypeDelayed"] = delayed_start

    flags = list()
    for bit in SERVICE_TYPE:
        if isinstance(bit, int):
            if config_info[0] & bit:
                flags.append(SERVICE_TYPE[bit])

    ret["ServiceType"] = flags if flags else config_info[0]

    flags = list()
    for bit in SERVICE_CONTROLS:
        if status_info["ControlsAccepted"] & bit:
            flags.append(SERVICE_CONTROLS[bit])

    ret["ControlsAccepted"] = flags if flags else status_info["ControlsAccepted"]

    try:
        ret["Status_ExitCode"] = SERVICE_ERRORS[status_info["Win32ExitCode"]]
    except KeyError:
        ret["Status_ExitCode"] = status_info["Win32ExitCode"]

    try:
        ret["StartType"] = SERVICE_START_TYPE[config_info[1]]
    except KeyError:
        ret["StartType"] = config_info[1]

    try:
        ret["ErrorControl"] = SERVICE_ERROR_CONTROL[config_info[2]]
    except KeyError:
        ret["ErrorControl"] = config_info[2]

    try:
        ret["Status"] = SERVICE_STATE[status_info["CurrentState"]]
    except KeyError:
        ret["Status"] = status_info["CurrentState"]

    return ret