def getUsers(self): self.current_item += 1 global userList self.itemPrint() # ↓ try: userList = arcpy.ListUsers(self.sde_connection) self.itemPrint() # ↑ maintenance_log.appendToFile([ self.maintenance_item[self.current_item], self.maintenance_status[1] ]) except Exception as e: self.handleException(e) self.current_item += 1 self.itemPrint() # ↓ try: user_before_log.appendToFile(userList) self.itemPrint() # ↑ maintenance_log.appendToFile([ self.maintenance_item[self.current_item], self.maintenance_status[1] ]) except Exception as e: self.handleException(e)
def get_connected_user(): # get a list of connected users. _user_list = arcpy.ListUsers(sedFileSde) _count = 0 for _user in _user_list: print("Connected user: "******"Number of connected users: " + str(_count)) return _count
# Configure the e-mail server and other info here. mail_server = 'smtprelay.domain.gov' mail_from = 'GIS DBA <*****@*****.**>' # ------------------------------------------------------------------------------ # DO NOT UPDATE BELOW THIS LINE OR RISK DOOM AND DISPAIR! Have a nice day! # ------------------------------------------------------------------------------ # Import Python libraries import arcpy, time, smtplib, string, re # List Users in Database arcpy.env.workspace = db_connection db_connection = arcpy.env.workspace userList = arcpy.ListUsers(db_connection) # Form User Account Listing emaillist = [user.Name for user in arcpy.ListUsers(db_connection)] emaillist = [i for i in set(emaillist)] # Count users in array listcount = len(emaillist) # Set how many e-mails have been sent emailed_count = 0 sent_msg = 0 # Begin Individual E-mail Delivery Process # Check count against array. If <= continue. Else, end. if emailed_count <= listcount:
def main(): users = arcpy.ListUsers("Database Connections/SDEPROD_SDE.sde") for user in users: print("Username: {0}, Connected at: {1}".format( user.Name, user.ConnectionTime))
# -*- coding:utf-8 -*- # disconnect users release the lock import arcpy #创建的sde连接文件 connection = 'Database Connections/[email protected]' users = arcpy.ListUsers(connection) print len(users) arcpy.AcceptConnections(connection, False) arcpy.DisconnectUser(connection, 'All') users = arcpy.ListUsers(connection) print len(users) #should be 1
try: import os, zipfile, re, timeit, sys, getpass, datetime, arcpy, logging, glob, arceditor from arcgis.gis import GIS except Exception as e: print(e) input('Press Enter') arcpy.env.workspace = r"\\Apexgis\GIS\ApexSDE\current" arcpy.env.overwriteOutput = 1 arcpy.Delete_management('in_memory') # Clear users logged in admin_workspace = r'C:\Users\Jlong\AppData\Roaming\ESRI\Desktop10.5\ArcCatalog\APEXWCD_ADMIN.sde' users = arcpy.ListUsers(admin_workspace) for user in users: print("Username: {0}, Connected at: {1},{2}".format( user.Name, user.ConnectionTime, user.ID)) if user.Name == 'WCD': print("Disconnecting----{}".format(user.Name)) arcpy.DisconnectUser(admin_workspace, user.ID) if user.Name == 'WCDVIEWER': print("Disconnecting----{}".format(user.Name)) arcpy.DisconnectUser(admin_workspace, user.ID) def clearWSLocks(inputWS): '''Attempts to clear locks on a workspace, returns stupid message.''' if all([ arcpy.Exists(inputWS),
import arcpy users = arcpy.ListUsers("Database Connections/Connection to sde_sde_db_rac.sde") # for user in users: # print("Username: {0}, Connected at: {1}".format( # user.Name, user.ConnectionTime)) # import arcpy # Set the admistrative workspace connection # arcpy.env.workspace = "Database Connections/[email protected]" # Create a list of users ''' NOTE: When the arcpy.env.workspace environment is set, a workspace does not need to be provided to the function. ''' # users = arcpy.ListUsers() # Create a list of SDE ID's. # Use a list comprehension to get the ID values in a new list. id_users = [user.ID for user in users] print(id_users) # import arcpy arcpy.DisconnectUser("Database Connections/Connection to sde_sde_db_rac.sde", "ALL")
''' Created on Feb 28, 2013 @author: kyleg ''' import arcpy users = arcpy.ListUsers("Database Connections/SDEDEV_SDE.sde") for user in users: print("Username: {0}, Connected at: {1}".format(user.Name, user.ConnectionTime))
#close the file error_Log.close() ################################################################################################################################ # PRE-MAINTENANCE CHECKS: print("Getting list of connected users...") # method to return a tuple of all currently connected DB users; tuple is assigned to the "userList" variable userList = [] try: userList = arcpy.ListUsers(db) # "db" variable defined in line *31* above with open(maintenance_log_FileName, 'a+') as maintenance_Log: maintenance_Log.write('\nGet connectedUsers list: SUCCESS!') maintenance_log.close() print("...done getting list of connected users!\n ") except Exception as e: print(e) error_count = error_count + 1 script_fails_time = datetime.datetime.now() with open( "C:\\GIS_Scripts\\SDE_Maintenance\\Update_2018-08-14\\Error_Log.txt", 'a+') as error_Log: error_Log.write(
import time import datetime import arcpy #Set Variables hour = 16 #Hour of the day to stop (24 hour). minute = 30 #Minute of that hour to stop. sdeCon = r'<path_to_gdb_admin_connection_file\<file_name>.sde' computerName = '<instance_name>' pauseMinutes = .5 #Figure out when to stop and how long to pause stop = datetime.time(hour, minute) now = datetime.datetime.now() finishTime = datetime.datetime.combine(now, stop) pause = pauseMinutes * 60 while finishTime > datetime.datetime.now(): userList = arcpy.ListUsers(sdeCon) idList = [(user.ID, user.Name) for user in userList if user.ClientName.lower() != computerName.lower()] if len(idList) == 0: print "Nobody else is connected" for id in idList: print 'Disconnecting user {0}'.format(id[1]) arcpy.DisconnectUser(sdeCon, id[0]) time.sleep(pause) else: print "Quittin' time"
import arcpy, time, smtplib # set the workspace arcpy.env.workspace = 'Database Connections/admin.sde' # set a variable for the workspace workspace = arcpy.env.workspace # get a list of connected users. userList = arcpy.ListUsers("Database Connections/admin.sde") # get a list of usernames of users currently connected and make email addresses emailList = [ u.Name + "@yourcompany.com" for user in arcpy.ListUsers("Database Connections/admin.sde") ] # take the email list and use it to send an email to connected users. SERVER = "mailserver.yourcompany.com" FROM = "SDE Admin <*****@*****.**>" TO = emailList SUBJECT = "Maintenance is about to be performed" MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off." # Prepare actual message MESSAGE = """\ From: {0:s} To: {1:s} Subject: {2:s} {3:s}
wrkspc = env.workspace ##-------------------------------------------------- ##-----Allow connections to the geodatabase. Previuos code executions code have ##-------executed unsuccessfully leading to the fact that the geodatabase is a state that cannot accept connections ##-------Ensures no manual intervention via creationof geodatabase connection as DBA to allow connections is NEEDED ##---------------------------------------------------------- #Accept new connections to the database. arcpy.AcceptConnections(connSDE, True) ##--- ##---Find Connected Users ##--- # get a list of connected users userList = arcpy.ListUsers(connSDE) ##--- ##---Pass the list of connected users ##--- ## # get a list of user names from the list of named tuples returned from ListUsers ## userNames = [u.Name for u in userList] ## ## # take the userNames list and make email addresses by appending the appropriate suffix. ## emailList = [name + '@company.com' for name in userNames] ##--- ##---Generate and send an E-Mail ##---
import arcpy, time, smtplib, random # get a list of connected users. userList = arcpy.ListUsers("Database Connections/###.sde") # get a list of usernames of users currently connected and make email addresses emailList = [ user.Name for user in arcpy.ListUsers("Database Connections/###.sde") ] filteredEmail = [] for users in emailList: try: #print users.split('\\',1)[1].split('"',1)[0] + "@###.org" if users.split('\\', 1)[1].split('"', 1)[0] == 'ARCGIS': #do not mail to 'ARCGIS' user (web services) pass else: filteredEmail = users.split('\\', 1)[1].split('"', 1)[0] + "@####.org" except: #do not mail to 'DBO' user (me) pass print filteredEmail # take the email list and use it to send an email to connected users. SERVER = "Your mail server" FROM = "SDE Admin <ccantey@####>" TO = filteredEmail SUBJECT = "Maintenance is about to be performed" MSG = "Auto generated Message.\n\rGIS: Server maintenance will be performed in 5 minutes, please save all edits and maps. \nReconciling and posting all edited versions of ####.sde. \n\nPlease log off of all ArcGIS applications."
#disconnectArcSDEProcesses.py #Purpsoe: #get rid of schema locking #import Modules import arcpy, string #banner print "Disconnect All AreSDE Processes" #todo: set the parameters gdb = r"Database Connections\[email protected]" usernames = ['SDE'] #todo: Loop throught the current connections hitting the db users = arcpy.ListUsers(sde_workspace=gdb) for user in users: #todo: disconnect all users if user.Name not in usernames: #arcpy.DisconnectUser(sde_workspace = gdb, users = user.ID) print user.Name
#Set the workspace logger.info("Defining the Workspace...") arcpy.env.workspace = "C:\\ScriptsForArcGIS\\OPERATIONS - Arcadmin.sde" outputMessage("Defining the Workspace...") #Set a variable for the workspace logger.info("Define variable for the Workspace...") outputMessage("Define variable for the Workspace...") workspace = arcpy.env.workspace logger.info("Workspace is: {0}".format(workspace)) outputMessage("Workspace is: {0}".format(workspace)) #Get a list of connected users. logger.info("Get a list of connected users...") outputMessage("Get a list of connected users...") userList = arcpy.ListUsers(workspace) userNames = [user.Name for user in userList] logger.info("Connected users are:") logger.info(userNames) outputMessage("Connected users are:") outputMessage(userNames) #Block new connections to the database. logger.info("Block new connections to the database...") outputMessage("Block new connections to the database...") arcpy.AcceptConnections(workspace, False) logger.info("Connections blocked.") outputMessage("Connections blocked.") #Disconnect all users from the database. logger.info("Disconnect all users from the database...")
server = smtplib.SMTP("172.21.1.147") server.sendmail(FROM, TO, message) server.close() log.write(str(time.asctime()) + ": Successfully sent email.\n") except: log.write(str(time.asctime()) + ": Failed to send email.\n") # Error handling for majority of script try: # Block new connections to the database. arcpy.AcceptConnections(sdeWorkspace, False) log.write(str(time.asctime()) + ": Database connections blocked.\n") # Get a list of database connections then disconnect users userList = arcpy.ListUsers(sdeWorkspace) log.write(str(time.asctime()) + ": " + str(userList)) if len( userList ) > 1: # Always "1" connection because script initiates connection to the gdb log.write("\n" + str(time.asctime()) + ": There are " + str(len(userList) - 1) + " database connections.\n") arcpy.DisconnectUser(sdeWorkspace, "ALL") log.write(str(time.asctime()) + ": User(s) have been disconnected.\n") else: log.write("\n" + str(time.asctime()) + ": There are no connections to the geodatabase.\n") # Reconcile and post edits to dbo.DEFAULT. defaultWorkspace = r"C:\Users\-bjones\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\[email protected]" reconcileLog = r"C:\Geodatabase Compress\ReconcileLog.txt"