We help IT Professionals succeed at work.

Batch File: Network PC Time Audit

Medium Priority
448 Views
Last Modified: 2012-08-13
Hi there,

I need to audit the time on the network PCs and servers.

Thanks for fixing my batch file,
Rene

@ECHO OFF
FOR /F %%A IN ('NET VIEW ^| FINDSTR \\') DO PSEXEC %%A TIME /T >>"%~dpn0.log"
EXIT

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
What's wrong with it now?  How is this not meeting your need?  Is it not printing seconds?  Not recording in the log?


FOR /F %%A IN ('NET VIEW ^| FINDSTR \\') DO PSEXEC %%A @echo ^%computername^%: ^%time^% >>"\\YourLocalHostNameOrIP\c$\%~dpn0.log"

Author

Commented:
Im working lots of hours and currently lacking some ability of generating creative ideas to resolve issues.

It is not outputting anything!! I also tried psexec %%A ECHO %time%>>"%~dpn0.log" = Still Nothing

Usually, I can > to a local drive as is.

You suggestion is a good idea. Will try it a bit later.

Thanks
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
The log file is okay just being on the local machine the way it was specified.  I suspect PSEXEC is unable to run on the remote, and displaying error messages to STDERR.  Adjust as below and see what you get.

@ECHO OFF
FOR /F %%A IN ('NET VIEW ^| FINDSTR \\') DO (
  ECHO Checking: %%A
  PSEXEC %%A TIME /T >>"%~dpn0.log" 2>>&1
)
EXIT

Open in new window

~bp

Author

Commented:
Here I am so far.

I tried all the proposed solutions and tried to make sense of what is going on.

As Bill was suspecting, psexec was not running on the remote PC. However if I run "psexec \\remotepc cmd", then the desired command, it works.

For example:
>PSEXEC \\RemotePC CMD
>time /t
>10:02

>PSEXEC \\RemotePC time /t
>The specified file not found (translated from french)

>PSEXEC \\RemotePC time ^/t
>The specified file not found (translated from french)

 
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
@ReneGe

TIME is an internal command, so you will actually need to run a command shell remotely to execute it.  So something like:

cmd.exe time /t

needs to be the command line that PSEXEC executes.

~bp
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Sorry, that should have been

cmd /c time /t

(not quite awake yet...)

~bp

Author

Commented:
Holy smokes!!  It works :)
Thanks a million + 1

@ECHO OFF
SET LogFile="%~dpn0.log"
IF EXIST "%LogFile%" DEL "%LogFile%"
FOR /F %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
  ECHO Checking: %%A
  PSEXEC %%A cmd /c echo %time% %%A>>"%LogFile%"
)
PAUSE
EXIT

Open in new window

Author

Commented:
Thanks Bill!!
Also, gave some thanks points to knight for his contribution!!

Author

Commented:
Bill, as a more personal note, I did an introspective of what recently happened I see where I did wrong. Plz accept my appolegies.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome Rene.

No need to apologize, I may have looked at it from a single perspective as well.  Either way, it's all good, I'm glad we can share ideas and feedback (positive or negative) and both take it in the spirit is in intended and remain partners in crime.

~bp

Author

Commented:
:)

Author

Commented:
FYI

I just realised that the time sent to the log file gives me the time of my PC, not the time of the remote PC.

So I decised to use WMIC, and the following script works.

Cheers
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET LogFile="%~dpn0.log"
IF EXIST "%LogFile%" DEL "%LogFile%"
FOR /F "tokens=1" %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
   SET Target=%%A
   SET Target=!Target:\\=!
   SET TheDate=---- -- --
   SET TheTime=-- -- --
   ECHO Checking: %%A
   PORTQRY -n !Target! -e 135 | FINDSTR /I /C:"not listening" >NUL || CALL :GetDateTime
   ECHO !TheDate! !TheTime! !Target!>>"%LogFile%"
)
PAUSE
EXIT

:GetDateTime
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC /node:"%Target%" Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF %%A GTR 0 (
   SET DayVal=%%A
   SET HourVal=%%B
   SET MinVal=%%C
   SET MonthVal=%%D
   SET SecVal=%%E
   SET YearVal=%%F
   )
)
REM CONVERT DATE TO JULIAN
   SET /a "YearJ=10000%YearVal% %%10000,MonthJ=100%MonthVal% %% 100,DayJ=100%DayVal% %% 100"
   SET /a DateJ=DayJ-32075+1461*(YearJ+4800+(MonthJ-14)/12)/4+367*(MonthJ-2-(MonthJ-14)/12*12)/12-3*((YearJ+4900+(MonthJ-14)/12)/100)/4
REM ADDING LEADING "0"
   IF %DayVal% LSS 10   (SET Day=0%DayVal%) 		ELSE (SET Day=%DayVal%)
   IF %HourVal% LSS 10  (SET Hour=0%HourVal%) 		ELSE (SET Hour=%HourVal%)
   IF %MinVal% LSS 10   (SET Min=0%MinVal%) 		ELSE (SET Min=%MinVal%)
   IF %MonthVal% LSS 10 (SET Month=0%MonthVal%) 	ELSE (SET Month=%MonthVal%)
   IF %SecVal% LSS 10   (SET Sec=0%SecVal%) 		ELSE (SET Sec=%SecVal%)
   SET Year=%YearVal%

   SET TheDate=%Year%-%Month%-%Day%
   SET TheTime=%Hour%.%Min%.%Sec%
EXIT /b

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
I'm cool with the use of WMIC, but the TIME command would be executing on the remote PC, so the time /t command results logged would be from the remote, not local.

Using %TIME% though is a problem (sorry I didn't notice you switched that) because it will replace that before it sends it to the remote PC, right in the local BAT file.  You could try doubling the % signs, that *might* work, as in:

PSEXEC %%A cmd /c echo %%time%% %%A>>"%LogFile%"

~bp

Author

Commented:
Your last command line does word!!!

You rock!!

Thanks Bill :)
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Cool!

~bp

Explore More ContentExplore courses, solutions, and other research materials related to this topic.