# Buggy code
def _buildKnownDataSearchString(
    reg_key, reg_valueName, reg_vtype, reg_data, check_deleted=False
):
    """
    helper function similar to _processValueItem to build a search string for a
    known key/value/type/data
    """
    registry = Registry()
    this_element_value = None
    expected_string = ""
    if reg_data and not check_deleted:
        if reg_vtype == "REG_DWORD":
            this_element_value = ""
            for v in struct.unpack("2H", struct.pack("I", int(reg_data))):
                this_element_value = this_element_value + unichr(v)
        elif reg_vtype == "REG_QWORD":
            this_element_value = ""
            for v in struct.unpack("4H", struct.pack("I", int(reg_data))):
                this_element_value = this_element_value + unichr(v)
        elif reg_vtype == "REG_SZ":
            this_element_value = "{0}{1}".format(reg_data, chr(0))
    if check_deleted:
        reg_vtype = "REG_SZ"
        expected_string = "[{1}{0};**del.{2}{0};{3}{0};{4}{0};{5}{0}]".format(
            chr(0),
            reg_key,
            reg_valueName,
            chr(registry.vtype[reg_vtype]),
            unichr(len(" {0}".format(chr(0)).encode("utf-16-le"))),
            " ",
        )
    else:
        expected_string = "[{1}{0};{2}{0};{3}{0};{4}{0};{5}]".format(
            chr(0),
            reg_key,
            reg_valueName,
            chr(registry.vtype[reg_vtype]),
            unichr(len(this_element_value.encode("utf-16-le"))),
            this_element_value,
        )
    return expected_string


# Traceback (UnicodeEncodeError)
win2016-3:
----------
ID: router_gpo_1
Function: lgpo.set
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\state.py", line 1843, in call
**cdata['kwargs'])
File "c:\salt\bin\lib\site-packages\salt\loader.py", line 1795, in wrapper
return f(*args, **kwargs)
File "c:\salt\bin\lib\site-packages\salt\states\win_lgpo.py", line 244, in set_
hierarchical_return=False)
File "c:\salt\var\cache\salt\minion\extmods\modules\win_lgpo.py", line 5021, in get
return_not_configured=return_not_configured))
File "c:\salt\var\cache\salt\minion\extmods\modules\win_lgpo.py", line 3810, in _checkAllAdmxPolicies
policy_filedata):
File "c:\salt\var\cache\salt\minion\extmods\modules\win_lgpo.py", line 3250, in _checkValueItemParent
check_deleted=check_deleted)
File "c:\salt\var\cache\salt\minion\extmods\modules\win_lgpo.py", line 3501, in _processValueItem
six.unichr(len(this_element_value.encode('utf-16-le'))),
UnicodeEncodeError: 'utf-16-le' codec can't encode character '\udd00' in position 0: surrogates not allowed
Started: 18:44:09.868773
Duration: 6765.204 ms
Changes:

Summary for win2016-3
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   6.765 s

# Fixed code
def _buildKnownDataSearchString(
    reg_key, reg_valueName, reg_vtype, reg_data, check_deleted=False
):
    """
    helper function similar to _processValueItem to build a search string for a
    known key/value/type/data
    """
    registry = Registry()
    this_element_value = None
    expected_string = b""
    encoded_semicolon = ";".encode("utf-16-le")
    encoded_null = chr(0).encode("utf-16-le")
    if reg_key:
        reg_key = reg_key.encode("utf-16-le")
    if reg_valueName:
        reg_valueName = reg_valueName.encode("utf-16-le")
    if reg_data and not check_deleted:
        if reg_vtype == "REG_DWORD":
            this_element_value = struct.pack("I", int(reg_data))
        elif reg_vtype == "REG_QWORD":
            this_element_value = struct.pack("Q", int(reg_data))
        elif reg_vtype == "REG_SZ":
            this_element_value = b"".join([reg_data.encode("utf-16-le"), encoded_null])
    if check_deleted:
        reg_vtype = "REG_SZ"
        expected_string = b"".join(
            [
                "[".encode("utf-16-le"),
                reg_key,
                encoded_null,
                encoded_semicolon,
                "**del.".encode("utf-16-le"),
                reg_valueName,
                encoded_null,
                encoded_semicolon,
                chr(registry.vtype[reg_vtype]).encode("utf-32-le"),
                encoded_semicolon,
                unichr(len(" {0}".format(chr(0)).encode("utf-16-le"))).encode(
                    "utf-32-le"
                ),
                encoded_semicolon,
                " ".encode("utf-16-le"),
                encoded_null,
                "]".encode("utf-16-le"),
            ]
        )
    else:
        expected_string = b"".join(
            [
                "[".encode("utf-16-le"),
                reg_key,
                encoded_null,
                encoded_semicolon,
                reg_valueName,
                encoded_null,
                encoded_semicolon,
                chr(registry.vtype[reg_vtype]).encode("utf-32-le"),
                encoded_semicolon,
                unichr(len(this_element_value)).encode("utf-32-le"),
                encoded_semicolon,
                this_element_value,
                "]".encode("utf-16-le"),
            ]
        )
    return expected_string