# Import necessary modules. import arcpy, datetime # Set the admin connection -> this user needs to have privilegs to disconnect users. adminConnection = r'Database Connections\dbo@testDB@testServer.sde' # Get a list of connected users. uList = arcpy.ListUsers(adminConnection) # Print the number of connected users count = len(uList) print 'There are currently {0} users connected\n'.format(count) # Exception lists -> these are used to determine machine names or users # which will not be disconnected. Leave blank if there are no exceptions machines = ['devinci','rocky'] users = ['samhill', 'stevepeat', 'stevesmith'] # Get current time. now = datetime.datetime.now() # Get time a week ago weekAgo = now - arcpy.time.EsriTimeDelta(7, 'days') # Iterate through users for u in uList: if u.ConnectionTime < weekAgo: if u.ClientName.lower() in machines: print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName) print "Machine name is on exception list.\n" elif u.Name.lower() in users: print "Skipping user: {0} on machine: {1}".format(u.Name, u.ClientName) print "User's name is on exception list.\n" else: try: arcpy.DisconnectUser(adminConnection, u.ID) print "Successfully disconnected user: {0}\n".format(u.Name) except: print arcpy.GetMessages() else: print "User ({0}) connection time did not exceed the set limit.\n".format(u.Name) # Get a list of connected users. uList = arcpy.ListUsers(adminConnection) # Print the number of connected users count = len(uList) print 'There are currently {0} users connected'.format(count) print 'Done'
Hi, the website seems to have killed your code formatting, I have just tidied it up for use myself, thought I would re-post for anyone else that may stumble upon this thread:
<SPAN class="comment token"># Import necessary modules. </SPAN> <SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> datetime <SPAN class="comment token"># Set the admin connection -> this user needs to have privilegs to disconnect users. </SPAN> adminConnection <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'Database Connections\dbo@testDB@testServer.sde'</SPAN> <SPAN class="comment token"># Get a list of connected users. </SPAN> uList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListUsers<SPAN class="punctuation token">(</SPAN>adminConnection<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Print the number of connected users </SPAN> count <SPAN class="operator token">=</SPAN> len<SPAN class="punctuation token">(</SPAN>uList<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'There are currently {0} users connected\n'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>count<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Exception lists -> these are used to determine machine names or users </SPAN> <SPAN class="comment token"># which will not be disconnected. Leave blank if there are no exceptions </SPAN> machines <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'devinci'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'rocky'</SPAN><SPAN class="punctuation token">]</SPAN> users <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'samhill'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'stevepeat'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'stevesmith'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Get current time. </SPAN> now <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> <SPAN class="comment token"># Get time a week ago </SPAN> weekAgo <SPAN class="operator token">=</SPAN> now <SPAN class="operator token">-</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>time<SPAN class="punctuation token">.</SPAN>EsriTimeDelta<SPAN class="punctuation token">(</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'days'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Iterate through users for u in uList: </SPAN> <SPAN class="keyword token">if</SPAN> u<SPAN class="punctuation token">.</SPAN>ConnectionTime <SPAN class="operator token"><</SPAN> weekAgo<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> u<SPAN class="punctuation token">.</SPAN>ClientName<SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> machines<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Skipping user: {0} on machine: {1}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>u<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">,</SPAN> u<SPAN class="punctuation token">.</SPAN>ClientName<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Machine name is on exception list.\n"</SPAN> <SPAN class="keyword token">elif</SPAN> u<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> users<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Skipping user: {0} on machine: {1}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>u<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">,</SPAN> u<SPAN class="punctuation token">.</SPAN>ClientName<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"User's name is on exception list.\n"</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>DisconnectUser<SPAN class="punctuation token">(</SPAN>adminConnection<SPAN class="punctuation token">,</SPAN> u<SPAN class="punctuation token">.</SPAN>ID<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Successfully disconnected user: {0}\n"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>u<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</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="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"User ({0}) connection time did not exceed the set limit.\n"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>u<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Get a list of connected users. </SPAN> uList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListUsers<SPAN class="punctuation token">(</SPAN>adminConnection<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Print the number of connected users </SPAN> count <SPAN class="operator token">=</SPAN> len<SPAN class="punctuation token">(</SPAN>uList<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'There are currently {0} users connected'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>count<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'Done'</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>
Here is a script that I have used with a little bit more flexibility than using the 'All' keyword.
Why don't you just run a scheduled task at night that disconnects all users?
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.