-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_task.bat
43 lines (38 loc) · 1.4 KB
/
set_task.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@echo off
REM Ensure script is running with admin privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
powershell -Command "Start-Process cmd -ArgumentList '/c \"%~f0\"' -Verb RunAs"
exit /b
)
setlocal
REM Define variables
set "script_dir=%~dp0"
set "task_name=AutoConnectNjupt"
REM Generate task XML configuration
set "xml_file=%script_dir%task.xml"
(
echo ^<?xml version="1.0" encoding="UTF-16"?^>
echo ^<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task" version="1.2"^>
echo ^<RegistrationInfo^>
echo ^<Author^>Aiden^</Author^>
echo ^</RegistrationInfo^>
echo ^<Triggers^>
echo ^<EventTrigger^>
echo ^<Enabled^>true^</Enabled^>
echo ^<Subscription^>^<QueryList^>^<Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"^>^<Select Path="Microsoft-Windows-NetworkProfile/Operational"^>*^[System^[Provider[@Name='Microsoft-Windows-NetworkProfile'] and EventID=10000^]]^</Select^>^</Query^>^</QueryList^>^</Subscription^>
echo ^</EventTrigger^>
echo ^</Triggers^>
echo ^<Actions Context="Author"^>
echo ^<Exec^>
echo ^<Command^>%script_dir%auto_connect_njupt.bat^</Command^>
echo ^</Exec^>
echo ^</Actions^>
echo ^</Task^>
) > "%xml_file%"
REM Delete and create task
schtasks /delete /tn "%task_name%" /f
schtasks /create /tn "%task_name%" /xml "%xml_file%" /f
REM Clean up
del /f /q "%xml_file%"
endlocal