def main():
    
    totalSuccess = True
    out_file = None
    
    # Check arguments
    results = check_args()
    if not results:
        sys.exit(exitErrCode)
    
    # Get parameter values
    portal_url, user, password, output_file, output_per_user = results
    
    try:
        access_mode = 'w'
        
        # Create portal object
        portal = Portal(portal_url, user, password)
        
        if not portal:
            raise Exception('ERROR: Could not create "portal" object.')
        
        if not portal.logged_in_user():
            raise Exception('\nERROR: Could not sign in with specified credentials.')
        
        # Get portal users
        portal_users = portal.org_users()
        
        out_file = open(output_file, access_mode)
        out_file_dir = os.path.dirname(output_file)
        
        for portal_user in portal_users:
            
            user_name = portal_user['username']
            
            if user_name in username_exclude:
                continue
            
            if output_per_user:
                service_list_outfile = os.path.join(out_file_dir, '{}_ServiceList.txt'.format(user_name))
                service_list_qc_outfile = os.path.join(out_file_dir, '{}_CreateServiceList_output.txt'.format(user_name))
                access_mode = 'OVERWRITE'
                redirect = '>'
            else:
                service_list_outfile = os.path.join(out_file_dir, 'ServiceList.txt')
                service_list_qc_outfile = os.path.join(out_file_dir, 'CreateServiceList_output.txt')
                access_mode = 'APPEND'
                redirect = '>>'
                
            script_parameters = '{} {} {} {} {} {} {} {} {}'.format(
                    os.path.join(script_path, 'CreateServiceList.py'),
                    portal_url, user, password, service_list_outfile,
                    access_mode, user_name, redirect, service_list_qc_outfile)
            
            out_file.write(script_parameters + '\n')
        
        print '\nCreated output batch file: {}\n'.format(output_file)
        print 'Done.'
        
    except:
        totalSuccess = False
        
        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
     
        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
     
        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"
        
    finally:
        if hasattr(out_file, 'close'):
            out_file.close()
        if totalSuccess:
            sys.exit(0)
        else:
            sys.exit(1)
dataset/ETHPy150Open Esri/ops-server-config/Utilities/CreateBatchFileToRunCreateServiceList.py/main/original