Powershell script to copy portion of log file from remote server

Hey Experts.  I need to copy a portion of the backup log (dsmsched.log) from a server to a log file on a network share via Powershell.  The backup job will run the night of day 1 and I will run this script in the morning of day 2.  I need the script to go to the server, look for the line in the text file (dsmsched.log from day 1) that says SCHEDULEREC STATUS BEGIN, copy that line and everything up to and including SCHEDULEREC STATUS END to a new log file (can be named anything.log).  

Here is the batch script I used if this helps:
@echo off
setlocal enabledelayedexpansion
set CentralLog=Z:\Logs\TSM_Backups\Combined_TSMLogs.log
if "%~1"=="" (
	set FindDate=
	set /p FindDate=Enter date to search: 
) else (
	set FindDate=%~1
)
if "%FindDate%"=="" goto :eof
set CopyBegin=SCHEDULEREC STATUS BEGIN
set CopyEnd=SCHEDULEREC STATUS END
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	set LogFile=%%a
	if "!LogFile:~0,2!"=="\\" (
		for /f "delims=\" %%s in ("!LogFile!") do set Server=%%s
	) else (
		set Server=%ComputerName%
	)
	echo Processing '!LogFile!' on !Server! ...
	set /a CopyFound = 0
	set /a Copy = 0
	for /f "delims=" %%b in ('type "!LogFile!" ^| findstr.exe "^%FindDate%"') do (
		set Line=%%b
		if !Copy!==0 (
			if not "!Line!"=="!Line:%CopyBegin%=!" (
				set /a CopyFound = 1
				set /a Copy = 1
				>>"%CentralLog%" echo ========== !Server! Start ============================================================
				>>"%CentralLog%" echo !Line!
			)
		) else (
			>>"%CentralLog%" echo !Line!
			if not "!Line!"=="!Line:%CopyEnd%=!" (
				set /a Copy = 0
				>>"%CentralLog%" echo ========== !Server! End ============================================================
			)
		)
	)
	if !CopyFound!==0 (
		echo No match found in '!LogFile!'.
	)
)
goto :eof

[DATA]
\\server\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window

LVL 9
samiam41Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
I can work on converting it between other things today.

However, if you have a working CMD script, why bother converting it?

Ben
samiam41Author Commented:
Thanks for the reply.  The batch script requires a manual input and I'm trying to evolve away from that.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
what do you mean?  Because you need yesterday?

We could just change this to always run for yesterday when no argument is supplied.

Just need to know the date format needed for the log file parse.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

samiam41Author Commented:
what do you mean?  Because you need yesterday?
The script prompts for a date.  The date that is entered is the one that is then filtered in the log file.  Backups run Mon-Sat night.

We could just fit it to always run yesterday when no argument is supplied.

Just need to know the date format needed from the log file.
dd/mm/yyyy > 01/02/2018
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
The script prompts for a date.  The date that is entered is the one that is then filtered in the log file.  Backups run Mon-Sat night.

The script only prompts if no date is supplied as an argument, as written.

Let me assume the script has the name "LastBackup.cmd"  and a full path "C:\Script\LastBakup.cmd"

If you wanted to scan for the backup from "01/02/2018" you could enter this at the command line:

"C:\Script\LastBakup.cmd" "01/02/2018"

Open in new window


If the script does not see an argument provided when it is run, then it prompts you for the date.

We could just change that step to find yesterday's date automatically instead, and be done if you prefer.
samiam41Author Commented:
@Ben, I'm good if you want to supply that option for the batch script.  Thank you sir.

I'm going to post a follow-up question for the powershell script as I have a portion of the code done but need help with it.  This way I have a fall back plan (batch script) if the powershell script takes too long to come up with.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Well here you are this should give you exactly what you need to get the date automatically in the existing script.

I tested everything other than the actual original script portion which I didn't change to make sure the date is generated automatically.

REM Script: TivoliLogYesterday.cmd
@(
	SETLOCAL
	echo off
	CALL :GetDate
	SET "CentralLog=Z:\Logs\TSM_Backups\Combined_TSMLogs.log"
	SET "FindDate=%~1"
	SET "CopyBegin=SCHEDULEREC STATUS BEGIN"
	SET "CopyEnd=SCHEDULEREC STATUS END"
	SET "_eLvl=0"
)

CALL :Main

(
	ENDLOCAL
	EXIT /b %_eLvl%
)
:Main
	setlocal enabledelayedexpansion
	IF NOT DEFINED FindDate (
		ECHO Finding Date Automatically
		CALL :GetYesterday
	)
	IF NOT DEFINED FindDate (
		SET "_eLvl=1"
		GOTO :EOF
	)
	ECHO Running Backup Log check for %FINDDate%
	for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
	for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
		set LogFile=%%a
		if "!LogFile:~0,2!"=="\\" (
			for /f "delims=\" %%s in ("!LogFile!") do set Server=%%s
		) else (
			set Server=%ComputerName%
		)
		echo Processing '!LogFile!' on !Server! ...
		set /a CopyFound = 0
		set /a Copy = 0
		for /f "delims=" %%b in ('type "!LogFile!" ^| findstr.exe "^%FindDate%"') do (
			set Line=%%b
			if !Copy!==0 (
				if not "!Line!"=="!Line:%CopyBegin%=!" (
					set /a CopyFound = 1
					set /a Copy = 1
					>>"%CentralLog%" echo ========== !Server! Start ============================================================
					>>"%CentralLog%" echo !Line!
				)
			) else (
				>>"%CentralLog%" echo !Line!
				if not "!Line!"=="!Line:%CopyEnd%=!" (
					set /a Copy = 0
					>>"%CentralLog%" echo ========== !Server! End ============================================================
				)
			)
		)
		if !CopyFound!==0 (
			echo No match found in '!LogFile!'.
		)
	)
GOTO :eof

:GetYesterday
	SETLOCAL enableextensions disabledelayedexpansion
	IF /I "%#dd%" NEQ "1" (
		SET /a "#dd-=1"
	) ELSE (
		IF /I "%#mm%" NEQ "1" (
			SET "#mm-=1"
		) ELSE (
			SET "#mm=12"
			SET /a "#yy-=1"
		)
		IF /I "!#mm!" NEQ "2" (
			SET /a "#dd=30+((!#mm!+!#mm!/8) %% 2)"
		) ELSE (
			SET /a "#dd=28+!(!#yy!%%4)-!(!#yy!%%100)+!(!#yy!%%400)"
		)
	)
	SET "dd=0%#dd%"
	SET "dd=%dd:~-2%"
	SET "mm=0%#mm%"
	SET "dd=%dd:~-2%"
(
	ENDLOCAL
	SET "FindDate=%mm%/%dd%/%yy%"
	EXIT /b 0
)

:GetDate
	SETLOCAL
	FOR /F "Tokens=1-3 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE%") DO (
		FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
			ENDLOCAL
			SET "%%~a=%%~A"
			SET "%%~b=%%~B"
			SET "%%~c=%%~C"
			SET /a "#%%~a=1%%~A - (2%%~A-1%%~A)"
			SET /a "#%%~b=1%%~B - (2%%~B-1%%~B)"
			SET /a "#%%~c=1%%~C - (2%%~C-1%%~C)"
		)
	)
	SET "TodayDate=%mm%/%dd%/%yy%"
GOTO :EOF

[DATA]
\\server\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samiam41Author Commented:
Great work!!  Thanks again.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Hey Sam, Glad to help,  Please use the following code instead, the original code I posted was not the last one I tested. (Ooops).  It would work most days so it wouldn't cause any problems yet, but this code will correct the cases where the old code didn't give the correct date.

Thanks.

REM Script: TivoliLogYesterday.cmd
@(
	SETLOCAL
	echo off
	CALL :GetDate
	SET "CentralLog=Z:\Logs\TSM_Backups\Combined_TSMLogs.log"
	SET "FindDate=%~1"
	SET "CopyBegin=SCHEDULEREC STATUS BEGIN"
	SET "CopyEnd=SCHEDULEREC STATUS END"
	SET "_eLvl=0"
)

CALL :Main

(
	ENDLOCAL
	EXIT /b %_eLvl%
)
:Main
	setlocal enabledelayedexpansion
	IF NOT DEFINED FindDate (
		ECHO Finding Date Automatically
		CALL :GetYesterday
	)
	IF NOT DEFINED FindDate (
		SET "_eLvl=1"
		GOTO :EOF
	)
	ECHO Running Backup Log check for %FINDDate%
	for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
	for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
		set LogFile=%%a
		if "!LogFile:~0,2!"=="\\" (
			for /f "delims=\" %%s in ("!LogFile!") do set Server=%%s
		) else (
			set Server=%ComputerName%
		)
		echo Processing '!LogFile!' on !Server! ...
		set /a CopyFound = 0
		set /a Copy = 0
		for /f "delims=" %%b in ('type "!LogFile!" ^| findstr.exe "^%FindDate%"') do (
			set Line=%%b
			if !Copy!==0 (
				if not "!Line!"=="!Line:%CopyBegin%=!" (
					set /a CopyFound = 1
					set /a Copy = 1
					>>"%CentralLog%" echo ========== !Server! Start ============================================================
					>>"%CentralLog%" echo !Line!
				)
			) else (
				>>"%CentralLog%" echo !Line!
				if not "!Line!"=="!Line:%CopyEnd%=!" (
					set /a Copy = 0
					>>"%CentralLog%" echo ========== !Server! End ============================================================
				)
			)
		)
		if !CopyFound!==0 (
			echo No match found in '!LogFile!'.
		)
	)
GOTO :eof

:GetYesterdayDate
	setlocal disabledelayedexpansion
	SET "Done=No"
	IF /I "%#dd%" NEQ "1" (
		SET /A "#dd-=1"
		SET "Done=YES"
	) ELSE (
		IF /I "%#mm%" NEQ "1" (
			SET /A "#mm-=1"
		) ELSE (
			SET "#mm=12"
			SET /A "#yy-=1"
		)
	)
	IF /I "%Done%" NEQ "YES" (
		IF /I "%#mm%" NEQ "2" (
				SET /A "#dd=30+((%#mm%+%#mm%/8) %% 2)"
			) ELSE (
				SET /A "#dd=28+!(%#yy%%%4)-!(%#yy%%%100)+!(%#yy%%%400)"
			)
	)
	SET "dd=0%#dd%"
	SET "dd=%dd:~-2%"
	SET "mm=0%#mm%"
	SET "mm=%mm:~-2%"
	SET "yy=%#yy%"
(
	ENDLOCAL
	SET "YesterdayDate=%mm%/%dd%/%yy%"
	EXIT /b 0
)

:GetDate
	SETLOCAL
	FOR /F "Tokens=1-3 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE%") DO (
		FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
			ENDLOCAL
			SET "%%~a=%%~A"
			SET "%%~b=%%~B"
			SET "%%~c=%%~C"
			SET /a "#%%~a=1%%~A - (2%%~A-1%%~A)"
			SET /a "#%%~b=1%%~B - (2%%~B-1%%~B)"
			SET /a "#%%~c=1%%~C - (2%%~C-1%%~C)"
		)
	)
	SET "TodayDate=%mm%/%dd%/%yy%"
GOTO :EOF

[DATA]
\\server\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window

samiam41Author Commented:
Thank you for coming back to this and fixing it.  I have it saved and ready for testing but haven't hit that stage yet.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Hey SamIAm41,

  No problem, I'm here to help so i want to make sure you get what you need not just close a question.

Have a good evening :)
samiam41Author Commented:
Good morning Ben.  Just ran the script from the command line and the message I'm getting back says "system cannot find the batch label specified - getyesterday".  Looking through the script, I edited line 23 to match line 64 and reran the script.  I now get "Finding Date Automatically" but that's it and the command prompt returns.

combine_bat.png
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
That's what I get for having done the date part in a separate batch.

 I changed the variable name from the one needed in the Tivoli script, even though in the Tivoli script I check for it so that it does not continue if not found.

 I tested the script as a whole to realize it was the misnamed variable by putting an exit (GOTO EOF) into the script and running it (As below) which works.  that is REMED out in this code so it will continue on and do the backup.


REM Script: TivoliLogYesterday.cmd
@(
	SETLOCAL
	echo off
	CALL :GetDate
	SET "CentralLog=Z:\Logs\TSM_Backups\Combined_TSMLogs.log"
	SET "FindDate=%~1"
	SET "CopyBegin=SCHEDULEREC STATUS BEGIN"
	SET "CopyEnd=SCHEDULEREC STATUS END"
	SET "_eLvl=0"
)

CALL :Main

(
	ENDLOCAL
	EXIT /b %_eLvl%
)
:Main
	setlocal enabledelayedexpansion
	IF NOT DEFINED FindDate (
		ECHO Finding Date Automatically
		CALL :GetYesterdayDate
	)
	echo. Finished Retrieving Date "%FindDate%"
	IF NOT DEFINED FindDate (
		SET "_eLvl=1"
		GOTO :EOF
	)
	ECHO Running Backup Log check for %FINDDate%
	REM GOTO :EOF
	for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
	for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
		set LogFile=%%a
		if "!LogFile:~0,2!"=="\\" (
			for /f "delims=\" %%s in ("!LogFile!") do set Server=%%s
		) else (
			set Server=%ComputerName%
		)
		echo Processing '!LogFile!' on !Server! ...
		set /a CopyFound = 0
		set /a Copy = 0
		for /f "delims=" %%b in ('type "!LogFile!" ^| findstr.exe "^%FindDate%"') do (
			set Line=%%b
			if !Copy!==0 (
				if not "!Line!"=="!Line:%CopyBegin%=!" (
					set /a CopyFound = 1
					set /a Copy = 1
					>>"%CentralLog%" echo ========== !Server! Start ============================================================
					>>"%CentralLog%" echo !Line!
				)
			) else (
				>>"%CentralLog%" echo !Line!
				if not "!Line!"=="!Line:%CopyEnd%=!" (
					set /a Copy = 0
					>>"%CentralLog%" echo ========== !Server! End ============================================================
				)
			)
		)
		if !CopyFound!==0 (
			echo No match found in '!LogFile!'.
		)
	)
GOTO :eof

:GetYesterdayDate
	setlocal disabledelayedexpansion
	SET "Done=No"
	IF /I "%#dd%" NEQ "1" (
		SET /A "#dd-=1"
		SET "Done=YES"
	) ELSE (
		IF /I "%#mm%" NEQ "1" (
			SET /A "#mm-=1"
		) ELSE (
			SET "#mm=12"
			SET /A "#yy-=1"
		)
	)
	IF /I "%Done%" NEQ "YES" (
		IF /I "%#mm%" NEQ "2" (
				SET /A "#dd=30+((%#mm%+%#mm%/8) %% 2)"
			) ELSE (
				SET /A "#dd=28+!(%#yy%%%4)-!(%#yy%%%100)+!(%#yy%%%400)"
			)
	)
	SET "dd=0%#dd%"
	SET "dd=%dd:~-2%"
	SET "mm=0%#mm%"
	SET "mm=%mm:~-2%"
	SET "yy=%#yy%"
(
	ENDLOCAL
	SET "FindDate=%mm%/%dd%/%yy%"
	EXIT /b 0
)

:GetDate
	SETLOCAL
	FOR /F "Tokens=1-3 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE%") DO (
		FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
			ENDLOCAL
			SET "%%~a=%%~A"
			SET "%%~b=%%~B"
			SET "%%~c=%%~C"
			SET /a "#%%~a=1%%~A - (2%%~A-1%%~A)"
			SET /a "#%%~b=1%%~B - (2%%~B-1%%~B)"
			SET /a "#%%~c=1%%~C - (2%%~C-1%%~C)"
		)
	)
	SET "TodayDate=%mm%/%dd%/%yy%"
GOTO :EOF

[DATA]
\\server\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window


2018-01-09-13_13_27-Administrator_-C.png
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.