<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> os
wrkspc <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C:/Project/BULK/'</SPAN>
<SPAN class="comment token">#mxd = arcpy.mapping.MapDocument('BULK_Test.mxd')</SPAN>
con <SPAN class="operator token">=</SPAN> <SPAN class="string token">'AGSconnectionfile.ags'</SPAN>
<SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">,</SPAN> dirs<SPAN class="punctuation token">,</SPAN> files<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> os<SPAN class="punctuation token">.</SPAN>walk<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> fl <SPAN class="keyword token">in</SPAN> files<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> fl<SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>endswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">".mxd"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">,</SPAN> fl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Provide other service details</SPAN>
service <SPAN class="operator token">=</SPAN> mxd
sddraft <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C:/Project/BULK/'</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.sddraft'</SPAN>
sd <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C:/Project/BULK/'</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.sd'</SPAN>
summary <SPAN class="operator token">=</SPAN> <SPAN class="string token">'BULK_Test'</SPAN>
tags <SPAN class="operator token">=</SPAN> <SPAN class="string token">'BULK_Test'</SPAN>
<SPAN class="comment token"># Create service definition draft</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>CreateMapSDDraft<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> sddraft<SPAN class="punctuation token">,</SPAN> mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ARCGIS_SERVER'</SPAN><SPAN class="punctuation token">,</SPAN> con<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Project"</SPAN><SPAN class="punctuation token">,</SPAN> summary<SPAN class="punctuation token">,</SPAN> tags<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Analyze the service definition draft</SPAN>
analysis <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>AnalyzeForSD<SPAN class="punctuation token">(</SPAN>sddraft<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Print errors, warnings, and messages returned from the analysis</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"The following information was returned during analysis of the MXD:"</SPAN>
<SPAN class="keyword token">for</SPAN> key <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'messages'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'warnings'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'errors'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'----'</SPAN> <SPAN class="operator token">+</SPAN> key<SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'---'</SPAN>
vars <SPAN class="operator token">=</SPAN> analysis<SPAN class="punctuation token">[</SPAN>key<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>message<SPAN class="punctuation token">,</SPAN> code<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> layerlist<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> vars<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">' '</SPAN><SPAN class="punctuation token">,</SPAN> message<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">' (CODE %i)'</SPAN> <SPAN class="operator token">%</SPAN> code
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">' applies to:'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="keyword token">for</SPAN> layer <SPAN class="keyword token">in</SPAN> layerlist<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">,</SPAN>
<SPAN class="keyword token">print</SPAN>
<SPAN class="comment token"># Stage and upload the service if the sddraft analysis did not contain errors</SPAN>
<SPAN class="keyword token">if</SPAN> analysis<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'errors'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Execute StageService. This creates the service definition.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>StageService_server<SPAN class="punctuation token">(</SPAN>sddraft<SPAN class="punctuation token">,</SPAN> sd<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Execute UploadServiceDefinition. This uploads the service definition and publishes the service.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>UploadServiceDefinition_server<SPAN class="punctuation token">(</SPAN>sd<SPAN class="punctuation token">,</SPAN> con<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Service successfully published"</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Service could not be published because errors were found during analysis."</SPAN>
<SPAN class="keyword token">print</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>I have a folder of MXDs and I want to publish all of them in batch to a single ArcGIS Server. I've had success using Python on a single MXD, but when I add a loop into the code so that I can do multiple services at once, I run into errors that I'm unable to troubleshoot. I found the code below on ArcGIS Enterprise site and tried to modify it for batch processing.
I've tried as a standalone script and from within ArcMap 10.7.1. The error:
File <SPAN class="string token">"C:\Project\BULK\PublishAGSBulk.py"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">29</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN>IOError<SPAN class="punctuation token">:</SPAN> Operation on file C<SPAN class="punctuation token">:</SPAN>\Users\<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>\Temp\<SPAN class="punctuation token">{</SPAN><SPAN class="number token">67337BAB</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1DDF</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">4A39</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">9FB4</SPAN><SPAN class="operator token">-</SPAN>B7C8EF8597B0<SPAN class="punctuation token">}</SPAN>\<SPAN class="operator token"><</SPAN>geoprocessing Map object object at <SPAN class="number token">0x036534A0</SPAN><SPAN class="operator token">></SPAN><SPAN class="punctuation token">.</SPAN>sddraft failed<SPAN class="punctuation token">.</SPAN> Unknown error<SPAN class="punctuation token">.</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>I am doing this with an active connection to the ArcGIS Server (10.8) in ArcCatalog.