0

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

5
  • The current path looks to have a tab character in it \t. You might try the r specifier or simply use forward slashes filename="C:/temp/out.txt" Also your "runas" command is missing the directory separator after c: Commented Sep 9, 2024 at 13:44
  • 1
    This question is similar to: Python windows path slash. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Sep 9, 2024 at 13:46
  • 1
    @JonSG that was a typo, and even after fixing the path, the logging info still does not show Commented Sep 9, 2024 at 13:52
  • Did you try using the forward slashes? Commented Sep 9, 2024 at 13:53
  • 1
    @JonSG for the log path, yes Commented Sep 9, 2024 at 13:54

1 Answer 1

0

Your path "C:\temp\out.txt" has single backslashes, which are used as escape characters.

If you mean for them to actually be part of the path, I would suggest you to change them to double backslashes:

"C:\\temp\\out.txt"

An even better way is to simply use forward slashes:

"C:/temp/out.txt"

Sign up to request clarification or add additional context in comments.

3 Comments

after fixing the path, this did not solve my issue. Something else must be happening
Did you also do the same for the path(s) in the command?
yes, unfortunately no luck

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.