SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

Remotely Shutting Down Windows (Building a Generic Shutdown Service with Task Scheduler)

Hello, this is Rcat.
This time, I will be remotely shutting down a Windows PC.
However, while this phrasing might make you think of Remote Desktop or shells, I will be using a completely different method.


Overview

As the title suggests, I will be using the Task Scheduler this time.
Task Schedulercan do things similar to systemd in Linux.
In other words, it means you canrun programs in the background as soon as the computer starts up.
By doing this, even onWindows, which is designed on the premise of logging in to operate, you canaccept access from various external tools.

Use Cases

For example, if you instruct theself-made assistant introduced in the next article to shut down, thechatbot can access the service and perform the shutdown.

I am also planning to use this for things like modifying the game server management tool introduced below for Windows and incorporating a feature to shut down the computer.

Explanation

Base

So, what you need to do is simple: just create a service solely for shutting down and make it so that it can accept access from the outside.

The basic knowledge required is explained in the following video.

Example

For instance, the tool I prepared this time is a web server, so when you access the shutdown URL, a shutdown button appears as shown below.
Since shutting down just by pressing a button carries the risk of accidental operation or mischief, I have set it up so that it will not trigger unless a pre-configured key code is provided.

In my case, it can be accessed via the URL below.
192.168.0.199:25050/shutdown
This is a mechanism where if you pass the key code to this URL via GET and it matches, it will shut down.

You access it like this.
It returned an error because the password was incorrect.

How to do it

First, create a program to perform the shutdown.
This time, I will use the web development template introduced in the video to write a program that accepts HTTP requests and performs a shutdown.
In simple terms, it meansaccessing a specific URL will trigger a shutdown.

Source Code

Here is the source code. It is super simple.
First,if you access it without specifying any parameters, the page will be displayed. That is theshutdown button.
Next, ifparameters are set, meaning a password has been sent, it willcompare it with the passwordwritten directly here.
Then, if the passwordmatches, it will execute the shutdown command.

Assuming you know the basics of Flask, this is just the URL event handler.If you don't understand, please watch the videoand you should be able to understand it.

About the shutdown command

Windows has a command for shutting down. It is called "shutdown".

If you check the help, various things will appear, but you can shut down like this.

shutdown /s

In the case of /s it is a shutdown, and in the case of /r it is a restart.

Also, you can use /t to shut down after N seconds.
In the following case, it will shut down after one hour.
When downloading large data before going to bed, I often calculate the approximate completion time and set this command in advance.

shutdown /s /t 3600
shutdown /s /t 3600

By the way, if there is time until the execution time, you can also cancel it by using "/a".

Steps to register in Task Scheduler

Search from Start.
This clock icon is it.

When you launch it, a confusing screen like this will open.
You can create it right away, but considering future organization, let's create a folder first.

Right-click on the folder icon of the Task Scheduler Library on the far left, create a new one, and create it with your preferred name.

Once you are ready, press the Create Basic Task button on the right.
The steps are as follows.

  • Enter name
    First, enter your preferred name

  • Select trigger
    Set it to At computer startup.

  • Select action
    Select Start a program.

  • Browse program script
    Here, I am entering the path for the batch file I created earlier to start the Python server.

  • Check Open properties and finish

If you check it, the properties will open, so please check Run whether user is logged on or not here. If you don't do this, you won't be able to shut down when the power is turned on remotely, for example.

If a new task is created as shown below, it is complete.
In this state, restart the computer and confirm that you can access the server without logging in.

About starting the program

I wrote that I created a batch file to start the program when registering, and the content is as follows.
It is simply content that starts a Python server script using a virtual environment.
It is known that it can be started by passing this path in Task Scheduler.

Filled-in sections are for download only

Data distribution

I am distributing the shutdown service web server I created this time below.
Please download it if you are interested, after agreeing to the terms of service.

https://script.google.com/macros/s/AKfycbxdcr8pnazR7RbjaSICTtaNWfN7h_rjQrKlZ3h9CZpPRFzRILk1OGc8mZqKbF-NXNO9/exec?name=WindowsTaskSchedulerServer

Also, since I used the template introduced in the video and only rewrote the necessary parts, the amount of source code is large, but there is also quite a bit of unused code. You might want to incorporate various services as a reference.

いいなと思ったら応援しよう!

Rcat999 情報が役に立ったと思えば、僅かでも投げ銭していただけるとありがたいです。