I need to connect to ArcGIS server with ArcPy and enumerate over map services. I need to:

  • Loop through each ArcGIS server map service
  • Stop/Start the service
  • Get the service source document (MXD/MSD) path

I am going to be running the findAndReplaceWorkspacePaths function on each service.

Can someone point me in the right direction how to do get a handle to the ArcGIS server map services please?


I've written a short python script that incorporates AGSSOM to stop running services,and then restart previously running services after performing some database maintenance. You can find it in answer to this question...Promrammatically start and stop map services with Python

This script does not identify the source map document for the service. However, you can use the "-describe" switch on the AGSSOM command to list details of the map service, one of which is "FilePath". This identifies the source document. The script below will list map documents for each map service:

# Import arcpy module
   import arcpy, subprocess

#Get list of map services currently running
   stream = os.popen('D:\GIS\Admin\AGSSOMv10.0\AGSSOM.exe -list')
   mapservices = stream.readlines()

   for item in mapservices:
       itemSplit = item.split(',')
       if len(itemSplit) > 1:
           stream2 = os.popen('D:\GIS\Admin\AGSSOMv10.0\AGSSOM.exe -describe ' + itemSplit[0])
           descMapServ = stream2.readlines()

           for desc in descMapServ:
               desc = desc.strip()
               if desc.find("FilePath") > -1:
                   desc = desc.replace("FilePath: ", "")
                   print desc
  • Thanks Jeff. This is a solution to part of my question. Although I was really hoping for something built into the ArcPy libraries. I'm still looking for a way to get the map service MXD/MSD path for each service. The best solution I have right now is to process the CFG files under the ArcGIS Server user/cfg directory. – Andy Arismendi Jan 30 '12 at 16:39
  • I edited the above post to include python code for extracting the map document path from a map service. Unfortunately, there are currently no native python procedures for this. But with AGSSOM you can get what you need. – Jeff Berry Jan 30 '12 at 16:52
  • Very nice, note that you don't need to import arcpy and you do need to import os. – valveLondon Feb 8 '12 at 18:44

You should be able to use ListMapServices which has its documentation here.


To stop and start services you could use AGSSOM command.


I wanted a single script solution without any additional dependencies. I am surprised I couldn't get objects representing ArcGIS server services. It would be nice if I could get more than just the names of the services from the ListMapServices function that @PolyGeo supplied. I ended up just parsing the ArcGIS server configuration file. Here is the script I ended up with. It doesn't start/stop individual services like I wanted to do, instead it just stops the whole SOM which stops all services.

Thanks for pointing out AGSSOM utility. I wish they would expose more to the ArcPy libraries though.

import os
import arcpy
from subprocess import call
import xml.etree.ElementTree as ET

# Step 1 - Replace the hostname of this path with the old hostname.

# Step 2 - Replace the hostname of this path with the new hostname.

directory = r"C:\Program Files (x86)\ArcGIS\Server10.0\server\user\cfg"

print "Stopping ArcGIS Server Object Manager"
call(["net.exe", "stop", "ArcServerObjectManager"])

for root, dirs, files in os.walk(directory):
    for myFile in files:
            fileExt = os.path.splitext(myFile)[1]
            if (fileExt == ".cfg"):
                fullPath = os.path.join(root, myFile)
                rootElem = ET.parse(fullPath)
                for elem in rootElem.findall("./Info/WebCapabilities"):
                    list = elem.text.split(",")
                    if ("Map" in list):
                        for elem2 in rootElem.findall("./Properties/FilePath"):
                            mapDocPath = elem2.text
                            fileName = os.path.basename(mapDocPath)
                                print "Processing: " + mapDocPath
                                mxd = arcpy.mapping.MapDocument(mapDocPath)
                                mxd.findAndReplaceWorkspacePaths(oldBasePath, newBasePath)
                            except Exception, e:
                                print "Failed to process " + fileName + "! The error was: " + arcpy.GetMessages()
                                print e

print "Starting ArcGIS Server Object Manager"
call(["net.exe", "start", "ArcServerObjectManager"])

print "Done."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.