We have the same use case as @BillFox, it needs to allow for multiple non-nested AD groups.We commonly have projects which span multiple parts of the business e.g. environment, engineering and property teams which all need access. They each have an AD group which we would like to be able to add to a Portal group, rather than having to add every user individually. If a user drops out of an AD group or a new user is added to an AD group the Portal group should reflect this and be updated.
There are similar ideas requesting the same functionality:
Groups from multipe active directory groupsInvite extra users to active directory group based groups
@BillFox, thanks for that additional information. We'll keep this in mind moving forward!
No, this should allow multiple Non-Nested AD groups be added to portal groups
@Anonymous User, there's been support for nested enterprise groups for many releases now. It's present in all supported versions of Enterprise (10.7+). Our product documentation goes into more detail regarding this: https://enterprise.arcgis.com/en/portal/10.7/administer/windows/create-groups.htm#ESRI_SECTION1_5E3FFFAA1B7E443FBB1E483E070B1979. Is this what you were referring to or were you wanting to use separate AD groups to create one group in your Enterprise organization?
@jill_es thanks, what version was it added at?
This functionality is currently present in the software.
As a workaround, this will add users from multiple groups to a PTL group. Run nightly as a scheduled task.
<SPAN class="keyword token">import</SPAN> subprocess <SPAN class="keyword token">import</SPAN> os<SPAN class="punctuation token">,</SPAN> sys<SPAN class="punctuation token">,</SPAN> string<SPAN class="punctuation token">,</SPAN> calendar<SPAN class="punctuation token">,</SPAN> datetime<SPAN class="punctuation token">,</SPAN> traceback<SPAN class="punctuation token">,</SPAN> smtplib <SPAN class="keyword token">import</SPAN> csv <SPAN class="keyword token">from</SPAN> os <SPAN class="keyword token">import</SPAN> listdir <SPAN class="keyword token">from</SPAN> os<SPAN class="punctuation token">.</SPAN>path <SPAN class="keyword token">import</SPAN> isfile<SPAN class="punctuation token">,</SPAN> join <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">import</SPAN> itertools <SPAN class="keyword token">from</SPAN> collections <SPAN class="keyword token">import</SPAN> Counter <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env <SPAN class="comment token"># Use this script when you need to add members of multiple AD groups to one or more PTL groups</SPAN> <SPAN class="comment token"># All scripts and files should read/write to the same directory</SPAN> <SPAN class="comment token">##########################</SPAN> <SPAN class="comment token">#Begin variables</SPAN> ptl <SPAN class="operator token">=</SPAN><SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fportal.com%2Fportal" target="_blank">https://portal.com/portal</A><SPAN>"</SPAN></SPAN> <SPAN class="comment token">#Coded to work with headless account</SPAN> <SPAN class="comment token"># Additional parameters will need to be defined to use enterprise account or token</SPAN> user <SPAN class="operator token">=</SPAN> <SPAN class="string token">"bigfoot"</SPAN> passw <SPAN class="operator token">=</SPAN> <SPAN class="string token">"sasquatch!"</SPAN> workdir <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C:\PRODUCTION\PTL_GROUPS\IANDM'</SPAN> <SPAN class="comment token"># List of PTL Groups that will include members of the multiple AD groups lists in adgroups.txt</SPAN> ptllist <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'GRSM IandM Map Editors'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'GRSM IandM Workspace'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'GRSM Vital Signs Editors'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'GRSM Vital Signs Workspace'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'GRSM Wetlands Editors'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'GRSM Wetlands Workspace'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'GRSM Sochan Workspace'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># a text file titled "adgroups.txt" containing the name of multiple AD groups (one per line, no delimiters or</SPAN> <SPAN class="comment token"># seperators) is required to be in the workdir</SPAN> <SPAN class="comment token"># Output file containing UPN of AD members from the groups listed in adgroups.txt</SPAN> admembers <SPAN class="operator token">=</SPAN> <SPAN class="string token">'GRSM_IANDM_USERS.csv'</SPAN> <SPAN class="comment token"># Define log setting</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> d <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> log <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C:\PYTHON_LOGS\\iandmLOG."</SPAN><SPAN class="operator token">+</SPAN>admembers<SPAN class="operator token">+</SPAN><SPAN class="string token">".txt"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"a"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"----------------------------"</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"----------------------------"</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Log: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Start process...</SPAN> starttime <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Begin process:\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" Process started at "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>starttime<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">### Start setting variables</SPAN> <SPAN class="comment token"># Mail Server Settings</SPAN> SERVER <SPAN class="operator token">=</SPAN> <SPAN class="string token">"mail.server"</SPAN> PORT <SPAN class="operator token">=</SPAN> <SPAN class="string token">"25"</SPAN> FROM <SPAN class="operator token">=</SPAN> <SPAN class="string token">"me"</SPAN> MAILDOMAIN <SPAN class="operator token">=</SPAN> <SPAN class="string token"><A class="jive-link-email-small" href="mailto:'@portal.com" rel="nofollow noopener noreferrer" target="_blank">'@portal.com</A><SPAN>'</SPAN></SPAN> <SPAN class="comment token"># Data Steward getting the email. Needs to be their email address...without @nps.gov at the end</SPAN> userList<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"the_man"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># get a list of usernames from the list of named tuples returned from ListUsers</SPAN> userNames <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>u <SPAN class="keyword token">for</SPAN> u <SPAN class="keyword token">in</SPAN> userList<SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># take the userNames list and make email addresses by appending the appropriate suffix.</SPAN> emailList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>name <SPAN class="operator token">+</SPAN> MAILDOMAIN <SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> userNames<SPAN class="punctuation token">]</SPAN> TO <SPAN class="operator token">=</SPAN> emailList <SPAN class="comment token"># Grab date for the email</SPAN> DATE <SPAN class="operator token">=</SPAN> d <SPAN class="comment token">#End variables</SPAN> <SPAN class="comment token">##########################</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> os<SPAN class="punctuation token">.</SPAN>remove<SPAN class="punctuation token">(</SPAN>admembers<SPAN class="punctuation token">)</SPAN><SPAN class="comment token"># Remove AD names list if it exists</SPAN> <SPAN class="keyword token">except</SPAN> OSError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">pass</SPAN> <SPAN class="comment token">#PS command to get members of ad group(s)</SPAN> command <SPAN class="operator token">=</SPAN> <SPAN class="string token">'$groups = Get-Content '</SPAN><SPAN class="operator token">+</SPAN>workdir<SPAN class="operator token">+</SPAN>'\\adgroups<SPAN class="punctuation token">.</SPAN>txt <SPAN class="punctuation token">;</SPAN>foreach<SPAN class="punctuation token">(</SPAN>$Group <SPAN class="keyword token">in</SPAN> $Groups<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>Get<SPAN class="operator token">-</SPAN>ADGroupMember <SPAN class="operator token">-</SPAN>Id $Group <SPAN class="operator token">|</SPAN> \ Where <SPAN class="punctuation token">{</SPAN> $_<SPAN class="punctuation token">.</SPAN>objectClass <SPAN class="operator token">-</SPAN>eq <SPAN class="string token">"user"</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="operator token">|</SPAN><SPAN class="operator token">%</SPAN><SPAN class="punctuation token">{</SPAN>Get<SPAN class="operator token">-</SPAN>ADUser $_<SPAN class="punctuation token">.</SPAN>SamAccountName <SPAN class="operator token">|</SPAN> select UserPrincipalName<SPAN class="punctuation token">}</SPAN> <SPAN class="operator token">|</SPAN> Export<SPAN class="operator token">-</SPAN>CSV <SPAN class="string token">'+workdir+'</SPAN>\\<SPAN class="string token">'+admembers+'</SPAN> \ <SPAN class="operator token">-</SPAN>NoTypeInformation <SPAN class="operator token">-</SPAN>append<SPAN class="punctuation token">}</SPAN>' process<SPAN class="operator token">=</SPAN>subprocess<SPAN class="punctuation token">.</SPAN>Popen<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'powershell'</SPAN><SPAN class="punctuation token">,</SPAN>command<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>stdin<SPAN class="operator token">=</SPAN>subprocess<SPAN class="punctuation token">.</SPAN>PIPE<SPAN class="punctuation token">,</SPAN>stdout<SPAN class="operator token">=</SPAN>subprocess<SPAN class="punctuation token">.</SPAN>PIPE<SPAN class="punctuation token">,</SPAN>stderr<SPAN class="operator token">=</SPAN>subprocess<SPAN class="punctuation token">.</SPAN>PIPE<SPAN class="punctuation token">)</SPAN> out<SPAN class="punctuation token">,</SPAN> err <SPAN class="operator token">=</SPAN> process<SPAN class="punctuation token">.</SPAN>communicate<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>process<SPAN class="punctuation token">.</SPAN>returncode<SPAN class="punctuation token">,</SPAN> out<SPAN class="punctuation token">,</SPAN> err<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Remove any special charactes, including quotes, from AD names list</SPAN> input_file <SPAN class="operator token">=</SPAN> workdir<SPAN class="operator token">+</SPAN><SPAN class="string token">'\\'</SPAN><SPAN class="operator token">+</SPAN>admembers chars <SPAN class="operator token">=</SPAN> <SPAN class="string token">'$%^*"_\n'</SPAN> <SPAN class="comment token"># etc notice the \n (linefeed)</SPAN> <SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>input_file<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> f<SPAN class="punctuation token">:</SPAN> lines <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>x<SPAN class="punctuation token">.</SPAN>strip<SPAN class="punctuation token">(</SPAN>chars<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> f<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>input_file<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"w"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> f<SPAN class="punctuation token">:</SPAN> f<SPAN class="punctuation token">.</SPAN>writelines<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{}\n"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> lines<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Initialize GIS connection to PTL</SPAN> gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>ptl<SPAN class="punctuation token">,</SPAN> user<SPAN class="punctuation token">,</SPAN> passw<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Loop through all PTL groups and do the following:</SPAN> <SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> ptllist<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#Get PTL group name</SPAN> group <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>groups<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'title: "'</SPAN><SPAN class="operator token">+</SPAN>item<SPAN class="operator token">+</SPAN><SPAN class="string token">'"'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>group<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Add AD users to PTL group</SPAN> readCSV <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>csv<SPAN class="punctuation token">.</SPAN>reader<SPAN class="punctuation token">(</SPAN>open<SPAN class="punctuation token">(</SPAN>workdir<SPAN class="operator token">+</SPAN><SPAN class="string token">'\\'</SPAN><SPAN class="operator token">+</SPAN>admembers<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> readCSV<SPAN class="punctuation token">:</SPAN> group<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>add_users<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'done'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Get list of all members in PTL group</SPAN> members <SPAN class="operator token">=</SPAN> group<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>get_members<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> members_list <SPAN class="operator token">=</SPAN> members<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'users'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#Get list of authorized members in PTL group</SPAN> allowed_list <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>itertools<SPAN class="punctuation token">.</SPAN>chain<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>readCSV<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Figure out who doesn't belong</SPAN> c1 <SPAN class="operator token">=</SPAN> Counter<SPAN class="punctuation token">(</SPAN>allowed_list<SPAN class="punctuation token">)</SPAN> c2 <SPAN class="operator token">=</SPAN> Counter<SPAN class="punctuation token">(</SPAN>members_list<SPAN class="punctuation token">)</SPAN> diff <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>c2 <SPAN class="operator token">-</SPAN> c1<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>elements<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Remove unauthorized group members</SPAN> <SPAN class="keyword token">for</SPAN> j <SPAN class="keyword token">in</SPAN> diff<SPAN class="punctuation token">:</SPAN> group<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>remove_users<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>str<SPAN class="punctuation token">(</SPAN>j<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Write nothing to log if success.</SPAN> endtime <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" Completed successfully in "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>endtime <SPAN class="operator token">-</SPAN> starttime<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">######################################################################</SPAN> <SPAN class="comment token">#Delete below if not want email on success</SPAN> <SPAN class="comment token">#Define email message if success</SPAN> <SPAN class="comment token">## SUBJECT = "Notification of Successful Update of "+str(ptllist)</SPAN> <SPAN class="comment token">## MSG = "Finished updating:"+str(ptllist)+" at "+ str(DATE)</SPAN> <SPAN class="comment token">## print (MSG)</SPAN> <SPAN class="comment token">## print (emailList)</SPAN> <SPAN class="comment token">## </SPAN> <SPAN class="comment token">## # Send an email notifying steward of successful archive</SPAN> <SPAN class="comment token">## #MESSAGE = "\ From: %s To: %s Subject: %s %s" % (FROM, ", ".join(TO), SUBJECT, MSG)</SPAN> <SPAN class="comment token">## MESSAGE = "Subject: %s\n\n%s" % (SUBJECT, MSG)</SPAN> <SPAN class="comment token">## try:</SPAN> <SPAN class="comment token">## try:</SPAN> <SPAN class="comment token">## print("Connecting to Server...")</SPAN> <SPAN class="comment token">## server = smtplib.SMTP(SERVER,PORT)</SPAN> <SPAN class="comment token">## try:</SPAN> <SPAN class="comment token">## print("Login...")</SPAN> <SPAN class="comment token">## try:</SPAN> <SPAN class="comment token">## print("Sending mail...")</SPAN> <SPAN class="comment token">## server.sendmail(FROM, TO, MESSAGE)</SPAN> <SPAN class="comment token">## except Exception as e:</SPAN> <SPAN class="comment token">## print("Send Error Mail\n" + e.message)</SPAN> <SPAN class="comment token">## except Exception as e:</SPAN> <SPAN class="comment token">## print("Error Authentication Server: check the credentials \n" + e.message)</SPAN> <SPAN class="comment token">## except Exception as e:</SPAN> <SPAN class="comment token">## print("Error Connecting to Server : check the URL of the server and communications port ( 25 and ' the default ) \n" + e.message)</SPAN> <SPAN class="comment token">## </SPAN> <SPAN class="comment token">## print("Quit.")</SPAN> <SPAN class="comment token">## server.quit()</SPAN> <SPAN class="comment token">## </SPAN> <SPAN class="comment token">## except Exception as e:</SPAN> <SPAN class="comment token">## print (e.message)</SPAN> <SPAN class="comment token">#Delete above if not want email on success</SPAN> <SPAN class="comment token">######################################################################</SPAN> <SPAN class="comment token"># Something with wrong</SPAN> <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Get the traceback object</SPAN> tb <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>exc_info<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> tbinfo <SPAN class="operator token">=</SPAN> traceback<SPAN class="punctuation token">.</SPAN>format_tb<SPAN class="punctuation token">(</SPAN>tb<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Concatenate information together concerning </SPAN> <SPAN class="comment token"># the error into a message string</SPAN> pymsg <SPAN class="operator token">=</SPAN> <SPAN class="string token">"PYTHON ERRORS:\nTraceback info:\n"</SPAN> <SPAN class="operator token">+</SPAN> tbinfo <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\nError Info:\n"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>sys<SPAN class="punctuation token">.</SPAN>exc_info<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> msgs <SPAN class="operator token">=</SPAN> <SPAN class="string token">"ArcPy ERRORS:\n"</SPAN> <SPAN class="operator token">+</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN> <SPAN class="comment token"># Return python error messages for use in </SPAN> <SPAN class="comment token"># script tool or Python Window</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>pymsg<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>msgs<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Print Python error messages for use in </SPAN> <SPAN class="comment token"># Python / Python Window</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN> <SPAN class="operator token">+</SPAN> pymsg <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN> <SPAN class="operator token">+</SPAN> msgs <SPAN class="operator token">+</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN> log<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Define email message if something went wrong</SPAN> SUBJECT <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Notification of Un-Successful Update of "</SPAN><SPAN class="operator token">+</SPAN>str<SPAN class="punctuation token">(</SPAN>ptllist<SPAN class="punctuation token">)</SPAN> MSG <SPAN class="operator token">=</SPAN> <SPAN class="string token">"This horrible thing happend:"</SPAN><SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>DATE<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN> <SPAN class="string token">"; "</SPAN> <SPAN class="operator token">+</SPAN>pymsg <SPAN class="operator token">+</SPAN> <SPAN class="string token">"; "</SPAN> <SPAN class="operator token">+</SPAN> msgs <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>MSG<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>emailList<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Send an email notifying steward of successful archive</SPAN> <SPAN class="comment token">#MESSAGE = "\ From: %s To: %s Subject: %s %s" % (FROM, ", ".join(TO), SUBJECT, MSG)</SPAN> MESSAGE <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Subject: %s\n\n%s"</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>SUBJECT<SPAN class="punctuation token">,</SPAN> MSG<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Connecting to Server..."</SPAN><SPAN class="punctuation token">)</SPAN> server <SPAN class="operator token">=</SPAN> smtplib<SPAN class="punctuation token">.</SPAN>SMTP<SPAN class="punctuation token">(</SPAN>SERVER<SPAN class="punctuation token">,</SPAN>PORT<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Login..."</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Sending mail..."</SPAN><SPAN class="punctuation token">)</SPAN> server<SPAN class="punctuation token">.</SPAN>sendmail<SPAN class="punctuation token">(</SPAN>FROM<SPAN class="punctuation token">,</SPAN> TO<SPAN class="punctuation token">,</SPAN> MESSAGE<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Send Error Mail\n"</SPAN> <SPAN class="operator token">+</SPAN> e<SPAN class="punctuation token">.</SPAN>message<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error Authentication Server: check the credentials \n"</SPAN> <SPAN class="operator token">+</SPAN> e<SPAN class="punctuation token">.</SPAN>message<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error Connecting to Server : check the URL of the server and communications port ( 25 and ' the default ) \n"</SPAN> <SPAN class="operator token">+</SPAN> e<SPAN class="punctuation token">.</SPAN>message<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Quit."</SPAN><SPAN class="punctuation token">)</SPAN> server<SPAN class="punctuation token">.</SPAN>quit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">.</SPAN>message<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></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><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></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>
Melden Sie sich an, um zu posten, Inhalte zu folgen und mehr. Neu hier? Kostenlos registrieren.