简体   繁体   中英

Windows C# Console Application Through Task Scheduler Won't Run Unless User Is Logged In

The environment is Windows Server 2016 Standard, using a C# Console Application developed in Visual Studio 2019 and published via the VS2019 Publish Wizard. The weird thing is this seemed to work fine on our old instance of Windows Server 2008, but has not worked properly since we upgraded to 2016.

I've developed a C# Console Application to add data to my SQL Server database. It is published and running directly on the DB server using Task Scheduler. If I'm logged in when the scheduled task runs, no problems. But if I am not logged in, the console application does not run and I'm having trouble seeing how far it even gets.

Any advice on how to get this to run with a non-logged in user, or how to better troubleshoot where the hold-up is would be greatly appreciated.

Currently as a test, all I am trying to do is start the app, write to the event viewer application log and to a text file (neither happen unless logged in).

static void Main()
    {
        using (EventLog eventLog = new EventLog("Application"))
        {
            eventLog.Source = "Application";
            eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1);
        }

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        string status = "Start: " + DateTime.Now.ToString() + "...";
        Log(status);
        status = "Complete: " + DateTime.Now.ToString() + ". Press Enter to exit.";
        Log(status);
    }

public static void Log(string message)
    {
        // Create a writer and open the file:
        StreamWriter log;

        string LogFilePath = "C:\\[folder]\\LogFile.txt";

        if (!File.Exists(LogFilePath))
        {
            log = new StreamWriter(LogFilePath);
        }
        else
        {
            log = File.AppendText(LogFilePath);
        }

        // Write to the file:
        log.WriteLine(DateTime.Now);
        log.WriteLine(message);
        log.WriteLine();

        // Close the stream:
        log.Close();
        Console.WriteLine("-Log Entry Added");
    }

Interestingly, I do see a ClickOnce process added to the Task Manager when I run as a non-logged in user, but still doesn't work appropriately.

在此处输入图像描述

Here are some of the things I've tried so far:

  • Run as my own domain account, system and as a local administrator account
  • Call a.bat file with the following command to open the console application: "START /B "Name" CMD /c "E:\setup.exe"" The call to the.bat file works but the Console Application does not run properly
  • Call setup.exe or ConsoleApplication.application directly from task scheduler
  • Changing the output type from "Console Application" to "Windows Application"

Below are screenshots to show my selected settings in Task Scheduler: 在此处输入图像描述

在此处输入图像描述

Something that fixed my problem: I re-created the console application from scratch, but created using .NET Core 3.0 framework. Copied my code in and re-added packages. I then published this new application to the server and it worked as expected. Still not sure what to attribute this to, but this fixed my issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM