forked from droolio/snapraid-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapraid-helper.ps1
1106 lines (1032 loc) · 39.7 KB
/
snapraid-helper.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################
# Snapraid Sync Helper Script #
###############################
# this is a helper script that keeps snapraid parity info in sync with
# your data. Here's how it works:
# 1) it first calls diff to figure out if the parity info is out of sync
# 2) if there are changed files (i.e. new, changed, moved or removed),
# it then checks how many files were removed.
# 3) if the deleted files exceed X (configurable), it triggers an
# alert email and stops. (in case of accidental deletions)
# 4) otherwise, it will call sync.
# 5) when sync finishes, it sends an email with the output to user.
#
# $Author: therealjmc
# $Version: 3.3 (2016/06/16)
#
# Originally inspired by bash script written by sidney for linux/bash
# Based on the powershell script written by lrissman at gmail dot com
#
#######################################################################
###################### CHANGELOG ######################################
#######################################################################
#
# Version 3.3 (2016/06/16)
# Cleanup of example ini file (Thanks @ Marco)
#
# Version 3.2 (2015/05/16)
# Fixed the SnapRAID 8.1 diff exit code 2 if a change is needed
#
# Version 3.1 (2015/02/09)
# Fixed miss-formated output from status if shorten logfile enabled
#
# Version 3.0 (2015/02/06)
# Added a switch to shorten to logfile to just 1 line per percentage
#
# Version 2.9 (2015/01/27)
# Fixed a small cosmetic bug in the logrotation
#
# Version 2.8 (2014/12/03)
# Added SnapRAIDStatusAfterScrub to get a snapraid status after scrubbing
# Fixed a bug where snapraid-output of diff would be attached twice in logfile
#
# Version 2.7 (2014/08/18)
# Fixed writing to eventlog if email is disabled
#
# Version 2.6 (2014/04/19)
# Added a way to influence the percentage of a default scrub run with optional -scrubpercent option
#
# Version 2.5.2 (2014/04/06)
# Looks like a small encoding bug in the script. Should fix "A positional parameter cannot be found that accepts argument[...]" error
#
# Version 2.5.1 (2014/04/06)
# Added some more Debug Output to find a user reported Error
#
# Version 2.5 (2014/04/05)
# Added EnableDebugOutput Variable, if set to 1 all variables will be printed before snapraid starts
#
# Version 2.4 (2014/04/05)
# Fixed a wrong info about the location of the output files in the ini file
#
# Version 2.3 (2014/04/03)
# Added syncandfix option
#
# Version 2.2 (2014/03/24)
# Fixed a small cosmetic bug regarding Eventlog on the first script run
#
# Version 2.1 (2014/03/14)
# Added SnapRAIDConfig to ini file and included it as passing arguments to snapraid (fixes bug from Task sheduler when working directory was not set to snapraid dir)
#
# Version 2.0 (2014/03/13)
# Release on codeplex
# Fix a little bug in the condition for running pre-process only if needed when SkipParityFilesAtStart=1
#
#######################################################################
###################### END CHANGELOG ##################################
#######################################################################
#
# Modification by therealjmc:
# - Various fixes (for example the LastExiStCode and various "=" instead of -eq)
# - Added parameter to script to pass other commands (for example scrub)
# - Added Eventlog logging
# - Added output as attachment (including zip if above certain size)
# - Max Attachment Size is configurable
# - Added check to prevent double execution of script and/or snapraid
# - Making Logfile rotation a config variable (How many zip files should be stored)
# - Fixed messed up Umlauts with .Net File reading method
# - Added possibility to send emails without auth
# - Filename for ini now has to be the same as the script (without the .ps1 of course) (instead of fixed name)
# - Fixed the Services Section (was Process in .ini instead of Service) - UNTESTED!
# - Fixed the counting for the diff (especially with Snapraid 5.X+ since update and resize are different now)
# - Fixed double printing of snapraid diff output in email
# - Include the extended SnapRAID Log (5.X+ prints detailed error infos there) in the email if there is an snapraid error
# - Added Pre/Post Process (Pre will be called before the parity file test and Post will be called before E-Mail will be sent)
# - Added SkipParityFilesAtStart - if set to 1 parity file checking will be skipped unless diff finds a diference and runs Pre Process only if needed
# - Post Process/Service start will only run if Pre Process/Service stop has run
# - If argument passed is "syncandcheck" (without the "") there will be a sync (if needed) before a check is called
# - If argument passed is "syncandscrub" (without the "") there will be a sync (if needed) before a scrub is called (scrub without any parameters, snapraid default)
# - If argument passed is "syncandfullscrub" (without the "") there will be a sync (if needed) before a full scrub is called (-p 100 -o 0 as parameters)
# - If argument passed is "syncandfix" (without the "") there will be a sync (if needed) before a fix option (without any parameters) is done. Usefull for fixable errors in parity i.e.
# - If -scrubpercent is added after the argument you can influence the percentage snapraid scrubs
# - Added optional snapraid status output after scrubbing
# - Added option to shorten the logfile to just 1 line per percent
# - Various other fixes/enhancements
#
# NOTE TO USERS WITH SPECIAL CHARACTERS IN FILE/FOLDER NAMES:
# I had a problem with German Umlauts not beeing displayed correct. Enable UTF8Console in the .ini
# You HAVE to Change the Powershell Console Font to something like Lucida Console
# Note: Windows has a bug saving Lucida with Fontsize 12 as default. Select 10 or 14 - this works
#
#######################################################################
#Enable to pass the check/scrub command to SnapRAID as a parameter for the Powershellscript
#Has to be the first non-comment and non-blank line, otherwise param won't work.
Param([string]$Argument1='sync',[int]$ScrubPercent=999)
$Argument1 = $Argument1.ToLower()
<#
Note: To run a powershell script you must perform the following:
Only Once:
1) Download the PowerShellExecutionPolicy.adm from http://go.microsoft.com/fwlink/?LinkId=131786.
2) Install it
3) open gpedit.msc
4) Under computer configuration, right-click Administrative Templates and then click Add/Remove Templates
5) Add PowerShellExecutionPolicy.adm from %programfiles%\Microsoft Group policy
6) Open Administrative Templates\Classic Administrative Templates\Windows Components\Windows PowerShell
7) Enable the property and allow unsigned scripts to be run
each time:
1) open the power shell prompt
2) from the directory of video files, run the script
#>
##########################################
############# NOTES ######################
##########################################
<#
- This script depends upon the powershell community extensions from http://pscx.codeplex.com/
- Or direct sownload: http://pscx.codeplex.com/downloads/get/523236
- Snapraid's output is unix formatted (CRLF vs CR) so -delim "`0" is required to have each line on newline when using Get-Content
- Service Start and Stop requires the script to run with Elevated Rights
- snapraid-helper.ini file is required in the same dir as the .ps1
#>
##########################################
############# END NOTES ##################
##########################################
##########################################
########## INCLUSDES #####################
##########################################
$env:PSModulePath=$env:PSModulePath+";C:\Program Files (x86)\PowerShell Community Extensions\Pscx3"
##########################################
########## END INCLUSDES #################
##########################################
$Scriptname = $MyInvocation.MyCommand.Name
#$Scriptrunning = get-wmiobject win32_process -filter "name='powershell.exe'AND CommandLine LIKE '%$Scriptname%'"
$Scriptrunning = get-wmiobject win32_process -filter "name='powershell.exe'AND CommandLine LIKE '%$Scriptname%' AND NOT Handle LIKE '$PID'"
$Snapraidrunning = get-wmiobject win32_process -filter "name='snapraid.exe'"
##############################################
############# VARIABLES ######################
##############################################
$global:PreProcessHasRun = 0
$global:ServicesStarted = 0
$global:ServicesStopped = 0
$global:Diffchanges = 99
$SomethingDone = 0
$HomePath = $MyInvocation.Line | Split-Path
$message = ""
$ConfigError = 0
##############################################
############# END VARIABLES ##################
##############################################
##############################################
############# FUNCTIONS ######################
##############################################
function Test-IsAdmin { #Borrowed from with some modifications: http://stackoverflow.com/questions/9999963/powershell-test-admin-rights-within-powershell-script
try {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )
} catch {
#throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_
return 0
}
return 1
}
Function Start-Pre-Process {
# If Process Management is enabled, then start Pre Process
if ($config["ProcessEnable"] -eq 1) {
# timestamp the job
$CurrentDate = Get-Date
$message = "Starting Pre-Process $CurrentDate"
WriteLogFile $message
$exe = $config["ProcessPre"]
& "$exe" | Out-Null
if (!($LastExitCode -eq "0")) {
$CurrentDate = Get-Date
$message = "ERROR: Pre-Process failed on $CurrentDate with exit code $LastExitCode"
WriteLogFile $message
Start-Post-Process
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error" $EmailBody
Stop-Transcript | out-null
exit 1
}
else {
$CurrentDate = Get-Date
$message = "Done Starting Pre-Process $CurrentDate"
WriteLogFile $message
$global:PreProcessHasRun = 1
}
}
}
Function Start-Post-Process {
# If Process Management is enabled, then start Post Process
if ($config["ProcessEnable"] -eq 1) {
if ($global:PreProcessHasRun -eq 1) {
# timestamp the job
$CurrentDate = Get-Date
$message = "Starting Post-Process $CurrentDate"
WriteLogFile $message
$exe = $config["ProcessPost"]
& "$exe" | Out-Null
if (!($LastExitCode -eq "0")) {
$CurrentDate = Get-Date
$message = "ERROR: Post-Process failed on $CurrentDate with exit code $LastExitCode"
WriteLogFile $message
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error" $EmailBody
Stop-Transcript | out-null
exit 1
}
else {
$CurrentDate = Get-Date
$message = "Done Starting Post-Process $CurrentDate"
WriteLogFile $message
}
}
}
}
# Build Email Function (used many times in script)
Function Send-Email ($fSubject,$fSuccess,$EmailBody){
#$fSubject -- passed subject line
#$fSuccess -- "success" = success email, "error" = error email, "error2" = error email script/snapraid running
$Body = ""
if ($fSuccess -eq "success") {
$EventlogID = 4711
}
if ($fSuccess -eq "error") {
$EventlogID = 4712
}
if ($fSuccess -eq "error2") {
$EventlogID = 4712
}
if ($fSuccess -ne "error2") {
if ($config["IncludeExtendedInfoZip"] -eq 1 ){
If (Test-Path $EmailBodyTmp) {
Rename-Item "$EmailBodyTmp" "$EmailBodyTxt"
}
if (Test-Path "$EmailBodyTxt") {
$file = Get-Item "$EmailBodyTxt"
if ($file.length -ge $config["LogFileMaxSizeZIP"]) {
Write-zip -Path "$EmailBodyTxt" -OutputPath "$EmailBodyZip" -level 9 -Quiet
}
else {
$EmailBodyZip = $EmailBodyTxt
}
}
}
}
if (($fSuccess -eq "success") -and ($config["EmailOnSuccess"] -eq 1) -and ($config["EmailEnable"] -eq 1)) {
if ($config["IncludeExtendedInfo"] -eq 1 ){
$Body = (Get-Content $EmailBody | Out-string)
}
if ($config["IncludeExtendedInfoZip"] -eq 1 ){
If (Test-Path $EmailBodyZip) {
If ((Get-Item $EmailBodyZip).length -le $config["MaxAttachSize"]){
If (Test-Path $EmailBodyZip) {
$att = new-object Net.Mail.Attachment($EmailBodyZip)
$MailMessage.Attachments.Add($att)
}
}
else {
Add-Content $EmailBody "LOG FILE TOO LARGE TO ATTACH"
}
}
$Body = (Get-Content $EmailBody | Out-string)
}
$MailMessage.Subject = $fSubject
$Mailmessage.Body = $Body
$smtpclient.Send($MailMessage)
if ($config["IncludeExtendedInfoZip"] -eq 1 ){
If (Test-Path $EmailBodyZip) {
$att.Dispose()
}
}
}
if (($fSuccess -eq "error") -and ($config["EmailOnError"] -eq 1) -and ($config["EmailEnable"] -eq 1)) {
if ($config["IncludeExtendedInfo"] -eq 1 ){
$Body = (Get-Content $EmailBody | Out-string)
}
if ($config["IncludeExtendedInfoZip"] -eq 1){
If (Test-Path $EmailBodyZip) {
If ((Get-Item $EmailBodyZip).length -le $config["MaxAttachSize"]){
If (Test-Path $EmailBodyZip) {
$att = new-object Net.Mail.Attachment($EmailBodyZip)
$MailMessage.Attachments.Add($att)
}
}
else {
Add-Content $EmailBody "LOG FILE TOO LARGE TO ATTACH"
}
}
$Body = (Get-Content $EmailBody | Out-string)
}
$MailMessage.Subject = $fSubject
$Mailmessage.Body = $Body
$smtpclient.Send($MailMessage)
if ($config["IncludeExtendedInfoZip"] -eq 1 ){
If (Test-Path $EmailBodyZip) {
$att.Dispose()
}
}
}
if (($fSuccess -eq "error2") -and ($config["EmailOnError"] -eq 1) -and ($config["EmailEnable"] -eq 1)) {
$MailMessage.Subject = $fSubject
$Mailmessage.Body = $fSubject
$smtpclient.Send($MailMessage)
}
if (!(Get-Eventlog -Source SnapRaid-Helper -LogName Application -ErrorAction SilentlyContinue)){
New-EventLog -Source SnapRaid-Helper -LogName Application
}
write-eventlog -logname Application -source SnapRaid-Helper -eventID $EventlogID -message $fSubject
}
Function Check-Content-Files {
foreach ($element in $config["SnapRAIDContentFiles"]) {
if (!(Test-Path $element)){
$message = "ERROR: Content file ($element) not found!"
Write-Host $message -ForegroundColor red -backgroundcolor yellow
Add-Content $EmailBody $message
Start-Post-Process
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error" $EmailBody
Stop-Transcript | out-null
exit 1
}
}
}
Function Check-Parity-Files {
foreach ($element in $config["SnapRAIDParityFiles"]) {
if (!(Test-Path $element)){
$message = "ERROR: Parity file ($element) not found!"
Write-Host $message -ForegroundColor red -backgroundcolor yellow
Add-Content $EmailBody $message
Start-Post-Process
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error" $EmailBody
Stop-Transcript | out-null
exit 1
}
}
}
Function WriteLogFile ($ftext){
Write-Host "----------------------------------------"
Write-Host $ftext
Write-Host "----------------------------------------"
Add-Content $EmailBody "----------------------------------------"
Add-Content $EmailBody $ftext
Add-Content $EmailBody "----------------------------------------"
}
Function WriteExtendedLogFile ($ftext){
Write-Host "----------------------------------------"
Write-Host $ftext
Write-Host "----------------------------------------"
Add-Content $EmailBody "----------------------------------------"
Add-Content $EmailBody $ftext
Add-Content $EmailBody "----------------------------------------"
if ($config["IncludeExtendedInfoZip"] -eq 1 ){
Add-Content $EmailBodyTmp "----------------------------------------"
Add-Content $EmailBodyTmp $ftext
Add-Content $EmailBodyTmp "----------------------------------------"
}
}
Function ServiceManagement ($startstop){
if ($startstop -eq "stop") {
# If Service Management is enabled, then take services offline
if ($config["ServiceEnable"] -eq 1 -and $global:ServicesStopped -ne 1) {
# timestamp the job
$CurrentDate = Get-Date
$message = "Stopping Services $CurrentDate"
WriteLogFile $message
foreach ($service in $ServiceList) {
$message = Stop-Service $service
WriteLogFile $message
}
# timestamp the job
$CurrentDate = Get-Date
$message = "Done Stopping Services $CurrentDate"
WriteLogFile $message
$global:ServicesStopped = 1
}
}
if ($startstop -eq "start") {
# If Service Management is enabled, then bring services back online
if ($config["ServiceEnable"] -eq 1 -and $global:ServicesStarted -ne 1 -and $global:ServicesStopped -eq 1) {
# timestamp the job
$CurrentDate = Get-Date
$message = "Starting Services $CurrentDate"
WriteLogFile $message
foreach ($service in $ServiceList) {
$message = Start-Service $service
WriteLogFile $message
}
# timestamp the job
$CurrentDate = Get-Date
$message = "Done Starting Services $CurrentDate"
WriteLogFile $message
$global:ServicesStarted = 1
}
}
}
Function RunSnapraid ($sargument){
$exe = $config["SnapRAIDPath"] + $config["SnapRAIDExe"]
$configfile = $config["SnapRAIDPath"] + $config["SnapRAIDConfig"]
if ($sargument -ne "fullscrub") {
if (($ScrubPercent -ne 999) -and ($sargument -eq "scrub")) {
& "$exe" -c $configfile $sargument -p $ScrubPercent -l $SnapRAIDLogfile 2>&1 3>&1 4>&1 | %{ "$_" } | tee-object -file $TmpOutput
}
else {
& "$exe" -c $configfile $sargument -l $SnapRAIDLogfile 2>&1 3>&1 4>&1 | %{ "$_" } | tee-object -file $TmpOutput
}
}
else {
$sargument = "scrub"
& "$exe" -c $configfile $sargument -p 100 -o 0 -l $SnapRAIDLogfile 2>&1 3>&1 4>&1 | %{ "$_" } | tee-object -file $TmpOutput
}
if ($config["IncludeExtendedInfoZip"] -eq 1 ){
$FileToAdd = $EmailBodyTmp
}
else {
$FileToAdd = $EmailBody
}
if (($config["ShortenLogFile"] -eq 1 ) -and ($sargument -ne "status" )){
$TmpOutputInRAM = Get-Content $TmpOutput -ReadCount 0
for ($i=0; $i -lt $TmpOutputInRAM.length; $i++)
{
if ($TmpOutputInRAM[$i] -match "[0-9]*[A-Z]")
{
if ($TmpOutputInRAM[$i+1] -match "[0-9]*[A-Z]")
{
$TmpOutputInRAM_First_Three = $TmpOutputInRAM[$i+1].substring(0,3)
if ($TmpOutputInRAM_First_Three.Substring(0,1) -match "[0-9]")
{
if ($TmpOutputInRAM[$i].startswith($TmpOutputInRAM_First_Three))
{
}
else
{
Add-Content $FileToAdd $TmpOutputInRAM[$i]
}
}
else
{
Add-Content $FileToAdd $TmpOutputInRAM[$i]
}
}
else
{
if ($TmpOutputInRAM[$i+2] -notmatch "Autosaving...")
{
Add-Content $FileToAdd $TmpOutputInRAM[$i]
}
}
}
}
}
else
{
#$TmpOutputInRAM = Get-Content $TmpOutput -readcount 100 -delim "`0"
# NOTE the above Get-Content command is VERY VERY VERY VERY slow, so I am using the .Net function below to get the output of the Snapraid command into a variable
# NOTE the .Net function breaks german Umlauts so I'm using this fast way with get-content and out-string - no real time difference to .Net function
$TmpOutputInRAM = (Get-Content $TmpOutput | Out-string)
foreach ($line in $TmpOutputInRAM){
Add-Content $FileToAdd $line
# since output is done with tee it isn't necessary to use write-host again
# Write-Host $line
}
}
if (!($LastExitCode -eq "0")) {
if (!(($LastExitCode -eq "2") -and ($sargument = "diff"))) {
# If enabled bring services back online
ServiceManagement "start"
$CurrentDate = Get-Date
$message = "ERROR: SnapRAID $sargument Job FAILED on $CurrentDate with exit code $LastExitCode"
WriteExtendedLogFile $message
$message2 = "Including detailed SnapRAID Log"
WriteExtendedLogFile $message2
$SnapRAIDLogfileInRAM = (Get-Content $SnapRAIDLogfile | Out-string)
if ($config["IncludeExtendedInfoZip"] -eq 1 ){
$FileToAdd = $EmailBodyTmp
}
else {
$FileToAdd = $EmailBody
}
foreach ($line in $SnapRAIDLogfileInRAM){
Add-Content $FileToAdd $line
Write-Host $line
}
Start-Post-Process
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error" $EmailBody
Stop-Transcript | out-null
exit 1
}
}
# Job was successful, move onto processing.
$CurrentDate = Get-Date
$message = "SnapRAID $sargument Job finished on $CurrentDate"
WriteExtendedLogFile $message
If ($sargument -eq "diff") {
DiffAnalyze
}
}
Function DiffAnalyze {
If ($global:Diffchanges -eq 99) {
$DEL_COUNT = Select-String $TMPOUTPUT -Pattern "^remove" | Measure-Object -Line
$ADD_COUNT = Select-String $TMPOUTPUT -Pattern "^add" | Measure-Object -Line
$MOVE_COUNT = Select-String $TMPOUTPUT -Pattern "^move" | Measure-Object -Line
$RESIZE_COUNT = Select-String $TMPOUTPUT -Pattern "^resize" | Measure-Object -Line
$UPDATE_COUNT = Select-String $TMPOUTPUT -Pattern "^update" | Measure-Object -Line
$DEL_COUNT = $DEL_COUNT.Lines
$ADD_COUNT = $ADD_COUNT.Lines
$MOVE_COUNT = $MOVE_COUNT.Lines
$UPDATE_COUNT = $UPDATE_COUNT.Lines + $RESIZE_COUNT.Lines
$message = "SUMMARY of changes - Added [$ADD_COUNT] - Deleted [$DEL_COUNT] - Moved [$MOVE_COUNT] - Updated [$UPDATE_COUNT]"
WriteExtendedLogFile $message
# check if files have changed
if ( $DEL_COUNT -gt 0 -or $ADD_COUNT -gt 0 -or $MOVE_COUNT -gt 0 -or $UPDATE_COUNT -gt 0 ) {
# YES, check if number of deleted files exceed DEL_THRESHOLD
if ( $DEL_COUNT -gt $config["SnapRAIDDelThreshold"] ) {
# YES, lets inform user and not proceed with the job just in case
$message = "WARNING: Number of deleted files ($DEL_COUNT) exceeded threshold (" + $config["SnapRAIDDelThreshold"] + "). NOT proceeding with job. Please run manually if this is not an error condition."
Write-Host $message
Add-Content $EmailBody $message
Start-Post-Process
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error" $EmailBody
Stop-Transcript | out-null
exit 1
}
else {
# NO, delete threshold not reached, lets run the job
$message = "Deleted files ($DEL_COUNT) did not exceed threshold (" + $config["SnapRAIDDelThreshold"] + "), proceeding with job."
Write-Host $message
Add-Content $EmailBody $message
$CurrentDate = Get-Date
$message = "$CurrentDate Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,U-$UPDATE_COUNT] and deleted files ($DEL_COUNT) is below threshold (" + $config["SnapRAIDDelThreshold"] + "). Running Command."
Write-Host $message
Add-Content $EmailBody $message
$global:Diffchanges = 1
}
}
else {
# NO, so lets log it and exit
$CurrentDate = Get-Date
$message = "$CurrentDate No change detected. Nothing to do"
WriteExtendedLogFile $message
$global:Diffchanges = 0
}
}
}
##############################################
############### END FUNCTIONS ################
##############################################
##############################################
###### Configuration Verification Start ######
##############################################
# Get variables from <scriptname>.ini
$Scriptname2=[System.IO.Path]::GetFileNameWithoutExtension("$Scriptname")
$ConfigFile="$HomePath\$Scriptname2.ini"
$config = @{}
Get-Content $ConfigFile | foreach {
if (($_.StartsWith(";")) -or (!($_))) {
#Non-variable or is Space
# write-host "Non-Variable: $_"
}
else {
$line = $_.Split("=")
#$config.($line[0]) = $line[1]
$config[$line[0]] = $line[1].TrimEnd()
# write-host Variable: $line[0] Content: $line[1]
}
}
##### Validate configuration variables are sane
#SnapRAID and LogFile Config
$SnapRAIDConfigs = "SnapRAIDDelThreshold","SnapRAIDPath","SnapRAIDExe","SnapRAIDContentFiles","SnapRAIDParityFiles","TmpOutputFile","LogFileName","LogFileMaxSize","LogFileZipCount","UTF8Console","SnapRAIDStatusAfterScrub"
foreach ($element in $SnapRAIDConfigs){
if (!($config[$element]) -or ($config[$element] -eq "")) {
write-host "$element is null, please add a value"
$ConfigError ++
}
}
if ($config["UTF8Console"] -eq 1){
chcp 65001
}
#Validate EmailBodyPath and if not specified, use ScriptPath
if (!($config["LogPath"]) -or ($config["LogPath"] -eq "") ) {
$config["LogPath"] = "$HomePath\"
}
if ( !(Test-Path $config["LogPath"] -pathType container) ) {
Write-host "ERROR: LogPath: "$config["LogPath"]" - Path Does not exist. Please fix $ConfigFile or create the path"
exit 1
}
else {
$LogPathTest = $config["LogPath"].EndsWith("\")
If (!($LogPathTest)) {
$config["LogPath"] = $config["LogPath"] += "\"
}
}
$LogFile=$config["LogPath"] + $config["LogFileName"]
#Email Configs
$EmailConfigs = "SubjectPrefix","EmailTo","EmailFrom","Body","SMTPHost","SMTPSSLEnable","SMTPAuthEnable","EmailBodyFile","EmailBodyFileZip","EmailEnable","SMTPPort","EmailOnSuccess","EmailOnError","IncludeExtendedInfo","IncludeExtendedInfoZip","LogFileMaxSizeZIP","MaxAttachSize","ShortenLogFile"
#If email is enabled, validate email configs are not null
if ($config["EmailEnable"] -eq 1){
foreach ($element in $EmailConfigs){
if (!($config[$element]) -or ($config[$element] -eq "")) {
write-host "$element is null, please add a value"
$ConfigError ++
}
}
if ($config["IncludeExtendedInfoZip"] -eq 1){
$config["IncludeExtendedInfo"] = 0
}
}
#Service/Process Configs
if ($config["ServiceEnable"] -eq 1){
if (!(Test-IsAdmin)) {
Write-Host "You need to run the script with elevated rights to start and stop services. Either run with Elevated Rights or change in $ConfigFile ProcessEnable=0"
exit 1
}
$ServiceConfigs = "ServiceName"
#If service handling is enabled, validate configs are not null
foreach ($element in $ServiceConfigs){
if (!($config[$element]) -or ($config[$element] -eq "")) {
write-host "$element is null, please add a value"
$ConfigError ++
}
}
$ServiceNum = 0
$ServiceList = $config["ServiceName"].Split(",").Replace('"',"")
foreach ($Service in $ServiceList) {
$ServiceNum ++
if (!(Get-Service $Service -ErrorAction SilentlyContinue))
{
"The Service $Service does not exist. Please remove or correct in ProcessName in $ConfigFile"
}
}
}
if ($config["ProcessEnable"] -eq 1){
#If process handling is enabled, validate configs are not null
$ProcessConfigs = "ProcessPre","ProcessPost"
foreach ($element in $ProcessConfigs){
if (!($config[$element]) -or ($config[$element] -eq "")) {
write-host "$element is null, please add a value"
$ConfigError ++
}
if (!(Test-Path $config[$element])){
wite-host "$config[$element] is not a valid path to execute!"
$ConfigError ++
}
}
}
#EventLog Configs
$EventLogConfigs = "EventLogSources","EventLogEntryType","EventLogDays","EventLogHaltOnDiskError"
if ($config["EventLogEnable"] -eq 1){
foreach ($element in $EventLogConfigs){
if (!($config[$element]) -or ($config[$element] -eq "")) {
write-host "$element is null, please add a value"
$ConfigError ++
}
}
$EventLogEntryTypeList = $config["EventLogEntryType"].Replace('"',"").Trim().Split(",")
$EventLogSourcesList = $config["EventLogSources"].Replace('"',"").Trim().Split(",")
}
#Report if there are errors and exit
if ($ConfigError -ge 1) {
write-host "Number of config errors: $ConfigError"
write-host "Please correct $ConfigFile and run again"
exit 1
}
#Validate EmailBodyPath and if not specified, use Windows Temp path
if (!($config["EmailBodyPath"]) -or ($config["EmailBodyPath"] -eq "") ) {
$config["EmailBodyPath"] = "$env:temp\"
}
if ( !(Test-Path $config["EmailBodyPath"] -pathType container) ) {
Write-host "ERROR: EmailBodyPath: "$config["EmailBodyPath"]" - Path Does not exist. Please fix $ConfigFile or create the path"
exit 1
}
#Validate TmpOutputPath and if not specified, use Windows Temp path
if (!($config["TmpOutputPath"]) -or ($config["TmpOutputPath"] -eq "")){
$config["TmpOutputPath"] = "$env:temp\"
}
if ( !(Test-Path $config["TmpOutputPath"] -pathType container) ) {
Write-host "ERROR: TmpOutputPath:" $config["TmpOutputPath"]" - Path Does not exist. Please fix $ConfigFile or create the path"
exit 1
}
##############################################
###### Configuration Verification End ########
##############################################
#Initalize Email
if ($config["EmailEnable"] -eq 1) {
$SMTPClient = New-Object Net.Mail.SmtpClient
$MailMessage= New-Object Net.Mail.Mailmessage
$MailMessage.IsBodyHtml = $false
$SMTPClient.Host = $config["SMTPHost"]
$SMTPClient.Port = $config["SMTPPort"]
$MailMessage.From = $config["EmailFrom"]
$MailMessage.To.add($config["EmailTo"])
if ($config["SMTPSSLEnable"] -eq 1) {
$SMTPClient.EnableSsl = $true
[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
else {
$SMTPClient.EnableSsl = $false
}
if ($config["SMTPAuthEnable"] -eq 1) {
$SMTPClient.Credentials = new-Object System.Net.NetworkCredential($config["SMTPUID"],$config["SMTPPass"]);
}
}
$TmpOutput=$config["TmpOutputPath"] + $config["TmpOutputfile"]
$EmailBody=$config["EmailBodyPath"] + $config["EmailBodyfile"]
$EmailBodyTmp=$config["EmailBodyPath"] + $config["EmailBodyFileZip"] + ".out"
$EmailBodyTxt=$config["EmailBodyPath"] + $config["EmailBodyFileZip"] + ".txt"
$EmailBodyZip=$config["EmailBodyPath"] + $config["EmailBodyFileZip"] + ".zip"
$SnapRAIDLogfile=$config["TmpOutputPath"] + "snapRAIDerror.out"
#Ensure only one Snapraid process and only one instance of this script is running
#Note that the detection for running script only works if it is called with the script as a parameter
#for example powershell.exe snapraid-helper.ps1 - but not if the script is called like .\snapraid-helper.ps1
If ($Scriptrunning -match "Handle"){
$CurrentDate = Get-Date
$message = "ERROR: Another instance of the script is still running! $Argument1 can't run on $CurrentDate"
Write-Host "----------------------------------------"
Write-Host $message
Write-Host "----------------------------------------"
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error2"
exit 1
}
If ($Snapraidrunning -match "Handle"){
$CurrentDate = Get-Date
$message = "ERROR: Another instance of snapraid is still running! $Argument1 can't run on $CurrentDate"
Write-Host "----------------------------------------"
Write-Host $message
Write-Host "----------------------------------------"
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error2"
exit 1
}
#Start with some cleanup
if (Test-Path $TmpOutput){
Remove-Item $TmpOutput
}
if (Test-Path $EmailBody){
Remove-Item $EmailBody
}
if (Test-Path $EmailBodyTmp){
Remove-Item $EmailBodyTmp
}
if (Test-Path $EmailBodyTxt){
Remove-Item $EmailBodyTxt
}
if (Test-Path $EmailBodyZip){
Remove-Item $EmailBodyZip
}
if (Test-Path $SnapRAIDLogfile){
Remove-Item $SnapRAIDLogfile
}
if ($config["EnableDebugOutput"] -eq 1) {
foreach ($element in $Config){
echo $element
echo "TmpOutput = $TmpOutput"
echo "EmailBody = $EmailBody"
echo "EmailBodyTmp = $EmailBodyTmp"
echo "EmailBodyTxt = $EmailBodyTxt"
echo "EmailBodyZip = $EmailBodyZip"
echo "SnapRAIDLogfile = $SnapRAIDLogfile"
}
}
#Log Management Section
if (Test-Path "$LogFile") {
$file = Get-Item "$LogFile"
If ($file.length -ge $config["LogFileMaxSize"]){
if ($config["LogFileZipCount"] -ge 1) {
$i = $config["LogFileZipCount"]
if (Test-Path "$LogFile.$i.zip") {
Remove-Item "$LogFile.$i.zip"
}
while ($i -gt 1) {
$j = $i - 1
if (Test-Path "$LogFile.$j.zip") {
Rename-Item "$LogFile.$j.zip" "$LogFile.$i.zip"
}
$i = $i-1
}
Write-zip "$LogFile" -level 9
Rename-Item "$LogFile.zip" "$LogFile.1.zip"
}
Remove-Item "$LogFile"
New-Item "$LogFile" -type file
}
}
# Start Transcript logging
# redirect all stdout to log file (leave stderr alone thou)
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path $LogFile -append
#Check Eventlog for Errors
$CurrentDate = Get-Date
$message = "Checking for Disk issues in Eventlog at $CurrentDate"
WriteLogFile $message
$EventLogOutput = get-eventlog -logname system -entrytype $EventLogEntryTypeList -Source $EventLogSourcesList -After (Get-Date).AddDays($config["EventLogdays"])
Write-Host "TimeGenerated,EntryType,Source,Message"
foreach ($event in $EventLogOutput) {
$EventLogCount = $EventLogcount + 1
$TimeGenerated = $event.TimeGenerated
$EntryType = $event.EntryType
$Source = $event.Source
$EventMessage = $event.Message
Write-Host "$TimeGenerated,$EntryType,$Source,$EventMessage"
Add-Content $EmailBody "$TimeGenerated,$EntryType,$Source,$EventMessage"
}
if (($EventLogCount -ge 1) -and ($config["EventLogHaltOnDiskError"] -eq 1)) {
$message = "WARN: Found disk Errors/Warnings in EventLogs. Aborting sync based on HaltOnDiskError"
Write-Host $message -ForegroundColor red -backgroundcolor yellow
Add-Content $EmailBody $message
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "error" $EmailBody
Stop-Transcript | out-null
exit 1
}
#sanity check first to make sure we can access the content and parity files
$config["SnapRAIDContentFiles"] = $config["SnapRAIDContentFiles"].split(",")
$config["SnapRAIDParityFiles"] = $config["SnapRAIDParityFiles"].split(",")
Check-Content-Files
if (!($config["SkipParityFilesAtStart"]) -or ($config["SkipParityFilesAtStart"] -ne 1) ) {
Start-Pre-Process
Check-Parity-Files
}
# timestamp the job
$CurrentDate = Get-Date
$message = "SnapRAID $argument1 Job started on $CurrentDate"
WriteExtendedLogFile $message
If ($Argument1 -eq "syncandcheck" -and $SomethingDone -ne 1) {
$argument = "diff"
RunSnapraid $argument
If ($global:Diffchanges -eq 1){
if ($config["SkipParityFilesAtStart"] -eq 1 -and $global:PreProcessHasRun -eq 0){
Start-Pre-Process
Check-Parity-Files
}
# If enabled take services offline
ServiceManagement "stop"
$argument = "sync"
RunSnapraid $argument
}
if ($config["SkipParityFilesAtStart"] -eq 1 -and $global:PreProcessHasRun -eq 0){
Start-Pre-Process
Check-Parity-Files
}
# If enabled take services offline
ServiceManagement "stop"
$argument = "check"
RunSnapraid $argument
# If enabled bring services back online
ServiceManagement "start"
$CurrentDate = Get-Date
$message = "SUCCESS: SnapRAID SYNC and CHECK Job finished on $CurrentDate"
WriteExtendedLogFile $message
Start-Post-Process
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "success" $EmailBody
$SomethingDone = 1
}
ElseIf ($Argument1 -eq "syncandscrub" -and $SomethingDone -ne 1) {
$argument = "diff"
RunSnapraid $argument
If ($global:Diffchanges -eq 1){
if ($config["SkipParityFilesAtStart"] -eq 1 -and $global:PreProcessHasRun -eq 0){
Start-Pre-Process
Check-Parity-Files
}
# If enabled take services offline
ServiceManagement "stop"
$argument = "sync"
RunSnapraid $argument
}
if ($config["SkipParityFilesAtStart"] -eq 1 -and $global:PreProcessHasRun -eq 0){
Start-Pre-Process
Check-Parity-Files
}
# If enabled take services offline
ServiceManagement "stop"
$argument = "scrub"
RunSnapraid $argument
if ($config["SnapRAIDStatusAfterScrub"] -eq 1) {
$argument = "status"
RunSnapraid $argument
}
# If enabled bring services back online
ServiceManagement "start"
$CurrentDate = Get-Date
$message = "SUCCESS: SnapRAID SYNC and SCRUB Job finished on $CurrentDate"
WriteExtendedLogFile $message
Start-Post-Process
$subject = $config["SubjectPrefix"]+" "+$message
Send-Email $subject "success" $EmailBody
$SomethingDone = 1
}
ElseIf ($Argument1 -eq "syncandfix" -and $SomethingDone -ne 1) {
$argument = "diff"
RunSnapraid $argument
If ($global:Diffchanges -eq 1){
if ($config["SkipParityFilesAtStart"] -eq 1 -and $global:PreProcessHasRun -eq 0){
Start-Pre-Process
Check-Parity-Files
}
# If enabled take services offline
ServiceManagement "stop"
$argument = "sync"
RunSnapraid $argument
}
if ($config["SkipParityFilesAtStart"] -eq 1 -and $global:PreProcessHasRun -eq 0){