static void SetLogonAsAServicePrivilege(UserAccount userAccount)
        {
            if (!userAccount.Domain.Equals("NT AUTHORITY", StringComparison.OrdinalIgnoreCase))
            {
                var privileges = Lsa.GetPrivileges(userAccount.QualifiedName).ToList();

                if (!privileges.Contains(LogonPrivileges.LogonAsAService, StringComparer.OrdinalIgnoreCase))
                {
                    privileges.Add(LogonPrivileges.LogonAsAService);
                    Lsa.GrantPrivileges(userAccount.QualifiedName, privileges.ToArray());
                }
            }
        }
        public void ChangeAccountDetails(string serviceAccount, string servicePassword)
        {
            var userAccount = UserAccount.ParseAccountName(serviceAccount);

            if (!(userAccount.IsLocalService() || userAccount.IsLocalService()))
            {
                var privileges = Lsa.GetPrivileges(userAccount.QualifiedName).ToList();
                if (!privileges.Contains(LogonPrivileges.LogonAsAService, StringComparer.OrdinalIgnoreCase))
                {
                    privileges.Add(LogonPrivileges.LogonAsAService);
                    Lsa.GrantPrivileges(userAccount.QualifiedName, privileges.ToArray());
                }
            }

            var objPath = $"Win32_Service.Name='{ServiceName}'";

            using (var win32Service = new ManagementObject(new ManagementPath(objPath)))
            {
                var inParams = win32Service.GetMethodParameters("Change");
                inParams["StartName"]     = ConvertAccountNameToServiceAccount(userAccount.QualifiedName);
                inParams["StartPassword"] = servicePassword;

                var outParams = win32Service.InvokeMethod("Change", inParams, null);
                if (outParams == null)
                {
                    throw new ManagementException($"Failed to set account credentials service {ServiceName}");
                }

                var wmiReturnCode = Convert.ToInt32(outParams["ReturnValue"]);
                if (wmiReturnCode != 0)
                {
                    var message = (wmiReturnCode < Win32ChangeErrorMessages.Length)
                        ? $"Failed to change service credentials on service {ServiceName} - {Win32ChangeErrorMessages[wmiReturnCode]}"
                        : "An unknown error occurred";

                    if (wmiReturnCode == 22)
                    {
                        message += $"( AccountName {userAccount.QualifiedName} converted to {ConvertAccountNameToServiceAccount(userAccount.QualifiedName)})";
                    }

                    throw new ManagementException(message);
                }
            }
        }