In my previous post, I discussed using Logman to automatically start Perfmon counters logging. The solution involved running logman.exe to start the collection, and if the counters were already running you’d get an error message saying words to that effect.
The problem with this solution is SQL Agent will report that the job failed as the error code returned by Logman is non-zero. You can tell SQL Agent a specific code to use as the “success” code, but the user interface will not allow big negative numbers, such as -2144337750 (which is what Perfmon returns if the counter is already running on my Vista machine. Windows 2003 returns a different code). While you may be able to enter this value via script, I’m not sure how Management Studio will handle it, and could cause problems down the line if you edit the job.
Instead, a solution is to use a batch file that can run logman, check the error code returned, and if the error code is “Already running”, it can then exit the batch file specifying an error code that SQL Agent can handle (such as 0):
@echo off C:\Windows\System32\logman.exe start TestCollection -s SERVERNAME IF %errorlevel%==-2144337750 ( echo Collection already started! exit /B 0 ) ELSE ( echo Exiting with error level: %errorlevel% )
That’s it! Now the job will report as succeeded if the collection is already running.