Solved! Go to Solution.
# 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'
Why don't you just run a scheduled task at night that disconnects all users?
# 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:
# 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'
Here is a script that I have used with a little bit more flexibility than using the 'All' keyword.