Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't stop a service using vbscript
    primarykey
    data
    text
    <p>I'm using the following code to try to stop a service. I can display the service state using <strong><em>WScript.Echo objService.State</em></strong> so I know I have the right service name and that it can find it and determine its state as <em>running</em> or <em>stopped</em> but when I run this script I get an Error on Line 51: Error Not Found Code 80041002 (see screenshot)</p> <p>The line of code at 51 is:</p> <pre><code> objService.StopService() </code></pre> <p>Where am I going wrong? I can stop and start this via the command line using sc.exe and am able to control other services ie Alerter but as soon as I try to control this particular service it fails.</p> <p>Thanks</p> <blockquote> <p><strong>EDIT</strong> The full code from the script (Thanks Brandon Moretz who pointed out that I hadn't posted the full code so the Line number didn't mean anything &amp; I have changed the StartService() back to Stop as it originally was so now you have more to go on. Sorry!)</p> </blockquote> <pre><code>' 1. Check that the latest backup zip exists and store its name in LBZ (LatestBackupZip) ' 2. Stop the Livecontent Service ' 3. Remove .dbx, .lck and .log files from DataFolder ' 4. restart the service ' 5. Restore the database Dim fileNewest Dim fso Set fso = WScript.CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") 'Set ImportFldr = fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data\Import") Set ImportFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\export") 'Set DataFldr = fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data") Set DataFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data") For Each aFile In ImportFldr.Files sExtension = fso.GetExtensionName(aFile.Name) If sExtension = "log" Then 'Msgbox "The file extension is a " &amp; sExtension Else 'Msgbox "The file extension is a " &amp; sExtension If fileNewest = "" Then Set fileNewest = aFile Else 'If fileNewest.DateCreated &lt; aFile.DateCreated Then If CDate(fileNewest.DateCreated) &lt; CDate(aFile.DateCreated) Then Set fileNewest = aFile End If End If End If Next Msgbox "The Newest File in the folder is " &amp; fileNewest.Name &amp; chr(13) &amp; "Size: " &amp; fileNewest.Size &amp; " bytes" &amp; chr(13) &amp; "Was last modified on " &amp; FileNewest.DateLastModified ' Change to /Data 'WScript.Echo WshShell.CurrentDirectory WshShell.CurrentDirectory = DataFldr 'WScript.Echo WshShell.CurrentDirectory ' Stop the Livecontent service strComputer = "." Dim objService Set objWMIService = GetObject("winmgmts:\\" &amp; strComputer &amp; "\root\cimv2") Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name = 'LiveContent'") For Each objService in colServiceList WScript.Echo objService.State If objService.State = "Running" Then objService.StopService() 'Wscript.Sleep 5000 End If Next 'Dim objShell 'Set objShell = CreateObject("WScript.Shell") 'objShell.Run "%comspec% /k c: &amp; cd ""C:\Program Files (x86)\XyEnterprise\SDL LiveContent\"" &amp; """"loaddb RESTORE -'Dlc.file=C:\PROGRA~2\XYENTE~1\SDLLIV~1\data\Import\" &amp; fileNewest.Name &amp; " -Dlc.pswd=N2kAs72z""""" </code></pre> <p><img src="https://i.stack.imgur.com/KL2e2.png" alt="script error when trying to stop service"></p> <p><strong>LATEST EDIT</strong></p> <p>I have taken your code and still can't get it to work. I noticed that the line:</p> <p>Set objWMIService = GetObject("winmgmts:\" &amp; strComputer &amp; "\root\cimv2")</p> <p>was missing a \ at "winmgmts:\" which I have added and I like your check to see if there is an (x86) directory as I am testing this on a 32bit server but will move it over to a 64 when it is ready so that will work nicely.</p> <p>Also this section didn't work:</p> <pre><code>If fso.FolderExists( "C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data" ) Then Set DataFldr= fso.GetFolder("C:\Program Files (x86)\XyEnterprise\SDL LiveContent\data") Else If fso.GetFolder("C:\Program diles\XyEnterprise\SDL LiveContent\data") Then Set DataFldr= fso.GetFolder("C:\Program diles\XyEnterprise\SDL LiveContent\data") End If </code></pre> <p>But did if I changed the ElseIf <strong>fso.GetFolder</strong> to <strong>fso.FolderExists</strong></p> <p>The script runs fine if I comment out Line 78 </p> <p>objService.StopService()</p> <p>But as soon as I uncomment it I get an error:</p> <p>Line: 78<br> Char: 9<br> Error: Not found Code: 80041002 Source: SWbemObjectEx </p> <p>But the Service can be found as the line: WScript.Echo objService.State Echos its state to the screen.</p> <p>Really confuzzled now.</p> <pre><code>' 1. Check that the latest backup zip exists and store its name in LBZ (LatestBackupZip) ' 2. Stop the Livecontent Service ' 3. Remove .dbx, .lck and .log files from DataFolder ' 4. restart the service ' 5. Restore the database Dim fileNewest Dim ImportFldr Dim DataFldr Dim fso Set fso = WScript.CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") If fso.FolderExists( "C:\Program Files\XyEnterprise\SDL LiveContent\data\Import" ) Then Set ImportFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\Import") Else WScript.Echo "Warning: Import Directory can not be found" End If If fso.FolderExists( "C:\Program Files\XyEnterprise\SDL LiveContent\data\export" ) Then Set ImportFldr = fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data\export") Else WScript.Echo "Warning: Export Directory can not be found" End If If fso.FolderExists( "C:\Program Files\XyEnterprise\SDL LiveContent\data" ) Then Set DataFldr= fso.GetFolder("C:\Program Files\XyEnterprise\SDL LiveContent\data") Else WScript.Echo "Warning: Data Directory can not be found" End If For Each aFile In ImportFldr.Files sExtension = fso.GetExtensionName(aFile.Name) If sExtension = "log" Then 'Msgbox "The file extension is a " &amp; sExtension Else 'Msgbox "The file extension is a " &amp; sExtension If fileNewest = "" Then Set fileNewest = aFile Else If fileNewest.DateCreated &lt; aFile.DateCreated Then If CDate(fileNewest.DateCreated) &lt; CDate(aFile.DateCreated) Then Set fileNewest = aFile End If End If End If End If Next 'Msgbox "The Newest File in the folder is " &amp; fileNewest.Name &amp; chr(13) &amp; "Size: " &amp; fileNewest.Size &amp; " bytes" &amp; chr(13) &amp; "Was last modified on " &amp; FileNewest.DateLastModified ' Change to /Data WScript.Echo WshShell.CurrentDirectory WshShell.CurrentDirectory = DataFldr WScript.Echo WshShell.CurrentDirectory ' Stop the Livecontent service strComputer = "." Dim objService Set objWMIService = GetObject("winmgmts:\\" &amp; strComputer &amp; "\root\cimv2") Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name = 'LiveContent'") For Each objService in colServiceList WScript.Echo objService.State If objService.State = "Running" Then objService.StopService() WScript.Echo objService.State Wscript.Sleep 5000 End If Next </code></pre> <p><strong>EDIT 2</strong></p> <p>After very extensive testing we have come to the conclusion that there is nothing wrong with the script, it is just that this particular service will not stop with this method.</p> <p>To this end we have moved on and are now using</p> <pre><code>objShell.Run "sc start LiveContent" </code></pre> <p>And this works a treat.</p> <p>Thanks to Brandon for your help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload