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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I resolve this permissions error in my script?

Status
Not open for further replies.

wchull

MIS
Jun 14, 2001
93
US
Back in 2018 I wrote a vbscript to start a service if the service was stopped. Back then I had this run as a scheduled task at specific time but decided to remove if from scheduler and run it manually if I ever needed it. I'm switching computers and tried this script out on my old Windows 10 Pro PC before moving it to my new Windows 11 PC and it no longer works. I put some echo's in he script and found that the return code is 2 and according to what I found on the internet it's telling me I don't have permissions to start the service. Obviously, I have not run this script for a very long tome and when I wrote the script my PC was Windows 7 Pro where this action might have been allowed. Before I delete this script I wanted to see if there's a way that it can be run under Windows 10 and 11. Here's the code:

ServiceName = "Cloudberry Backup Service"
Set wmi = GetObject("winmgmts://./root/cimv2")
state = wmi.Get("Win32_Service.Name='" & ServiceName & "'").State
wscript.echo state
If State = "Stopped" then
Set shell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & ServiceName & "'")
For Each objService in colListOfServices
wscript.echo "Service Name: " & objservice.name & vbnewline & "State: " & objservice.state
objService.StartService()
errReturn = objService.StartService()
wscript.echo errReturn
Next
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top