Link to home
Start Free TrialLog in
Avatar of Edward Crist
Edward CristFlag for United States of America

asked on

Script to write to a log file

I want to use a GPO script to write the computer name to a logfile.txt file in a shared folder

Here's my script

@echo off

pushd \\cityhi-host1\studentsoftware

FINDSTR "^%computername%" < logfile.txt >NUL || runscriptonce.bat

popd


I cannot get it to work....any thoughts on my mistake(s) here??

Thanks!!!
Avatar of ste5an
ste5an
Flag of Germany image

hmm, what about

ECHO %computername% >> yourLogFile.txt

?
Avatar of oBdA
oBdA

Well, you're not writing anything at all, you're just checking.
Then you're starting a batch file without using "call", so this script will never continue after calling runscriptonce.bat.
Will that be a user logon script or a computer startup script?
Does the account running the script (AD user or AD computer, respectively) have change permissions for the share(!) and in NTFS?
Aside form the shared log, this script will create a log file in the account's temporary folder (%Temp% for a user, C:\Windows\Temp for the computer).
@echo off
setlocal
set ScriptLog=%Temp%\%~n0.log
set Share=\\cityhi-host1\studentsoftware
set RunLog=logfile.txt

>"%ScriptLog%" echo [%Date%][%Time%] Script started.
pushd "%Share%"
if errorlevel 1 (
	>>"%ScriptLog%" echo [%Date%][%Time%] Could not access '%Share%'.
	exit /b 1
)

findstr.exe "^%computername%$" "%RunLog%" >NUL
if errorlevel 1 (
	>>"%ScriptLog%" echo [%Date%][%Time%] '%ComputerName%' not found in '%RunLog%', starting runscriptonce.bat.
	call runscriptonce.bat
	>>"%ScriptLog%" echo [%Date%][%Time%] runscriptonce.bat ended, updating log.
	>>"%RunLog%" echo %ComputerName%
) else (
	>>"%ScriptLog%" echo [%Date%][%Time%] '%ComputerName%' found in '%RunLog%', leaving.
)
popd

Open in new window

Avatar of Edward Crist

ASKER

Thanks a bunch.....as a neophyte scripter, my plan is this....

I need to delete certain files and folders in a single location on each laptop, using a STARTUP script through GPO

But...I only want to run the script once...so I want the 1st script (namecheck.bat) to look for the laptop computer name in the logfile.txt file....if it finds it, don't run the 2nd script...If it doesn't find the computer name, it runs the 2nd script (runscriptonce.bat) which will write to the logfile.txt and delete the items i need to delete

Again, I'm a beginner at this and appreciate your assistance.
The script above should work. Remove the part about writing to the log file in runscriptonce.bat, the script above will take care of it, and it's easier to understand if only one script reads and writes this log file.
The AD group "Domain Computers" (or any other group containing the AD computer objects running the script) needs Change permissions for the Share and NTFS location where the log file is stored.
Ok, running namecheck.bat as a STARTUP SCRIPT from NETLOGON  using your code above

runscriptonce.bat is in \\cityhi-host1\studentsoftware

Nothing written to logfile.txt

Thoughts?
As I said: the namecheck.bat as posted above should write a log file "C:\Windows\Temp\namecheck.log" (its name will be the script's name, and the extension '.log'). Check what's in there.
If this file doesn't exist, then the startup script didn't start at all, and you need to troubleshoot your GPOs.
Yes, the file is in C:Windows\Temp

Each restart updates that file and says the computer name is not listed in logfile.txt, though.

Should the 1st script write the computer name to logfile.txt??

Thanks!
namecheck.bat should be writing to the name check file, but it can only do that if runscriptonce.bat returns gracefully.
So in the local log, you see a line
[<Date>][<Time>] 'ComputerName' not found in '<RunLog>', starting runscriptonce.bat.
Is there anything following after that?
If not, you need to troubleshoot runscriptonce.bat. If you have any "Exit" there, replace that with "Exit /b".
Can you post that script (inside [code] tags, please, see the toolbar above the input field), or does it contain sensitive information?
@echo off

net stop spooler
for /D %%I in ("C:\windows\system32\spool\drivers\*") do rmdir /s/q "%%I"
del /q C:\windows\system32\spool\drivers\*
net start spooler

Open in new window



The namecheck.txt file ends with

runscriptonce.bat ended, updating log.
It looks like Line #19 of the first script isn't writing to logfile.txt

"%RunLog%" echo %ComputerName%
I suspect that it's running just fine, but that the computer account is not allowed to write to the share with the log file.
This will now test write access. If it says it doesn't have write access, then again: The AD group "Domain Computers" (or any other group containing the AD computer objects running the script) needs Change permissions for the Share and NTFS location where the log file is stored.
@echo off
setlocal
set ScriptLog=%Temp%\%~n0.log
set Share=\\cityhi-host1\studentsoftware
set RunLog=logfile.txt

>"%ScriptLog%" echo [%Date%][%Time%] Script started.
pushd "%Share%"
if errorlevel 1 (
	>>"%ScriptLog%" echo [%Date%][%Time%] ERROR: Could not access '%Share%', leaving.
	exit /b 1
)
set TmpFile=~tmp_%ComputerName%_%Random%.tmp
set WritePermission=
>"%TmpFile%" echo .&&set WritePermission=True
if defined WritePermission (
	del "%TmpFile%"
) else (
	>>"%ScriptLog%" echo [%Date%][%Time%] ERROR: No write access to '%Share%', leaving.
	exit /b 1
)

findstr.exe "^%computername%$" "%RunLog%" >NUL
if errorlevel 1 (
	>>"%ScriptLog%" echo [%Date%][%Time%] '%ComputerName%' not found in '%RunLog%', starting runscriptonce.bat.
	call runscriptonce.bat
	>>"%ScriptLog%" echo [%Date%][%Time%] runscriptonce.bat ended.
	>>"%RunLog%" echo %ComputerName%
) else (
	>>"%ScriptLog%" echo [%Date%][%Time%] '%ComputerName%' found in '%RunLog%', leaving.
)
>>"%ScriptLog%" echo [%Date%][%Time%] Script ended.
popd

Open in new window

Ran the updated script....here's the contents of namecheck.txt

[Tue 01/31/2017][11:50:20.35] Script started.
[Tue 01/31/2017][11:50:26.87] 'HS1YG12-18' not found in 'logfile.txt', starting runscriptonce.bat.
[Tue 01/31/2017][11:50:26.87] runscriptonce.bat ended.
[Tue 01/31/2017][11:50:31.59] Script ended.
Does the logfile.txt exist in \\cityhi-host1\studentsoftware, and if so, does it contain the 'HS1YG12-18' name?
Yes it is....and after each process, it opens up empty.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh...now the last line in namecheck is

ERROR: could not update 'logfile.txt' , leaving

I'm attaching the security settings on the GPO I'm using
runonce.png
Got it!

I added "Domain Computers' to the ACL of the text file itself and the computername is now written into logfile.txt

I'll do more testing, but that looks to have solved the problem.

Thanks a bunch!
Worked with me through it in detail until I got it!  Thanks!