Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Start/Stop Services

Status
Not open for further replies.

steventor

IS-IT--Management
Dec 17, 2002
10
0
0
AU
I am trying to start/stop a service and start/stop a process but i am having trouble because the service/process names have spaces in them.

The script works fine for services/processes without any spaces.. Here is the script which i am having the problem with:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='Automatic Updates'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
objService.StopService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='Automatic Updates'")
For each objService in colServiceList
errReturn = objService.StopService()
Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='Automatic Updates'")
For each objService in colServiceList
errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='Automatic Updates'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
objService.StartService()
Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Process Test.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next



strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
Error = objWMIService.Create("Process Test.exe", null, null, intProcessID)


This works fine if i replace Automatic Updates with Alerter and Process Test.exe with notepad.exe.

Why are the spaces in the names causing a problem?

Any help would be appreciated.
 
Hello steventor,

Space shouldn't be a forbidden character for the Name property. It is rather your .create usage. (But, you said notepad.exe works for you therefore, I give a benefit of the doubt. Do check it again.)
Code:
const st_intProcessID=&H0100
const ec_userNotified=1
const sm_manual="Automatic"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
Error = objWMIService.Create("Process Test","Process Test", "Process Test.exe", st_intProcessID, ec_userNotified, sm_automatic, true, null, null, null)
If Error<>0 Then
    wscript.echo "Service not created. Operation aborted."
    wscript.quit(1)
End If
Test it again see if the problem persists. As to the parameters, always have the reference on hand.

regards - tsuji
 
Amendment:

I was taken to understand we're talking about service. By this, I have to correct the connection line:
Code:
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_[COLOR=red]service[/color]")
And no wonder notepad.exe is done successfully as we're told. But the query lines are all referring to win32_service... In any case, that's how service is created, stop and restart.

- tsuji
 
amendment(2):

Followup on the above, I want to leave your intProcessID to the original meaning which is the out-parameter, uniquely identifying the process so created. Hence, the part corresponding to process should be this:

[1] Terminate the process created with processid being intprocessid generated from the create process.

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where [COLOR=green]ProcessID = " & intProcessID[/color])
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

[2] Create process
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
Error = objWMIService.Create("Process Test.exe", null, null, intProcessID)
The intProcessID is the out-parameter unique identifier of the process. When locate it as in [1], always use the intProcessID to discover the process, because name (read-only parameter) may not be unique.

[3] Create service
Here I amend the code I posted above without using intProcessID name which was due to a misunderstanding of your intention.
Code:
const st_interactive=&H0100
const ec_userNotified=1
const sm_manual="Automatic"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
Error = objWMIService.Create("Process Test","Process Test", "Process Test.exe", st_interactive, ec_userNotified, sm_automatic, true, null, null, null)
If Error<>0 Then
    wscript.echo "Service not created. Operation aborted."
    wscript.quit(1)
End If
- tsuji
 
Amendment(3):

Sorry to borther you guys.

I've overlooked a minor but _important_ issue in referencing the exact process with intProcessID under control. It is of data type uint32, a unsigned integer of 8-byte long. But, in vbs, it will be converted to a signed integer. Hence a superficial phenonemon has to be addressed. This is how I do this---and, this is an amendment to the amendment(2) item [1]. The code has to be replaced taking into consideration of this fact.

[1'] Terminate the process created with processid being intprocessid generated from the create process.
Code:
const CvToUnsigned=4294967296
if intProcessID<0 then
    uintProcessID=CvToUnsigned+intProcessID
else
    uintProcessID=intProcessID
end if
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where ProcessID = " & uintProcessID)
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

- tsuji
 
Thanks for your help tsuji.. I still cant seem to get it from the posts(I think i am just missing something). I should have probably explained it better. This is what i am trying to do exactly.

I have 1 service and 1 process which are running. I want to create a script which will stop the service, once the service has been stopped, I need to stop the process.

After the process has been stopped I need to restart the service, and then once the service is restarted i need to start the process.

Let the process be called: Process Test.exe
Let the service be called: Automatic Updates

My original script works ok doing this if the process and the service do not have spaces in their names. Hence why notepad and alerter work fine. I am not too sure how to find and use the Process ID of a process which is already running.

Thanks in advance.
 
One more thing.. The process is located in the C:\Program Files\ALI directory..

Not too sure how to reference this.
 
steventor,

The space in the name should not pose any problem. You can easily verify it my say, make a copy of notepad.exe and call it "note pad.exe" with a space in the filename. Create a process from it and using name to terminate it without any problem.

- tsuji

ps. Hate to note that, with cut-and-paste, I forgot to correct the win32_process to win32_service which was already done previously! (very frustrating)
 
Thanks again tsuji.. Have got the spaces in the name working..

What about if the process is in the C:\Program Files directory as opposed to the Windows directory..
 
steventor,

I check it, shouldn't be a problem.
Code:
filespec="c:\program file\note pad.exe"  'renamed notepad copy

set svcProcess=getobject("winmgmts:root\cimv2:win32_process")
iret=svcProcess.create(filespec,nul,nul,pid)
set svcProcess=nothing
if iret<>0 then
	wscript.echo "iret=" & iret & vbcrlf & "Create failed."
	wscript.quit(1)
else
	wscript.echo iret & vbcrlf & pid & vbcrlf & hex(pid)
end if
wscript.echo "close the process, ok?..."
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & filespec & "'"
set cProc=svc.execquery(sQuery)
'note:might have more than 1 instance, in that case this construction will terminate all
wscript.echo cProc.count
for each oProc in cProc
	oProc.terminate
next
set cProc=nothing
set svc=nothing
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top