Figured it out!
Solution Walkthrough
- Firstly, create a scheduled task and point it to a .bat file (pointing it directly to a .js will not work, I discovered).
- In the batch file point to your .js file to run it (for example, mine was located in "D:\BF_Ping.js"
BF_Ping.bat:
D:
cd\
BF_Ping.js
- Next, create your JavaScript file. My fully commented file is below for reference:
BF_Ping.js:
//*** Time Variables ***//
var UptimeBeginsAtHour = 6; // Hour at which the services should be started
var UptimeEndsAtHour = 22; // Hour at which the services should be ended
//*** Flags ***//
var KILL_FLAG = FindFile("C:\\BF_KILL.txt"); // If this flag is true, services will be shut down and not started.
var LAZY_FLAG = FindFile("C:\\BF_LAZY.txt"); // If this flag is true, nothing in the script will run.
if (KILL_FLAG == true)
{
StopBizFlowServices();
}
if (KILL_FLAG == false && LAZY_FLAG == false)
{
DetectTimeAndProcess(UptimeBeginsAtHour, UptimeEndsAtHour);
}
/***
* DetectTimeAndProcess(startAtHour, stopAtHour):
* Starts or stops BizFlow Services based on uptime variables.
*
* ---Arguments---
* startAtHour Hour after which services should be started (defined by variable in main code)
* stopAtHour Hour after which services should be started (defined by variable in main code)
*
* ---Returns---
* None (void)
*
* ---Called By---
* Main code.
*/
function DetectTimeAndProcess(startAtHour, stopAtHour)
{
var currentTime = new Date();
var hour = currentTime.getHours(); // Returns Hour in 24-hour format
if (hour > startAtHour && hour < stopAtHour)
{
StartBizFlowServices();
}
else
{
StopBizFlowServices();
}
}
/***
* StartBizFlowServices():
* Starts BizFlow Services using "net start" and the service name.
*
* --- Arguments ---
* None.
*
* --- Returns ---
* None (void)
*
* --- Called By---
* DetectTimeAndProcess()
*/
function StartBizFlowServices()
{
var objShell = WScript.CreateObject("WScript.Shell");
objShell.Run("net start \"BizFlow Database Service\" ", 1, true);
objShell.Run("net start \"BizFlow Main Service\"", 1, true);
objShell.Run("net start \"BizFlow OLE-DB Service\"", 1, true);
objShell.Run("net start \"BizFlow PKI Service\"", 1, true);
objShell.Run("net start \"BizFlow Queue Service\"", 1, true);
objShell.Run("net start \"BizFlow Scheduler Service\"", 1, true);
}
/***
* StopBizFlowServices():
* Stops BizFlow Services using "net stop" and the service name.
*
* --- Arguments ---
* None.
*
* --- Returns ---
* None (void)
*
* --- Called By---
* DetectTimeAndProcess()
*/
function StopBizFlowServices()
{
var objShell = WScript.CreateObject("WScript.Shell");
objShell.Run("net stop \"BizFlow OLE-DB Service\"", 1, true);
objShell.Run("net stop \"BizFlow PKI Service\"", 1, true);
objShell.Run("net stop \"BizFlow Queue Service\"", 1, true);
objShell.Run("net stop \"BizFlow Scheduler Service\"", 1, true);
objShell.Run("net stop \"BizFlow Main Service\"", 1, true);
objShell.Run("net stop \"BizFlow Database Service\"", 1, true);
}
/***
*
* FindFile (filePath):
* Searches for the existence of a given file path.
*
* --- Arguments ---
* filePath Full Path of file (including file name)
*
* --- Returns ---
* true if file is found
* false if file is not found
*
* --- Called By---
* Main Code (while setting flags)
*/
function FindFile(filePath)
{
var fso; //For FileSystemObject
fso = new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(filePath))
{
return true;
}
else
{
return false;
}
}
I'm actually a little proud of this one -- it ran without error the first time :)
Some Notes:
- Times are given in 24 hour format.
- Set the scheduled task to run every 5-10 minutes to ensure that the check is performed often.
- The services I use here are BizFlow services, a product that a client is using. You can change the names of the functions accordingly, of course.
- Note the escape characters for the quotes when running "net start" or "net stop" for the service name.
- Use the full service name, as it appears in services.msc.
Please let me know if you have any questions or comments on how I might improve the code!
Thanks,
--
Sean