Edward Crist
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\studentsoft ware
FINDSTR "^%computername%" < logfile.txt >NUL || runscriptonce.bat
popd
I cannot get it to work....any thoughts on my mistake(s) here??
Thanks!!!
Here's my script
@echo off
pushd \\cityhi-host1\studentsoft
FINDSTR "^%computername%" < logfile.txt >NUL || runscriptonce.bat
popd
I cannot get it to work....any thoughts on my mistake(s) here??
Thanks!!!
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).
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
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.
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.
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.
ASKER
Ok, running namecheck.bat as a STARTUP SCRIPT from NETLOGON using your code above
runscriptonce.bat is in \\cityhi-host1\studentsoft ware
Nothing written to logfile.txt
Thoughts?
runscriptonce.bat is in \\cityhi-host1\studentsoft
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.
If this file doesn't exist, then the startup script didn't start at all, and you need to troubleshoot your GPOs.
ASKER
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!
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?
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?
ASKER
@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
The namecheck.txt file ends with
runscriptonce.bat ended, updating log.
ASKER
It looks like Line #19 of the first script isn't writing to logfile.txt
"%RunLog%" echo %ComputerName%
"%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.
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
ASKER
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.
[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\studentsoft ware, and if so, does it contain the 'HS1YG12-18' name?
ASKER
Yes it is....and after each process, it opens up empty.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
ERROR: could not update 'logfile.txt' , leaving
I'm attaching the security settings on the GPO I'm using
runonce.png
ASKER
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!
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!
ASKER
Worked with me through it in detail until I got it! Thanks!
ECHO %computername% >> yourLogFile.txt
?