javascript 停止特定服务

发布于 2024-08-19 18:20:27 字数 548 浏览 8 评论 0原文

我在脚本中有以下代码。 问题是我想获取以特定名称启动并处于特定启动模式的脚本的信息。

var e = new Enumerator(GetObject("winmgmts:").InstancesOf("Win32_Service"))
var WSHShell = new ActiveXObject ("WScript.Shell");

var strPrefix = "TTTT";

for(;!e.atEnd(); e.moveNext()){
 var Service = e.item();
 var strName = Service.Name;

 if (strName.substr (0, strPrefix.length) == strPrefix) { 
  if(Service.StartMode == 'mmManual') {
   WScript.Echo("Yes");
  }
  if(e.StartMode == 'Manual') {
   WScript.Echo("Yes");
  }
 } 
}

在上面的脚本中,我试图了解启动模式,但它总是返回 true。

I have the following code in a script.
The problem is That I want to get information of scripts that starts in a specific name and are in a specific startmode.

var e = new Enumerator(GetObject("winmgmts:").InstancesOf("Win32_Service"))
var WSHShell = new ActiveXObject ("WScript.Shell");

var strPrefix = "TTTT";

for(;!e.atEnd(); e.moveNext()){
 var Service = e.item();
 var strName = Service.Name;

 if (strName.substr (0, strPrefix.length) == strPrefix) { 
  if(Service.StartMode == 'mmManual') {
   WScript.Echo("Yes");
  }
  if(e.StartMode == 'Manual') {
   WScript.Echo("Yes");
  }
 } 
}

In the above script I tried to know the start mode but it always return true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当梦初醒 2024-08-26 18:20:27

McDowell 是对的,但请注意,如果将前缀和启动模式检查作为 WMI 查询的一部分,则可以在循环中删除它们:

从 Win32_Service 中选择 *,其中名称类似于“TTTT%”且 StartMode =“手动”

使用此查询,您的脚本可能如下所示:

var strComputer = ".";
var oWMI = GetObject("winmgmts://" + strComputer + "/root/CIMV2");

var colServices = oWMI.ExecQuery("SELECT * FROM Win32_Service WHERE Name LIKE 'TTTT%' AND StartMode = 'Manual'");
var enumServices = new Enumerator(colServices);

for(; !enumServices.atEnd(); enumServices.moveNext())
{
  var oService = enumServices.item();
  WScript.Echo(oService.Name);
}

McDowell is right, but note that you can get rid of prefix and start mode checks in your loop if you make them part of the WMI query:

SELECT * FROM Win32_Service WHERE Name LIKE 'TTTT%' AND StartMode = 'Manual'

Using this query, your script could look like this:

var strComputer = ".";
var oWMI = GetObject("winmgmts://" + strComputer + "/root/CIMV2");

var colServices = oWMI.ExecQuery("SELECT * FROM Win32_Service WHERE Name LIKE 'TTTT%' AND StartMode = 'Manual'");
var enumServices = new Enumerator(colServices);

for(; !enumServices.atEnd(); enumServices.moveNext())
{
  var oService = enumServices.item();
  WScript.Echo(oService.Name);
}
泪眸﹌ 2024-08-26 18:20:27

我不确定您到底在问什么,但这...

if(Service.StartMode = 'mmManual')

...将始终评估为true。您缺少 =。应该是:

if(Service.StartMode == 'mmManual')

I'm not sure exactly what you're asking, but this...

if(Service.StartMode = 'mmManual')

...will always evaluate to true. You are missing an =. It should be:

if(Service.StartMode == 'mmManual')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文