I am running a python script as a different user with the runas command but I cannot see my output/errors anywhere. I am trying to debug my script, but I cannot see any logging info so it is near impossible to for me to tell what is happening.
Here is what my script looks like:
def main():
logging.basicConfig(filename="C:/temp/out.txt",
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
# using pyodbc
# Connection string for SQL Server using Windows Authentication
conn_str = (
r'DRIVER={ODBC Driver 17 for SQL Server};'
r'SERVER=server;'
r'DATABASE=database;'
r'Trusted_Connection=yes;'
)
try:
# Attempt to establish a connection
conn = pyodbc.connect(conn_str)
logging.info("Connection successful!")
# Close connection
conn.close()
except pyodbc.Error as e:
# Handle connection errors
if '28000' in str(e): # SQL Server Login failed
logging.info("Authentication failed: User does not have access to the database.")
else:
logging.info(f"An error occurred: {e}")
if __name__ == "__main__":
main()
Here is the runas command I use:
runas /user:domain\username "C:/path/to/exe" "C:/temp/file.py"
Everytime I run the command nothing gets logged to my output file and I am unsure what is happening. Am I missing something obvious here?
I know something is going on because when I run the script manually in my IDE the logging works, and I will get an error since I don't have access to the DB and I can see it in the server error logs. When I run with runas I don't see any error logs on the server, but also no logging info
tabcharacter in it\t. You might try therspecifier or simply use forward slashesfilename="C:/temp/out.txt"Also your "runas" command is missing the directory separator afterc: