Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
1  
See this question –  Chad Cooper Jan 30 '12 at 4:12
add comment

4 Answers

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
share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

To stop and start services you could use AGSSOM command.

share|improve this answer
1  
Note: The version on ArcScripts is out of date (latest version as of now is 10.0.0.2 on the Code Gallery): resources.arcgis.com/gallery/file/net-web-adf/… –  blah238 Jan 30 '12 at 19:17
add comment

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.
oldBasePath = r"\\OLD_SERVER_NAME\SHARENAME"

# Step 2 - Replace the hostname of this path with the new hostname.
newBasePath = r"\\NEW_SERVER_NAME\SHARENAME"

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)
                            try:
                                print "Processing: " + mapDocPath
                                mxd = arcpy.mapping.MapDocument(mapDocPath)
                                mxd.findAndReplaceWorkspacePaths(oldBasePath, newBasePath)
                                mxd.save()
                            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."
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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