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."