1

I am trying to create a mechanism (in my case Robocopy wrapped in PowerShell) that will back up IIS logs from all of our web servers into a central CIFS share. As you'd expect, I've had issues with copying the currently open log file, since NTFS doesn't tend to reliably update the LastWriteTime as the file is written to unless all the handles to it are closed.

I'd noticed however that any time I browsed the directory with Windows Explorer not only would the Date Modified update but the next Robocopy run would properly catch the file and bring it current. At an attempt to emulate the Windows Explorer behavior, I ran the following code in PowerShell before invoking the Robocopy job:

$aryFileList = Get-ChildItem $job.SourcePath
ForEach ($file in $aryFileList) {(New-object System.IO.FileInfo $file).LastWriteTime | Out-Null}

This worked for some of my IIS servers and all of my other logging methods (mostly log4net). However, on two of my IIS servers this had no effect, even though they are the same OS and IIS version as others that it worked perfectly on.

This leads me back to the thought; what exactly does Windows Explorer do when you use it to refresh a directory to the point that it updates the Date Modified / LastWriteTime info? Most importantly to my purposes, is there any way that the same thing can be invoked in PowerShell?

Tony Mitera
  • 55
  • 1
  • 1
  • 5
  • Did you try calling `Refresh` method of `System.IO.FileInfo`? – Aziz Kabyshev Mar 19 '16 at 17:10
  • @AzizKabyshev I did, and I get odd results. Doing the Refresh() does update the Length and LastWriteTime for what PowerShell sees. I can then query the same data for the file on the remote share, and will invariably find that file to be both shorter and older. However, despite the Refresh, Robocopy still detects that the file is the same, and with the older length reported. – Tony Mitera Mar 21 '16 at 14:00
  • Oh, now I got it. So the problem is target system caches the metadata, and `Refresh()` refreshes it only for a process, not system-wide. What are the values of these keys in your PC's registry - https://technet.microsoft.com/en-us/library/ff686200%28v=ws.10%29.aspx ? – Aziz Kabyshev Mar 21 '16 at 14:41
  • @AzizKabyshev I don't have any of the three keys in that location, for either the servers that I have the issue on nor for the servers that seem to be behaving as intended. – Tony Mitera Mar 21 '16 at 15:06
  • Ok, so defaults are set. On Win8+ `Get-SmbClientConfiguration` should report everything SMB-related. `Set-SmbClientConfiguration -FileInfoCacheLifetime 0` will set cache lifetime to zero, replace `0` to `10` to restore default. Mind to try it on test machine to see if it does force `robocopy` and other client-side processes to always get latest data from network share? – Aziz Kabyshev Mar 21 '16 at 15:19
  • @AzizKabyshev Unfortunately, all of the servers in question are running Server 2008 R2, so the Get-SmbClientConfiguration cmdlet is unavailable. – Tony Mitera Mar 21 '16 at 17:43
  • You can always add the `dword` key `FileInfoCacheLifetime` that's set to 0 to `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters` manually – Aziz Kabyshev Mar 21 '16 at 18:57
  • hi there. Did you decide to leave it as it is? – Aziz Kabyshev Apr 02 '16 at 19:59

0 Answers0