ReneGe
asked on
Batch File: Network PC Time Audit
Hi there,
I need to audit the time on the network PCs and servers.
Thanks for fixing my batch file,
Rene
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
~bp
ASKER
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)
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)
@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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Holy smokes!! It works :)
Thanks a million + 1
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
ASKER
Thanks Bill!!
Also, gave some thanks points to knight for his contribution!!
Also, gave some thanks points to knight for his contribution!!
ASKER
Bill, as a more personal note, I did an introspective of what recently happened I see where I did wrong. Plz accept my appolegies.
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
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
ASKER
:)
ASKER
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
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
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
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
ASKER
Your last command line does word!!!
You rock!!
Thanks Bill :)
You rock!!
Thanks Bill :)
Cool!
~bp
~bp
ASKER
FYI: Related question
https://www.experts-exchange.com/questions/27432642/Batch-File-Run-at-the-same-time.html
https://www.experts-exchange.com/questions/27432642/Batch-File-Run-at-the-same-time.html
ASKER
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