I would like to run a python script everytime the computer boots, without having to login.
To do so, I tried to create a windows service that will first run a powershell script (to do some basic check configurations) and that will run a simple python command line like this:
python ./app.py
But like I said I would like this script to run at startup and for this I've created a windows service. The problem is that even though the powershell script runs, the python command (and only this command) won't work.
Some usefull information
If I start the powershell script from a terminal: it works.
If I start the python script directly from a terminal: it works.
If I start the python script as a background process with pythonw
: it works.
I know the powershell scripts runs at startup because I asked him to create folders (as a test) to show me it works.
The python program is using multi threading and a network connection. I don't know if that's relevant but that just to say it's a pretty big program that doesn't need any human interaction and that works on its own. Maybe Windows is blocking something about that ?
Other information: the powershell script (and so the python script) both need to run as Administrator. I know it already does because with the following condition it creates a folder.
if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
mkdir C:\Users\Desktop\Test2
}
What I've tried
I first created a windows service in C# that runs with full privileges with this basic code:
var ps1File = @"C:\\path\\to\\powershell\\script\\start.ps1";
var startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -NoExit -ExecutionPolicy unrestricted -file \"{ps1File}\"",
UseShellExecute = true
};
Process.Start(startInfo);
The service does seem to works like I said, it really is the python command that won't.
I also tried to create a service with NSSM and with AlwaysUp too but nothing works.