Skip to main content

"The trust relationship between this workstation and the primary domain failed." issue, and PowerShell Script to monitor a specific event log and copy some logs to other location on Windows Server

Hi IT Support folks,

I hope that you are doing well.

The reason to write this post to share the incident that I came across recently while working in a multi-domain environment at one of the major banks here in Canada where one of the member servers stopped communicating with the domain intermittently and throwing the error, "The trust relationship between this workstation and the primary domain failed." when user was trying to log into it using his domain credentials. In other words, this issue was seen when the session logon was attempted through Remote Desktop Protocol.

This can happen due to a number of reasons.

  • More than one host with the same name in Active Directory Domain environment.
  • DNS issues due to which the member server cannot communicate securely with the Active Directory domain controller to have its computer account password reset. As a result, the computer account password used by the member server is different than the computer account password used by the AD.

The simple fix would be to reset the computer account password to resolve the issue, if there are not more than one host with the same name in an AD environment. There are different ways to reset the computer account password.

1. Use dsa.msc (Active Directory Users and Computers console). If you are resetting the password from your own computer, you should have RSAT (Remote Server Administration Tools) installed on your computer. You should also have proper privilege to reset the computer account.


2. You can also use the PowerShell Cmdlets to achieve the same result.

Reset-ComputerMachinePassword -Server "DC01" -Credential Domain01\Admin01

For more information on this Cmdlet, visit https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/reset-computermachinepassword?view=powershell-5.1

Once, the computer account password is reset, you can use another PowerShell Cmdlet to test and repair the secure channel between the member server and its domain.

Test-ComputerSecureChannel -server DCName.fabrikam.com

Test-ComputerSecureChannel -repair

For more information on the above Cmdlet, visit https://docs.microsoft.com/en-us/previous-versions/powershell/module/Microsoft.PowerShell.Management/Test-ComputerSecureChannel?view=powershell-3.0

3. You can also remove the member server from domain and rejoin it with domain to have its computer account password reset, but this method is risky or say NOT recommended especially for resetting password for servers like Exchange Servers.

4. You can also use another utility called "nltest" for testing and resetting the secure channel between the member server and the domain controller. Please refer to https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/cc731935(v=ws.11)?redirectedfrom=MSDN.

5. There's one more command line utility called netdom.exe that you can also use to reset the computer account password. For step-by-step to use this utility, refer to https://support.microsoft.com/en-us/help/325850/how-to-use-netdom-exe-to-reset-machine-account-passwords-of-a-windows.

If computer account password reset does not fix the issue, you can enable the Netlogon logs on the machine on which you have the trust relationship issue.

Just as an FYI, The machine account password change is initiated by the computer every 30 days by default . Since Windows 2000, all versions of Windows have the same value. This behavior can be modified to a custom value using the following group policy setting in Active Directory.

Domain member: Maximum machine account password age
You can configure this security setting by opening the appropriate policy and expanding the console tree as such:

Computer Configuration\Windows Settings\Security Settings\Local Policies\Security Options

It is important to remember that machine account password changes are driven by the CLIENT (computer), and not the AD. The Netlogon service on the client computer is responsible for doing this.

To enable Netlogon logging:

  1. Open a Command Prompt window (administrative Command Prompt window for Windows Server 2008 and later versions).
  2. Type the following command, and then press Enter:

                 Nltest /DBFlag:2080FFFF

It's typically not necessary to stop and restart the Netlogon service for Windows 2000 Server/Professional or later operating systems to enable Netlogon logging. Netlogon-related activity is logged to %windir%\debug\netlogon.log.

Setting the maximum log file size for Netlogon logs: 

The MaximumLogFileSize registry entry can be used to specify the maximum size of the Netlogon.log file. By default, this registry entry does not exist, and the default maximum size of the Netlogon.log file is 20 MB. When the file reaches 20 MB, it's renamed to Netlogon.bak, and a new Netlogon.log file is created. This registry entry has the following parameters:

Path: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters
Value Name: MaximumLogFileSize
Value Type: REG_DWORD
Value Data: <maximum log file size in bytes>

There was one application event id 1309 which was being logged on this problem server when it wasn't able to communicate with the AD DC when trying to rdp into it. I also opened a case with Microsoft Support to find the root cause of this issue, and as advised by them, I had to monitor this particular application event, and when it is logged, I had to copy the contents of %windir%\debug\ to some other location, so the logs files within it do not get overwritten.

To accomplish this task, I wrote the following PowerShell Script, which I scheduled to trigger at every 5 mins on the problem server. It checks to see, if the application event id 1309 was logged in last 5 mins. If so, it copies the contents of "C:\Windows\debug" to Destination Directory in D: drive.

$Begin = (Get-Date).AddMinutes(-5)
$End = Get-Date
$Logs = Get-EventLog -LogName Application -EntryType Warning -Source "ASP.NET 4.0.30319.0" -After $Begin -Before $End | where {($_.EventID -eq 1309) -and ($_.Category -eq "Web Event")}
if($Logs -ne $Null)
{
                $Logs >> D:\Script\EventLog1309.txt
                $DateStr = $($End).tostring("MM-dd-yyyy-h-m")
                $DestDir = "D:\Script\Debug" + "$($DateStr)"
                Copy-Item -Path "C:\Windows\debug\*"  -Destination $DestDir -Recurse
}
else
{
                "No Application Warning Event 1309 was logged between $($Begin) and $($End)." + [char]13 + [char]10 >> D:\Script\EventLog1309.txt
}

You can use the similar PowerShell script, if you need to monitor any specific event log id. Save the above script in a file with .ps1 extension.

I hope that this has been informative for you.

Thank you,

Nirav Soni

Some links for further reading:
1. https://blogs.technet.microsoft.com/askds/2009/02/15/machine-account-password-process-2/
2. https://support.microsoft.com/en-ca/help/109626/enabling-debug-logging-for-the-netlogon-service

Comments

Popular posts from this blog

Microsoft Azure Administrator (AZ-104) Course in Hindi

 Below in the link for the YouTube Video series for the Microsoft Azure Administrator (AZ-104) course in Hindi. https://www.youtube.com/watch?v=fqnsqJoWyjM This video is the first video in this series teaching you the Microsoft Azure Administrator (AZ-104) course in Hindi. I hope, you will enjoy this series, and I am sure that it will help you prepare for your Microsoft Azure Administrator (AZ-104) certification.  If you are enjoying my videos, please like and share them. Please also subscribe to my channel to get notified about the new videos that I publish. Thank you, Nirav Soni

Working with Server Core machine

Before I talk about the Server Core, I would, first of all, list the editions of Windows Server 2012 R2. There are 4 editions in Windows Server 2012 R2. Data Center Standard Essentials Foundation They can be installed with two states - either GUI or without GUI i.e. Server Core. Actually, there's also a third state here and it's kind of the middle ground between Server Core and Server with the GUI and that is the Minimal Server Interface. Server Core is being installed mostly for Domain Controllers, DHCP, Hyper-V and DNS. Server Core only has Command Line and PowerShell. All of those user interface elements that you're used to in Server with GUI like the Microsoft Management Console, Server Manager, Explorer, all of those things are gone. The whole idea here is that it contains a much smaller footprint. It's actually four gigabytes smaller than server with a GUI. This is going to reduce the potential attack surface too. So, it's smaller and more secure....

Setup Wireshark capturing for a remote Windows server

Many times, while working as a Systems Administrator providing support for the Windows or Linux Infrastructure, you come across some issues where you may suspect that those issues might be causing on some server(s) due to network related issues. It might be a DNS related issues where DNS queries do not resolve some times for some unknown reason or it could be a case where remote user who establishes a VPN connection and assume that his/her domain user password is about to expire soon and he or she needs to reset the password over VPN connection, but password cannot be reset because the TCP port 464 is blocked. These are just some examples of the incidents when you would think to capture network traffic on some servers to find the root cause. Below, you will find the step-by-step instructions showing you how you can use the Wireshark to capture the network traffic for a remote server. You can capture the network traffic from within the server too on which you have issues, but some t...