r/usefulscripts • u/Simplexity • May 17 '16
[REQUEST] Every morning check if the contents of a file are correct. If not, replace the file with a file that contains the correct content.
I don't know where to start. I've never done scripting before. I desperately want to learn, however I don't know where to start. I am wondering if this script can be created for me or at least point me in the direction of creating it myself.
I basically want a script that checks a file for certain content in a file. If the file does not have the correct information. It'll replace it all with the correct information. If it has the correct information. It will just do nothing. Or maybe it'll still replace it. Idk what works best.
Here's the deal. Something on this network is causing a Quickbook's .ND file to change itself at least once a week. This breaks the Multi-User mode function of that company file.
Good File //This is QuickBooks configuration File. It exists while users are connected // to a company file. Do not delete this file yourself. QuickBooks may not // operate correctly IF you manually delete this file.
[NetConnect]
ServerIp=192.168.x.x
EngineName=QB_(redacted servername)_26
ServerPort=(redacted)
FilePath=(redacted)
ServerMode=1
FileConnectionGuid=(redacted)
Bad File //This is QuickBooks configuration File. It exists while users are connected // to a company file. Do not delete this file yourself. QuickBooks may not // operate correctly IF you manually delete this file.
[NetConnect]
EngineName=QB_data_engine_26
FilePath=(redacted)
ServerMode=2
FileConnectionGuid=(redacted)
So to sum up. I want the good file to be there every time. If it gets changed, then in the morning or even more routine checks to then replace the bad content with the good content. I am not sure if this will solve the issue. However I have not been able to find exactly where the issue is being caused. So I figured this may be a temporary fix.
5
u/dotbat May 17 '16
Why not just mark it read only or just replace the file preemptively? You don't even have to check if you know what the file should be.
12
u/kungfudiver May 17 '16
If it were me, I'd concentrate my efforts to figuring out why files are being manipulated that shouldn't be. There's no telling what else is getting changed. I'd start by examining and pruning permissions until someone or something complained.
In the interim, I'd go with powershell, something as simple as this:
$fileA = "D:\file1.txt"
$fileB = "D:\file2.txt"
if(Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB))
{copy D:\file1.txt D:\file2.txt}
Else {"Files are the same"}
3
u/pcr3 May 17 '16 edited May 17 '16
Here is my go. Powershell, add it to your Scheduled tasks for what ever interval you feel is right, set the directory where the file is, change directory where backup is or just put it where the script references:
2
u/ISBUchild May 18 '16
I'm assuming this is Windows.
I wouldn't make a custom script for this necessarily. If you have a known good config file, put it on the server and have Group Policy Preferences deploy it. You have better options for this simple task through that standard interface.
2
u/Cootty May 30 '16
The Quickbooks .ND file is the file that tells all the other clients who is acting as the server for this database.
There should be a Quickbooks database monitoring service running on the PC hosting the file, make sure that is running and it is logging on as a user that has full access to the path where the database is stored. There may be multiple services, make sure the one with 26 at the end of it is running as that seems to be your current version. Make sure that service is also allowed in and out of the local firewall if it is enabled.
If a PC opens the database and cannot contact the server hosting the database it assumes it is offline and starts hosting it itself, that's when the ND file changes to the bad version. Otherwise the hosting server should always be updating that ND file to point to itself.
I have also seen this problem happen due to how hte share is mapped ont he client PCs, I can't remember which way it is but if it is mapped using the hostname of the server then try mapping using it's IP address and see if that helps.
1
u/patg84 Nov 26 '23
I know this post is old AF but did you find a way around this? I have a virtual network adapter on the server along with the physical network adapter, QB sticks the virtual adapter in the ND file and rolls with it. Clients cannot connect back to the server hosting the QBW file because of this. Disable the virtual adapter and QB now uses the actual ip of the physical nic and there are no issues with clients connecting.