[Solved] Python if funtion not working as intended


In the copy/pasted code, when defining service_name, [enter image description here][1] does not make sense. I assume it is a typo.
From what you said, here is an example of corrected code (for restarting services). You still have to adapt it if you want to stop services.

import wmi
services_to_monitor = ['SolarWindsAdministration','SolarWindsAgent64',
                       'SolarWindsAlertingServiceV2','SWCollectorService',
                       'SolarWindsCortex','SWInfoServiceSvc',
                       'SWInfoServiceSvcV3','SWJobEngineSvc2',
                       'SWBrowserIntegration','SolarWindsRecommendations',
                       'SolarWindsSyslogService','SolarWinds TFTP Server',
                       'SolarWinds NetPerfMon']
c = wmi.WMI()
for service in c.Win32_Service():
    if service.Name in services_to_monitor and service.State == 'Stopped':
        result, = service.StartService()        
        if result == 0:
            print("Service " +str(service.Name)+" started")
        else:
            print("Oops. Something went wrong starting "+str(service.Name))

0

solved Python if funtion not working as intended