Nonprivileged: create_main_task_only_runonce.cmd

File create_main_task_only_runonce.cmd, 3.4 KB (added by pcfreak, 11 years ago)

create_main_task_only_runonce.cmd

Line 
1@ECHO OFF
2CLS
3
4REM Detect current user (should be an administrator)
5SET USER=%USERNAME%
6SET DOM=%USERDOMAIN%
7SET ACCOUNT=%DOM%\%USER%
8SET TASKNAME=OpenVPN Logon Task Creator (main)
9
10REM Where to store the created XML-File
11SET XML=%temp%\%RANDOM%_temp.xml
12
13REM What to start via the generated task
14SET TOSTART=C:\ProgramData\OpenVPN\create_usertask.cmd
15
16ECHO ^<?xml version="1.0" encoding="UTF-16"?^> > "%XML%"
17ECHO ^<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"^> >> "%XML%"
18echo.   ^<RegistrationInfo^> >> "%XML%"
19echo.     ^<Date^>2001-01-01T01:01:01.01^</Date^> >> "%XML%"
20echo.     ^<Author^>Der PCFreak^</Author^> >> "%XML%"
21echo.     ^<Description^>This task will be executed at logon of any user. The script will then determine the username and domain/computer of this user. The user will then added to the group "Network Configuration Operators" and additionally a new scheduled task with name "%DOM%_%USER%_openvpn" will be created. The automatically created task will be set up to only run when the specific, detected user logs on. This task will then start OpenVPN-GUI with highest privileges at logon.^</Description^> >> "%XML%"
22echo.   ^</RegistrationInfo^> >> "%XML%"
23
24echo.   ^<Triggers^> >> "%XML%"
25echo.     ^<LogonTrigger^> >> "%XML%"
26echo.       ^<StartBoundary^>2001-01-01T01:01:01^</StartBoundary^> >> "%XML%"
27echo.       ^<Enabled^>true^</Enabled^> >> "%XML%"
28echo.     ^</LogonTrigger^> >> "%XML%"
29echo.   ^</Triggers^> >> "%XML%"
30
31echo.   ^<Principals^> >> "%XML%"
32echo.     ^<Principal id="Author"^> >> "%XML%"
33echo.       ^<UserId^>%ACCOUNT%^</UserId^> >> "%XML%"
34echo.       ^<LogonType^>Password^</LogonType^> >> "%XML%"
35echo.       ^<RunLevel^>HighestAvailable^</RunLevel^> >> "%XML%"
36echo.     ^</Principal^> >> "%XML%"
37echo.   ^</Principals^> >> "%XML%"
38
39echo.   ^<Settings^> >> "%XML%"
40echo.     ^<MultipleInstancesPolicy^>IgnoreNew^</MultipleInstancesPolicy^> >> "%XML%"
41echo.     ^<DisallowStartIfOnBatteries^>false^</DisallowStartIfOnBatteries^> >> "%XML%"
42echo.     ^<StopIfGoingOnBatteries^>false^</StopIfGoingOnBatteries^> >> "%XML%"
43echo.     ^<AllowHardTerminate^>true^</AllowHardTerminate^> >> "%XML%"
44echo.     ^<StartWhenAvailable^>false^</StartWhenAvailable^> >> "%XML%"
45echo.     ^<RunOnlyIfNetworkAvailable^>false^</RunOnlyIfNetworkAvailable^> >> "%XML%"
46echo.     ^<IdleSettings^> >> "%XML%"
47echo.       ^<StopOnIdleEnd^>true^</StopOnIdleEnd^> >> "%XML%"
48echo.       ^<RestartOnIdle^>false^</RestartOnIdle^> >> "%XML%"
49echo.     ^</IdleSettings^> >> "%XML%"
50echo.     ^<AllowStartOnDemand^>true^</AllowStartOnDemand^> >> "%XML%"
51echo.     ^<Enabled^>true^</Enabled^> >> "%XML%"
52echo.     ^<Hidden^>false^</Hidden^> >> "%XML%"
53echo.     ^<RunOnlyIfIdle^>false^</RunOnlyIfIdle^> >> "%XML%"
54echo.     ^<WakeToRun^>false^</WakeToRun^> >> "%XML%"
55echo.     ^<ExecutionTimeLimit^>P3D^</ExecutionTimeLimit^> >> "%XML%"
56echo.     ^<Priority^>7^</Priority^> >> "%XML%"
57echo.   ^</Settings^> >> "%XML%"
58
59echo.   ^<Actions Context="Author"^> >> "%XML%"
60echo.     ^<Exec^> >> "%XML%"
61echo.       ^<Command^>"%TOSTART%"^</Command^> >> "%XML%"
62echo.     ^</Exec^> >> "%XML%"
63echo.   ^</Actions^> >> "%XML%"
64
65echo ^</Task^> >> "%XML%"
66
67
68REM Create the task using schtasks
69REM use /f to make sure we can re-create this task on demand
70%windir%\system32\schtasks.exe /create /TN "%TASKNAME%" /XML "%XML%" /RU %ACCOUNT% /RP "" /F
71
72REM Delete temporary XML FILE
73DEL /Q "%XML%"