弄清楚了!
解决方案演练
- 首先,创建一个计划任务并将其指向一个 .bat 文件(我发现直接指向一个 .js 文件是行不通的)。
- 在批处理文件中指向您的 .js 文件以运行它(例如,我的位于“D:\BF_Ping.js”
BF_Ping.bat:
D:
cd\
BF_Ping.js
- 接下来,创建您的 JavaScript 文件。我的完整评论文件如下供参考:
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;
}
}
我实际上对此有点自豪——它第一次运行没有错误:)
一些注意事项:
- 时间以 24 小时格式给出。
- 将计划任务设置为每 5-10 分钟运行一次,以确保经常执行检查。
- 我在这里使用的服务是 BizFlow 服务,一个客户正在使用的产品。当然,您可以相应地更改函数的名称。
- 为服务名称运行“net start”或“net stop”时,请注意引号的转义字符。
- 使用在 services.msc 中显示的完整服务名称。
如果您对我如何改进代码有任何问题或意见,请告诉我!
谢谢,
——肖恩