Skip to content

Commit 2de1570

Browse files
committed
added setTask.vbs for automatic task creation
1 parent e10544e commit 2de1570

File tree

4 files changed

+135
-5
lines changed

4 files changed

+135
-5
lines changed

ExchangeSetOOF.exe

1.5 KB
Binary file not shown.

Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void Main(string[] args) {
5454
}
5555

5656
// find next "out of office" appointment being either today or on the next business day
57-
logfile.WriteLine("getting oof appointments..");
57+
logfile.WriteLine("getting oof appointments");
5858
DateTime startDate = DateTime.Now; //startDate = DateTime.Parse("2016-08-05"); //uncomment to test/debug
5959
DateTime endDate = startDate.AddBusinessDays(2); // need to add 2 days because otherwise the endDate is <nextBDate> 00:00:00
6060
// Initialize the calendar folder object with only the folder ID.
@@ -201,14 +201,14 @@ static void Main(string[] args) {
201201
myOOF.State = OofState.Scheduled;
202202
// Select the scheduled time period to send OOF replies.
203203
myOOF.Duration = new TimeWindow(myStartOOFDate, myEndOOFDate);
204-
logfile.WriteLine("oof appointment detected and OOFstate disabled, so schedule set, oof state set to scheduled and int/ext replies set changed accordingly:");
204+
logfile.WriteLine("oof appointment detected, so schedule set to " + myStartOOFDate.ToString() + " - " + myEndOOFDate.ToString() + ", oof state set to scheduled and int/ext replies set changed accordingly:");
205205
logfile.WriteLine("=================================================== internal Reply:");
206206
logfile.WriteLine(myOOF.InternalReply.Message);
207207
logfile.WriteLine("=================================================== external Reply:");
208208
logfile.WriteLine(myOOF.ExternalReply.Message);
209209
logfile.WriteLine("===================================================");
210210
logfile.Flush();
211-
} else if ((oofAppointment == null) && myOOF.State != OofState.Disabled) {
211+
} else if (oofAppointment == null && myOOF.State != OofState.Disabled) {
212212
// just in case exchange server didn't disable OOF automatically.
213213
myOOF.State = OofState.Disabled;
214214
logfile.WriteLine("no oof appointment detected and OOFstate not disabled, so set OOFstate to disabled (just in case exchange didn't do this)");
@@ -219,7 +219,7 @@ static void Main(string[] args) {
219219
}
220220
// Now send the OOF settings to Exchange server. This method will result in a call to EWS.
221221
try {
222-
logfile.WriteLine("sending changed OOF Settings to EWS..");
222+
logfile.WriteLine("sending changed OOF Settings to EWS");
223223
service.SetUserOofSettings(UserPrincipal.Current.EmailAddress, myOOF);
224224
} catch (Exception ex) {
225225
logfile.WriteLine("Exception occured when sending User OOF Settings to EWS: " + ex.Message);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ in case of whole single day absences both DateLangX[0] and DateLangX[1] are bein
3131
--> "am/on dd.mm.yyyy", there can be no time component for whole day absences!
3232

3333
Install: copy ExchangeSetOOF.exe (optionally ExchangeSetOOF.exe.cfg for different templatespec/replacements) and both Managed EWS assemblies (Microsoft.Exchange.WebServices.Auth.dll
34-
and Microsoft.Exchange.WebServices.dll) anywhere you want and start on a regular basis (e.g. using task scheduler), execution hints are sent to c:\temp\ExchangeSetOOF.log for problem determination.
34+
and Microsoft.Exchange.WebServices.dll) anywhere you want and start on a regular basis (e.g. using task scheduler, the vb script "setTask.vbs" does this automatically), execution hints/exceptions are sent to c:\temp\ExchangeSetOOF.log for problem determination.
3535

3636
Build: Download/clone repository to a folder named ExchangeSetOOF.
3737
To compile succesfully, you also need to download Managed EWS (used/tested version 2.2: https://www.microsoft.com/en-us/download/details.aspx?id=42951) and set references to Microsoft.Exchange.WebServices.Auth.dll

setTask.vbs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
' For all enumeration documentation see - http://msdn.microsoft.com/en-us/library/windows/desktop/aa383602%28v=vs.85%29.aspx
2+
Option Explicit
3+
4+
' TASK_ACTION_TYPE
5+
Const TASK_ACTION_EXEC = 0
6+
Const TASK_ACTION_COM_HANDLER = 5
7+
Const TASK_ACTION_SEND_EMAIL = 6
8+
Const TASK_ACTION_SHOW_MESSAGE = 7
9+
10+
' TASK_COMPATIBILITY
11+
Const TASK_COMPATIBILITY_AT = 0
12+
Const TASK_COMPATIBILITY_V1 = 1
13+
Const TASK_COMPATIBILITY_V2 = 2
14+
15+
' TASK_CREATION
16+
Const TASK_VALIDATE_ONLY = &H01&
17+
Const TASK_CREATE = &H02&
18+
Const TASK_UPDATE = &H04&
19+
Const TASK_CREATE_OR_UPDATE = &H06&
20+
Const TASK_DISABLE = &H08&
21+
Const TASK_DONT_ADD_PRINCIPAL_ACE = &H10&
22+
Const TASK_IGNORE_REGISTRATION_TRIGGERS = &H20&
23+
24+
' TASK_INSTANCES_POLICY
25+
Const TASK_INSTANCES_PARALLEL = 0
26+
Const TASK_INSTANCES_QUEUE = 1
27+
Const TASK_INSTANCES_IGNORE_NEW = 2
28+
Const TASK_INSTANCES_STOP_EXISTING = 3
29+
30+
' TASK_LOGON_TYPE
31+
Const TASK_LOGON_NONE = 0
32+
Const TASK_LOGON_PASSWORD = 1
33+
Const TASK_LOGON_S4U = 2
34+
Const TASK_LOGON_INTERACTIVE_TOKEN = 3
35+
Const TASK_LOGON_GROUP = 4
36+
Const TASK_LOGON_SERVICE_ACCOUNT = 5
37+
Const TASK_LOGON_INTERACTIVE_TOKEN_OR_PASSWORD = 6
38+
39+
' TASK_RUNLEVEL_TYPE
40+
Const TASK_RUNLEVEL_LUA = 0
41+
Const TASK_RUNLEVEL_HIGHEST = 1
42+
43+
' TASK_TRIGGER_TYPE2
44+
Const TASK_TRIGGER_EVENT = 0
45+
Const TASK_TRIGGER_TIME = 1
46+
Const TASK_TRIGGER_DAILY = 2
47+
Const TASK_TRIGGER_WEEKLY = 3
48+
Const TASK_TRIGGER_MONTHLY = 4
49+
Const TASK_TRIGGER_MONTHLYDOW = 5
50+
Const TASK_TRIGGER_IDLE = 6
51+
Const TASK_TRIGGER_REGISTRATION = 7
52+
Const TASK_TRIGGER_BOOT = 8
53+
Const TASK_TRIGGER_LOGON = 9
54+
Const TASK_TRIGGER_SESSION_STATE_CHANGE = 11
55+
' -------------------------------------------------------------------------------
56+
57+
Dim objTaskService, objRootFolder, objTaskFolder, objNewTaskDefinition
58+
Dim objTaskTrigger, objTaskAction, objTaskTriggers, blnFoundTask
59+
Dim oFSO
60+
Dim taskCommand
61+
62+
taskCommand = replace(Wscript.ScriptFullName, Wscript.ScriptName, "") & "ExchangeSetOOF.exe"
63+
msgbox taskCommand
64+
65+
' Create the TaskService object and connect
66+
Set objTaskService = CreateObject("Schedule.Service")
67+
call objTaskService.Connect()
68+
69+
' Get the Root Folder where we will place this task
70+
Set objTaskFolder = objTaskService.GetFolder("\")
71+
dim xmlText
72+
xmlText = "<?xml version=""1.0"" encoding=""UTF-16""?><Task version=""1.3"" xmlns=""http://schemas.microsoft.com/windows/2004/02/mit/task""><RegistrationInfo><Date>2015-09-18T21:21:38.8028563</Date><Author></Author></RegistrationInfo><Triggers><CalendarTrigger><Repetition><Interval>PT1H</Interval><Duration>PT10H</Duration><StopAtDurationEnd>false</StopAtDurationEnd></Repetition><StartBoundary>2015-09-18T07:00:00</StartBoundary><Enabled>true</Enabled><ScheduleByWeek><DaysOfWeek><Monday /><Tuesday /><Wednesday /><Thursday /><Friday /></DaysOfWeek><WeeksInterval>1</WeeksInterval></ScheduleByWeek></CalendarTrigger></Triggers><Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>true</StopIfGoingOnBatteries><AllowHardTerminate>true</AllowHardTerminate><StartWhenAvailable>false</StartWhenAvailable><RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable><IdleSettings><StopOnIdleEnd>true</StopOnIdleEnd><RestartOnIdle>false</RestartOnIdle></IdleSettings><AllowStartOnDemand>true</AllowStartOnDemand><Enabled>true</Enabled><Hidden>false</Hidden><RunOnlyIfIdle>false</RunOnlyIfIdle><DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession><UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine><WakeToRun>false</WakeToRun><ExecutionTimeLimit>P3D</ExecutionTimeLimit><Priority>7</Priority></Settings><Actions Context=""Author""><Exec><Command>" & taskCommand & "</Command></Exec></Actions></Task>"
73+
dim pTask, strUser, strPwd
74+
strUser = CreateObject("WScript.Network").UserName
75+
strPwd = GetPassword("Bitte Ihr Windows Passwort zum Starten des Tasks eingeben:" )
76+
if strPwd = "" then Wscript.Quit
77+
' set TASK_LOGON_PASSWORD if user can logon as a batch job (privilege)
78+
objTaskFolder.RegisterTask "ExchangeSetOOF", xmlText, TASK_CREATE_OR_UPDATE, strUser, strPwd, TASK_LOGON_INTERACTIVE_TOKEN, pTask
79+
80+
Set oFSO = CreateObject("Scripting.FileSystemObject")
81+
' Create temp folder, ignore error if already exists
82+
on error resume next
83+
oFSO.CreateFolder "C:\temp"
84+
msgbox "Der Task wurde in der Aufgabenplanung eingetragen, nicht vergessen: Abwesenheitsnotiz anpassen!"
85+
86+
87+
Function GetPassword( myPrompt )
88+
dim objIE
89+
Set objIE = CreateObject( "InternetExplorer.Application" )
90+
objIE.Navigate "about:blank"
91+
objIE.Document.Title = "Password " & String( 100, "." )
92+
objIE.ToolBar = False
93+
objIE.Resizable = False
94+
objIE.StatusBar = False
95+
objIE.Width = 400
96+
objIE.Height = 220
97+
Do While objIE.Busy
98+
WScript.Sleep 200
99+
Loop
100+
' Insert the HTML code to prompt for a password
101+
objIE.Document.Body.InnerHTML = "<div align=""center""><p>" & myPrompt _
102+
& "</p><p><input type=""password"" size=""20"" " _
103+
& "id=""Password""></p><p><input type=" _
104+
& """hidden"" id=""OK"" name=""OK"" value=""0"">" _
105+
& "<input type=""submit"" value="" OK "" " _
106+
& "onclick=""VBScript:OK.Value=1""></p></div>"
107+
108+
objIE.Document.Body.Style.overflow = "auto"
109+
objIE.Visible = True
110+
objIE.Document.All.Password.Focus
111+
112+
On Error Resume Next
113+
Do While objIE.Document.All.OK.Value = 0
114+
WScript.Sleep 200
115+
If Err Then 'user clicked red X (or alt-F4) to close IE window
116+
IELogin = Array( "", "" )
117+
objIE.Quit
118+
Set objIE = Nothing
119+
Exit Function
120+
End if
121+
Loop
122+
On Error Goto 0
123+
124+
' Read the password from the dialog window
125+
GetPassword = objIE.Document.All.Password.Value
126+
127+
' Close and release the object
128+
objIE.Quit
129+
Set objIE = Nothing
130+
End Function

0 commit comments

Comments
 (0)