-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCreate-ExchangeReporterTask.ps1
217 lines (172 loc) · 7.48 KB
/
Create-ExchangeReporterTask.ps1
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#--------------------------------------------------------------------------------------
# Exchange Reporter Task
# für Exchange Server 2010/2013/2016/2019
# www.frankysweb.de
#
# erstellt geplanten Task für Exchange Reporter
# by Frank Zöchling
#
#--------------------------------------------------------------------------------------
Param([Parameter(Mandatory=$false)][string]$Installpath = $PSScriptRoot)
clear-host
write-host "
------------------------------------------------------------------------------------------"
write-host "
_____ _ ______ _
| ___| | | | ___ \ | |
| |____ _____| |__ __ _ _ __ __ _ ___ | |_/ /___ _ __ ___ _ __| |_ ___ _ __
| __\ \/ / __| '_ \ / _`` | '_ \ / _`` |/ _ \ | // _ \ '_ \ / _ \| '__| __/ _ \ '__|
| |___> < (__| | | | (_| | | | | (_| | __/ | |\ \ __/ |_) | (_) | | | || __/ |
\____/_/\_\___|_| |_|\__,_|_| |_|\__, |\___| \_| \_\___| .__/ \___/|_| \__ \___|_|
__/ | | |
|___/ |_|
" -foregroundcolor cyan
write-host "
for Exchange Server 2010 / 2013 / 2016 / 2019 / Office365
www.FrankysWeb.de
Version: 3.12
------------------------------------------------------------------------------------------
"
#Laden der Funktionen aus "Include-Functions.ps1"
#--------------------------------------------------------------------------------------
#Laden der Funktionen aus "Include-Functions.ps1"
#--------------------------------------------------------------------------------------
write-host " Loading functions from Include-Functions.ps1:" -nonewline
$origpos = $host.UI.RawUI.CursorPosition
$origpos.X = 70
$functionfile = test-path "$installpath\Includes\Include-Functions.ps1"
if ($functionfile)
{
. "$installpath\Includes\Include-Functions.ps1"
$host.UI.RawUI.CursorPosition = $origpos
write-host "Done" -foregroundcolor green
}
else
{
$host.UI.RawUI.CursorPosition = $origpos
write-host "Error (not found)" -foregroundcolor red
exit 0
write-host ""
}
# settings.ini einlesen
#--------------------------------------------------------------------------------------
try
{
write-host " Loading settings from settings.ini:" -nonewline
$origpos = $host.UI.RawUI.CursorPosition
$origpos.X = 70
$globalsettingsfile = "$installpath\settings.ini"
$inifile = get-inicontent "$globalsettingsfile"
}
Catch
{
$host.UI.RawUI.CursorPosition = $origpos
write-host "Error" -foregroundcolor red
exit 0
write-host ""
}
$host.UI.RawUI.CursorPosition = $origpos
write-host "Done" -foregroundcolor green
write-host "
-----------------------------------------------------------------------------------------
What do you want to do?
1 - Create / refresh Task
2 - Delete Task
"
while ($aktion -notmatch 1 -and $aktion -notmatch 2)
{
$aktion = read-host " Action"
}
$filepath = "$installpath" + "\New-ExchangeReport.ps1"
if ($aktion -match 1)
{
$reportsettingshash = $inifile["Reportsettings"]
$reportsettings = convert-hashtoobject $reportsettingshash
$reportinterval = ($reportsettings | Where-Object {$_.Setting -eq "Interval"}).Value
if ($reportinterval -match 1)
{
write-host ""
$zeitpunkt = read-host " Please specify Starttime (Example: 22:00)"
$username = read-host " Please enter Task User (Domain\User)"
$SecurePassword = read-host " Password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$startTime = "$zeitpunkt" | get-date -format s
$taskService = New-Object -ComObject Schedule.Service
$taskService.Connect()
$rootFolder = $taskService.GetFolder($NULL)
$taskDefinition = $taskService.NewTask(0)
$registrationInformation = $taskDefinition.RegistrationInfo
$registrationInformation = $taskDefinition.RegistrationInfo
$registrationInformation.Description = "Exchange Reporter Task - www.FrankysWeb.de"
$registrationInformation.Author = $username
$taskPrincipal = $taskDefinition.Principal
$taskPrincipal.LogonType = 1
$taskPrincipal.UserID = $username
$taskPrincipal.RunLevel = 0
$taskSettings = $taskDefinition.Settings
$taskSettings.StartWhenAvailable = $true
$taskSettings.RunOnlyIfNetworkAvailable = $true
$taskSettings.Priority = 7
$taskTriggers = $taskDefinition.Triggers
$executionTrigger = $taskTriggers.Create(2)
$executionTrigger.StartBoundary = $startTime
$taskAction = $taskDefinition.Actions.Create(0)
$taskAction.Path = "powershell.exe"
$taskAction.Arguments = "-Command `"&'$installpath\New-ExchangeReport.ps1' -installpath '$installpath'`""
$job = $rootFolder.RegisterTaskDefinition("Exchange-Reporter (www.FrankysWeb.de)" , $taskDefinition, 6, $username, $password, 1)
}
if ($reportinterval -match 7)
{
write-host ""
$zeitpunkt = read-host " Please specify Starttime (Example: 22:00)"
$username = read-host " Please enter Task User (Domain\User)"
$SecurePassword = read-host " Password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$startTime = "$zeitpunkt" | get-date -format s
$taskService = New-Object -ComObject Schedule.Service
$taskService.Connect()
$rootFolder = $taskService.GetFolder($NULL)
$taskDefinition = $taskService.NewTask(0)
$registrationInformation = $taskDefinition.RegistrationInfo
$registrationInformation.Description = "Exchange Reporter Task - www.FrankysWeb.de"
$registrationInformation.Author = $username
$taskPrincipal = $taskDefinition.Principal
$taskPrincipal.LogonType = 1
$taskPrincipal.UserID = $username
$taskPrincipal.RunLevel = 0
$taskSettings = $taskDefinition.Settings
$taskSettings.StartWhenAvailable = $true
$taskSettings.RunOnlyIfNetworkAvailable = $true
$taskSettings.Priority = 7
$taskTriggers = $taskDefinition.Triggers
$executionTrigger = $taskTriggers.Create(3)
$executionTrigger.DaysOfWeek = 2
$executionTrigger.StartBoundary = $startTime
$taskAction = $taskDefinition.Actions.Create(0)
$taskAction.Path = "powershell.exe"
$taskAction.Arguments = "-Command `"&'$installpath\New-ExchangeReport.ps1' -installpath '$installpath'`""
$job = $rootFolder.RegisterTaskDefinition("Exchange-Reporter (www.FrankysWeb.de)" , $taskDefinition, 6, $username, $password, 1)
}
if ($reportinterval -notmatch 1 -and $reportinterval -notmatch 7)
{
write-host ""
write-host " Reportinterval (settings.ini) is not 1 (Daily) or" -foregroundcolor yellow
write-host " 7 (weekly), Could not create task, please do it manualy" -foregroundcolor yellow
write-host ""
}
if ($job)
{
write-host ""
write-host " Task created!" -foregroundcolor green
write-host ""
}
}
if ($aktion -match 2)
{
$job = invoke-command {SCHTASKS /Delete /TN "Exchange-Reporter (www.FrankysWeb.de)" /F} -ea 0 | out-null
write-host ""
write-host " Task deleted!" -foregroundcolor green
write-host ""
}