views:

111

answers:

2

Hello,

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.

+1  A: 

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')
McDowell
Yes, you are right!!! I updated the question.I still can't find how I determine data about the StartMode of the serive?
Roman Dorevich
+2  A: 

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);
}
Helen
What is "winmgmts://" + strComputer + "/root/CIMV2" ?
Roman Dorevich
@Roman: It's a moniker string used to connect to the WMI service (see http://msdn.microsoft.com/en-us/library/aa389292.aspx). It's the equivalent of `GetObject("winmgmts:")` in your script, but explicitly specifies the computer name (`.` means local computer) and the default WMI namespace path (`root\cimv2`).
Helen