-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAE.psm1
More file actions
2858 lines (2542 loc) · 118 KB
/
AE.psm1
File metadata and controls
2858 lines (2542 loc) · 118 KB
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
#==========================================================================
# List of all functions in this script:
# -------------------------------------
# -Windows functions
# -Files and folders
# -AE_DeleteFiles -> Delete files in a directory
# -AE_CopyArchive -> Copy a file or multiple files
# -AE_NewDirectory -> Create a directory
# -AE_DeleteDirectory -> Delete a directory
# -AE_MoveDirectory -> Move a directory
# -AE_DeleteFile -> Delete a file
# -AE_RenameItem -> Rename a file or registry value
# -AE_UnzipArchive -> Expand a zip file
# -AE_AddStream -> Add content to a file
# -AE_GetStream -> Get content from file
# -AE_MoveArchive -> Move a file or multiple files
# -AE_GetFileName -> Get a name of any file
# -AE_ISOSetupInstall -> Execute an EXE file located on a ISO image, with or without arguments.
# -AE_CopyWithProgress -> Copy files and folders showing a progress
# -Installations and executables
# -AE_RunProcess -> Start a process
# -AE_RunProcessNoWait -> Start a process without wait
# -AE_MSIInstall -> Install msi software
# -AE_SetupInstall -> Install exe software
# -Logging
# -AE_WriteLog -> Write log file
# -Registry
# -AE_NewRegKey -> Create a registry key
# -AE_DeleteRegKey -> Delete a registry key
# -AE_DeleteRegKeyValue -> Delete a registry value (any data type)
# -AE_ImportRegFile -> Import a registry (*.reg) file into the registry
# -AE_RenameRegKey -> Rename a registry key
# -AE_RenameRegKeyValue -> Rename a registry value
# -AE_SetRegKeyValue -> Create or modify a registry value (any data type)
# -Services
# -AE_ModifyStartupService -> Change the startup type of a service (Boot, System, Automatic, Manual and Disabled)
# -AE_StopService -> Stop a service (including depend services)
# -AE_StartingService -> Start service (including depend services)
# -System
# -AE_WipePrefetch -> Clear prefetch folder
# -AE_StartStopProcess -> Start and stop a process
# -AE_ResolvePath -> Resolves a given path (Path or LiteralPath) and checks if it exists.
# -AE_ChangeLocation -> Change the current working directory
# -AE_GetLocation -> Get the current working directory
# -AE_CheckPath -> Check if a path exists
# -AE_InvokeCommand -> Invoke an external command
#
# -Downloads
# -AE_ResolveUri -> Resolves a URI and retrieves information such as file size, last modified date, and filename.
# -AE_NovaDownloader -> Downloading a file with progress bar and some features
#
$global:ErrorActionPreference = "Stop"
if ($verbose) {
$global:VerbosePreference = "Continue"
}
if (([string]::IsNullOrEmpty($LogFile)) -Or ($LogFile.Length -eq 0)) {
$LogDir = "$(Get-Location)\Logs"
$LogFileName = "DefaultLogFile_$(Get-Date -format dd-MM-yyyy)_$((Get-Date -format HH:mm:ss).Replace(":","-")).log"
$LogFile = Join-path $LogDir $LogFileName
}
# FUNCTION AE_DeleteFiles
# Description: delete all files and subfolders in one specific directory (e.g. C:\Windows\Temp). Do not delete the main folder itself.
#==========================================================================
Function AE_DeleteFiles {
<#
.SYNOPSIS
Delete all files and subfolders in one specific directory, but do not delete the main folder itself
.DESCRIPTION
Delete all files and subfolders in one specific directory, but do not delete the main folder itself
.PARAMETER Directory
This parameter contains the full path to the directory that needs to cleaned (for example 'C:\Temp')
.EXAMPLE
AE_DeleteFiles -Directory "C:\Temp"
Deletes all files and subfolders in the directory 'C:\Temp'
#>
[CmdletBinding()]
Param(
[Alias("D")]
[Parameter(Mandatory = $true, Position = 0)][String]$Directory
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
AE_WriteLog "INFO" "Cleanup directory $Directory" $LogFile
if (Test-Path $Directory ) {
try {
Remove-Item "$Directory\*.*" -Force -Recurse | Out-Null
Remove-Item "$Directory\*" -Force -Recurse | Out-Null
AE_WriteLog "SUCCESS" "Successfully deleted all files and subfolders in the directory $Directory" $LogFile
}
catch {
AE_WriteLog "ERROR" "An error occurred trying to delete files and subfolders in the directory $Directory (exit code: $($Error[0]))!" $LogFile
Exit 1
}
}
else {
AE_WriteLog "INFO" "The directory $Directory does not exist. Nothing to do" $LogFile
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_UnzipArchive
#==========================================================================
Function AE_UnzipArchive {
<#
.SYNOPSIS
Expand an archive file (ZIP).
.DESCRIPTION
This function extracts the contents of a compressed archive (ZIP) to a specified destination folder.
.PARAMETER File
This parameter contains the full path to the ZIP file that you want to extract.
.PARAMETER DestinationPath
This parameter contains the full path to the directory where the contents will be extracted.
.PARAMETER Pattern
This parameter specifies whether existing files should be overwritten. The default is 'True'.
.EXAMPLE
AE_UnzipArchive -File "C:\Temp\MyArchive.zip" -DestinationPath "C:\Temp\ExtractedFiles" -Pattern $true
Extracts the ZIP file 'MyArchive.zip' to the folder 'ExtractedFiles', overwriting existing files.
#>
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)][String]$File,
[Alias("D")]
[Parameter(Mandatory = $false, Position = 1)][AllowEmptyString()][String]$DestinationPath,
[Alias("P")]
[switch]$Pattern
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
# Logging source and destination paths
AE_WriteLog "INFO" "Source file: $File" $LogFile
AE_WriteLog "INFO" "Destination path: $DestinationPath" $LogFile
# Check if the source file exists
if (!(Test-Path $File)) {
AE_WriteLog "ERROR" "The file '$File' does not exist!" $LogFile
Exit 1
}
# Check if the destination directory exists, if not, create it
if (!(Test-Path $DestinationPath)) {
AE_WriteLog "INFO" "The destination directory does not exist. Creating directory: $DestinationPath" $LogFile
New-Item -ItemType Directory -Path $DestinationPath | Out-Null
}
# Expand the archive
try {
if ($Pattern) {
AE_WriteLog "INFO" "Expanding archive '$File' to '$DestinationPath' with overwrite enabled" $LogFile
Expand-Archive -Path $File -DestinationPath $DestinationPath -Force
}
else {
AE_WriteLog "INFO" "Expanding archive '$File' to '$DestinationPath' without overwriting" $LogFile
Expand-Archive -Path $File -DestinationPath $DestinationPath
}
AE_WriteLog "SUCCESS" "The archive '$File' was successfully extracted to '$DestinationPath'" $LogFile
}
catch {
AE_WriteLog "ERROR" "An error occurred while extracting the archive '$File' to '$DestinationPath': $_" $LogFile
Exit 1
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_AddStream
#==========================================================================
Function AE_AddStream {
<#
.SYNOPSIS
Adds content to a file.
.DESCRIPTION
This function adds specified content to a file. If the file does not exist, it will be created automatically.
.PARAMETER FilePath
Path to the file where content will be added.
.PARAMETER Content
Content to be added to the file.
.EXAMPLE
AE_AddStream -FilePath "C:\Logs\example.txt" -Content "This is a new line of text."
Adds the line "This is a new line of text." to the file example.txt at the specified path.
#>
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)][String]$FilePath,
[Alias("C")]
[Parameter(Mandatory = $true, Position = 1)][String]$Content
)
begin {
# Log the start of the function
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
# Log the start of the content-adding process
AE_WriteLog "INFO" "Adding content to the file '$FilePath'" $LogFile
try {
# Add the content to the specified file
Add-Content -Path $FilePath -Value $Content
AE_WriteLog "INFO" "Content successfully added to '$FilePath'" $LogFile
}
catch {
# Log any errors encountered during the content addition
AE_WriteLog "ERROR" "Error adding content to '$FilePath' (error: $($_.Exception.Message))" $LogFile
Exit 1
}
}
end {
# Log the end of the function
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_GetStream
#==========================================================================
Function AE_GetStream {
<#
.SYNOPSIS
Retrieves content from a file.
.DESCRIPTION
This function retrieves the content of a specified file. If the file does not exist, it will log a warning.
.PARAMETER FilePath
Path to the file from which content will be retrieved.
.EXAMPLE
AE_GetStream -FilePath "C:\Logs\example.txt"
Retrieves and returns the content of the file example.txt at the specified path.
#>
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)][String]$FilePath
)
begin {
# Log the start of the function
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
# Log the attempt to read content from the file
AE_WriteLog "INFO" "Retrieving content from the file '$FilePath'" $LogFile
try {
# Check if the file exists before reading
if (Test-Path $FilePath) {
# Get the content from the specified file
$Content = Get-Content -Path $FilePath
AE_WriteLog "INFO" "Content successfully retrieved from '$FilePath'" $LogFile
return $Content
}
else {
# Log a warning if the file does not exist
AE_WriteLog "WARNING" "File '$FilePath' does not exist." $LogFile
return $null
}
}
catch {
# Log any errors encountered during the content retrieval
AE_WriteLog "ERROR" "Error retrieving content from '$FilePath' (error: $($_.Exception.Message))" $LogFile
Exit 1
}
}
end {
# Log the end of the function
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_MoveArchive
#==========================================================================
Function AE_MoveArchive {
<#
.SYNOPSIS
Move one or more files
.DESCRIPTION
Move one or more files to a specified destination.
.PARAMETER SourceFiles
Specifies the source file(s) or folder(s) to move. Wildcards can be used, and UNC paths are supported.
.PARAMETER Destination
Specifies the destination path where the file(s) should be moved. The path may include a file name to rename the file during the move operation.
.EXAMPLE
AE_MoveArchive -SourceFiles "C:\Temp\MyFile.txt" -Destination "C:\Temp2"
Moves 'C:\Temp\MyFile.txt' to 'C:\Temp2'.
.EXAMPLE
AE_MoveArchive -SourceFiles "C:\Temp\MyFile.txt" -Destination "C:\Temp2\MyRenamedFile.txt"
Moves 'C:\Temp\MyFile.txt' to 'C:\Temp2' and renames it to 'MyRenamedFile.txt'.
.EXAMPLE
AE_MoveArchive -SourceFiles "C:\Temp\*.txt" -Destination "C:\Temp2"
Moves all '.txt' files from 'C:\Temp' to 'C:\Temp2'.
#>
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)][String]$Files,
[Alias("D")]
[Parameter(Mandatory = $true, Position = 1)][String]$Destination
)
begin {
# Log the start of the function
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
# Log the source and destination details
AE_WriteLog "INFO" "Move the source file(s) '$Files' to '$Destination'" $LogFile
# Determine the destination directory, creating it if needed
if ($Destination.Contains(".")) {
# If $Destination contains a dot, treat it as a path with a filename
$TempFolder = Split-Path -Path $Destination
}
else {
# Otherwise, assume it's a directory path
$TempFolder = $Destination
}
# Check if destination path exists; create it if it does not
AE_WriteLog "INFO" "Check if the destination path '$TempFolder' exists. If not, create it" $LogFile
if (Test-Path $TempFolder) {
AE_WriteLog "INFO" "The destination path '$TempFolder' already exists. Nothing to do" $LogFile
}
else {
AE_WriteLog "INFO" "The destination path '$TempFolder' does not exist" $LogFile
AE_NewDirectory -Directory $TempFolder
}
# Move the source files
AE_WriteLog "INFO" "Start moving the source file(s) '$Files' to '$Destination'" $LogFile
try {
Move-Item -Path $Files -Destination $Destination -Force
AE_WriteLog "SUCCESS" "Successfully moved the source file(s) '$Files' to '$Destination'" $LogFile
}
catch {
# Log the error if the move fails
AE_WriteLog "ERROR" "An error occurred while moving the source file(s) '$Files' to '$Destination'. Error: $($_.Exception.Message)" $LogFile
Exit 1
}
}
end {
# Log the end of the function
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_CopyArchive
#==========================================================================
Function AE_CopyArchive {
<#
.SYNOPSIS
Copy one or more files
.DESCRIPTION
Copy one or more files
.PARAMETER SourceFiles
This parameter can contain multiple file and folder combinations including wildcards. UNC paths can be used as well. Please see the examples for more information.
To see the examples, please enter the following PowerShell command: Get-Help AE_CopyArchive -examples
.PARAMETER Destination
This parameter contains the destination path (for example 'C:\Temp2' or 'C:\MyPath\MyApp'). This path may also include a file name.
This situation occurs when a single file is copied to another directory and renamed in the process (for example '$Destination = C:\Temp2\MyNewFile.txt').
UNC paths can be used as well. The destination directory is automatically created if it does not exist (in this case the function 'AE_NewDirectory' is called).
This works both with local and network (UNC) directories. In case the variable $Destination contains a path and a file name, the parent folder is
automatically extracted, checked and created if needed.
Please see the examples for more information.To see the examples, please enter the following PowerShell command: Get-Help AE_CopyArchive -examples
.EXAMPLE
AE_CopyArchive -Files "C:\Temp\MyFile.txt" -Destination "C:\Temp2"
Copies the file 'C:\Temp\MyFile.txt' to the directory 'C:\Temp2'
.EXAMPLE
AE_CopyArchive -Files "C:\Temp\MyFile.txt" -Destination "C:\Temp2\MyNewFileName.txt"
Copies the file 'C:\Temp\MyFile.txt' to the directory 'C:\Temp2' and renames the file to 'MyNewFileName.txt'
.EXAMPLE
AE_CopyArchive -Files "C:\Temp\*.txt" -Destination "C:\Temp2"
Copies all files with the file extension '*.txt' in the directory 'C:\Temp' to the destination directory 'C:\Temp2'
.EXAMPLE
AE_CopyArchive -Files "C:\Temp\*.*" -Destination "C:\Temp2"
Copies all files within the root directory 'C:\Temp' to the destination directory 'C:\Temp2'. Subfolders (including files within these subfolders) are NOT copied.
.EXAMPLE
AE_CopyArchive -Files "C:\Temp\*" -Destination "C:\Temp2"
Copies all files in the directory 'C:\Temp' to the destination directory 'C:\Temp2'. Subfolders as well as files within these subfolders are also copied.
.EXAMPLE
AE_CopyArchive -Files "C:\Temp\*.txt" -Destination "\\localhost\Temp2"
Copies all files with the file extension '*.txt' in the directory 'C:\Temp' to the destination directory '\\localhost\Temp2'. The directory in this example is a network directory (UNC path).
#>
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)][String]$Files,
[Alias("D")]
[Parameter(Mandatory = $true, Position = 1)][String]$Destination
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
AE_WriteLog "INFO" "Copy the source file(s) '$Files' to '$Destination'" $LogFile
# Retrieve the parent folder of the destination path
if ( $Destination.Contains(".") ) {
# In case the variable $Destination contains a dot ("."), return the parent folder of the path
$TempFolder = split-path -path $Destination
}
else {
$TempFolder = $Destination
}
# Check if the destination path exists. If not, create it.
AE_WriteLog "INFO" "Check if the destination path '$TempFolder' exists. If not, create it" $LogFile
if ( Test-Path $TempFolder) {
AE_WriteLog "INFO" "The destination path '$TempFolder' already exists. Nothing to do" $LogFile
}
else {
AE_WriteLog "INFO" "The destination path '$TempFolder' does not exist" $LogFile
AE_NewDirectory -Directory $TempFolder
}
# Copy the source files
AE_WriteLog "INFO" "Start copying the source file(s) '$Files' to '$Destination'" $LogFile
try {
Copy-Item $Files -Destination $Destination -Force -Recurse
AE_WriteLog "SUCCESS" "Successfully copied the source files(s) '$Files' to '$Destination'" $LogFile
}
catch {
AE_WriteLog "ERROR" "An error occurred trying to copy the source files(s) '$Files' to '$Destination'" $LogFile
Exit 1
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_NewDirectory
#==========================================================================
Function AE_NewDirectory {
<#
.SYNOPSIS
Create a new directory
.DESCRIPTION
Create a new directory
.PARAMETER Directory
This parameter contains the name of the new directory including the full path (for example C:\Temp\MyNewFolder).
.EXAMPLE
AE_NewDirectory -Directory "C:\Temp\MyNewFolder"
Creates the new directory "C:\Temp\MyNewFolder"
#>
[CmdletBinding()]
Param(
[Alias("D")]
[Parameter(Mandatory = $true, Position = 0)][String]$Directory
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
AE_WriteLog "INFO" "Create directory $Directory" $LogFile
if ( Test-Path $Directory ) {
AE_WriteLog "INFO" "The directory $Directory already exists. Nothing to do" $LogFile
}
else {
try {
New-Item -ItemType Directory -Path $Directory -Force | Out-Null
AE_WriteLog "SUCCESS" "Successfully created the directory $Directory" $LogFile
}
catch {
AE_WriteLog "ERROR" "An error occurred trying to create the directory $Directory (exit code: $($Error[0]))!" $LogFile
Exit 1
}
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_DeleteDirectory
# Description: delete the entire directory
#==========================================================================
Function AE_DeleteDirectory {
<#
.SYNOPSIS
Delete a directory
.DESCRIPTION
Delete a directory
.PARAMETER Directory
This parameter contains the full path to the directory which needs to be deleted (for example C:\Temp\MyFolder).
.EXAMPLE
AE_DeleteDirectory -Directory "C:\Temp\MyFolder"
Deletes the directory "C:\Temp\MyFolder"
#>
[CmdletBinding()]
Param(
[Alias("D")]
[Parameter(Mandatory = $true, Position = 0)][String]$Directory
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
AE_WriteLog "INFO" "Delete directory $Directory" $LogFile
if ( Test-Path $Directory ) {
try {
Remove-Item $Directory -Force -Recurse | Out-Null
AE_WriteLog "SUCCESS" "Successfully deleted the directory $Directory" $LogFile
}
catch {
AE_WriteLog "ERROR" "An error occurred trying to delete the directory $Directory (exit code: $($Error[0]))!" $LogFile
Exit 1
}
}
else {
AE_WriteLog "INFO" "The directory $Directory does not exist. Nothing to do" $LogFile
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_DeleteFile
# Description: delete one specific file
#==========================================================================
Function AE_DeleteFile {
<#
.SYNOPSIS
Delete a file
.DESCRIPTION
Delete a file
.PARAMETER File
This parameter contains the full path to the file (including the file name and file extension) that needs to be deleted (for example C:\Temp\MyFile.txt).
.EXAMPLE
AE_DeleteFile -File "C:\Temp\MyFile.txt"
Deletes the file "C:\Temp\MyFile.txt"
.EXAMPLE
AE_DeleteFile -File "C:\Temp\*.txt"
Deletes all files in the directory "C:\Temp" that have the file extension *.txt. *.txt files stored within subfolders of 'C:\Temp' are NOT deleted
.EXAMPLE
AE_DeleteFile -File "C:\Temp\*.*"
Deletes all files in the directory "C:\Temp". This function does NOT remove any subfolders nor files within a subfolder (use the function 'AE_DeleteFiles' instead)
#>
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)][String]$File
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
AE_WriteLog "INFO" "Delete the file '$File'" $LogFile
if ( Test-Path $File ) {
try {
Remove-Item "$File" | Out-Null
AE_WriteLog "SUCCESS" "Successfully deleted the file '$File'" $LogFile
}
catch {
AE_WriteLog "ERROR" "An error occurred trying to delete the file '$File' (exit code: $($Error[0]))!" $LogFile
Exit 1
}
}
else {
AE_WriteLog "INFO" "The file '$File' does not exist. Nothing to do" $LogFile
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_MoveDirectory
#==========================================================================
Function AE_MoveDirectory {
<#
.SYNOPSIS
Move one or more directories
.DESCRIPTION
Move one or more directories to a specified destination.
.PARAMETER SourceDirectories
Specifies the source directory(s) to move. Wildcards can be used, and UNC paths are supported.
.PARAMETER Destination
Specifies the destination path where the directory(s) should be moved. The path may include a new directory name.
.EXAMPLE
AE_MoveDirectory -SourceDirectories "C:\Temp\MyFolder" -Destination "C:\Temp2"
Moves 'C:\Temp\MyFolder' to 'C:\Temp2'.
.EXAMPLE
AE_MoveDirectory -SourceDirectories "C:\Temp\MyFolder" -Destination "C:\Temp2\MyRenamedFolder"
Moves 'C:\Temp\MyFolder' to 'C:\Temp2' and renames it to 'MyRenamedFolder'.
.EXAMPLE
AE_MoveDirectory -SourceDirectories "C:\Temp\*" -Destination "C:\Temp2"
Moves all directories from 'C:\Temp' to 'C:\Temp2'.
#>
[CmdletBinding()]
Param(
[Alias("S")]
[Parameter(Mandatory = $true, Position = 0)][String]$SourceDirectories,
[Alias("D")]
[Parameter(Mandatory = $true, Position = 1)][String]$Destination
)
begin {
# Log the start of the function
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
# Log the source and destination details
AE_WriteLog "INFO" "Move the source directory(s) '$SourceDirectories' to '$Destination'" $LogFile
# Determine the destination directory
if ($Destination.Contains(".")) {
# If $Destination contains a dot, treat it as a path with a directory name
$TempFolder = Split-Path -Path $Destination
}
else {
# Otherwise, assume it's a directory path
$TempFolder = $Destination
}
# Check if destination path exists; create it if it does not
AE_WriteLog "INFO" "Check if the destination path '$TempFolder' exists. If not, create it" $LogFile
if (Test-Path $TempFolder) {
AE_WriteLog "INFO" "The destination path '$TempFolder' already exists. Nothing to do" $LogFile
}
else {
AE_WriteLog "INFO" "The destination path '$TempFolder' does not exist" $LogFile
AE_NewDirectory -Directory $TempFolder
}
# Move the source directories
AE_WriteLog "INFO" "Start moving the source directory(s) '$SourceDirectories' to '$Destination'" $LogFile
try {
Move-Item -Path $SourceDirectories -Destination $Destination -Force
AE_WriteLog "SUCCESS" "Successfully moved the source directory(s) '$SourceDirectories' to '$Destination'" $LogFile
}
catch {
# Log the error if the move fails
AE_WriteLog "ERROR" "An error occurred while moving the source directory(s) '$SourceDirectories' to '$Destination'. Error: $($_.Exception.Message)" $LogFile
Exit 1
}
}
end {
# Log the end of the function
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_RenameItem
#==========================================================================
Function AE_RenameItem {
<#
.SYNOPSIS
Rename files and folders
.DESCRIPTION
Rename files and folders
.PARAMETER ItemPath
This parameter contains the full path to the file or folder that needs to be renamed (for example 'C:\Temp\MyOldFileName.txt' or 'C:\Temp\MyOldFolderName')
.PARAMETER NewName
This parameter contains the new name of the file or folder (for example 'MyNewFileName.txt' or 'MyNewFolderName')
.EXAMPLE
AE_RenameItem -ItemPath "C:\Temp\MyOldFileName.txt" -NewName "MyNewFileName.txt"
Renames the file "C:\Temp\MyOldFileName.txt" to "MyNewFileName.txt". The parameter 'NewName' only requires the new file name without specifying the path to the file
.EXAMPLE
AE_RenameItem -ItemPath "C:\Temp\MyOldFileName.txt" -NewName "MyNewFileName.rtf"
Renames the file "C:\Temp\MyOldFileName.txt" to "MyNewFileName.rtf". Besides changing the name of the file, the file extension is modified as well. Please make sure that the new file format is compatible with the original file format and can actually be opened after being renamed! The parameter 'NewName' only requires the new file name without specifying the path to the file
.EXAMPLE
AE_RenameItem -ItemPath "C:\Temp\MyOldFolderName" -NewName "MyNewFolderName"
Renames the folder "C:\Temp\MyOldFolderName" to "C:\Temp\MyNewFolderName". The parameter 'NewName' only requires the new folder name without specifying the path to the folder
#>
[CmdletBinding()]
Param(
[Alias("I")]
[Parameter(Mandatory = $true, Position = 0)][String]$ItemPath,
[Alias("N")]
[Parameter(Mandatory = $true, Position = 1)][String]$NewName
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
AE_WriteLog "INFO" "Rename '$ItemPath' to '$NewName'" $LogFile
# Rename the item (if exist)
if ( Test-Path $ItemPath ) {
try {
Rename-Item -path $ItemPath -NewName $NewName | Out-Null
AE_WriteLog "SUCCESS" "The item '$ItemPath' was renamed to '$NewName' successfully" $LogFile
}
catch {
AE_WriteLog "ERROR" "An error occurred trying to rename the item '$ItemPath' to '$NewName' (exit code: $($Error[0]))!" $LogFile
Exit 1
}
}
else {
AE_WriteLog "INFO" "The item '$ItemPath' does not exist. Nothing to do" $LogFile
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
###########################################################################
# #
# WINDOWS \ INSTALLATIONS AND EXECUTABLES #
# #
###########################################################################
Function AE_RunProcess {
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)]
[String]$FileName,
[Alias("A")]
[Parameter(Mandatory = $false, Position = 1)]
[String[]]$Arguments # ← Change from [String] to [String[]]
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
if ($null -eq $Arguments -or $Arguments.Count -eq 0) {
AE_WriteLog "INFO" "Execute process '$FileName' with no arguments" $LogFile
if (-not (Test-Path $FileName)) {
AE_WriteLog "ERROR" "Executable not found: '$FileName'" $LogFile
Exit 1
}
$Process = Start-Process -FilePath $FileName -Wait -NoNewWindow -PassThru
}
else {
AE_WriteLog "INFO" "Execute process '$FileName' with $($Arguments.Count) arguments" $LogFile
if (-not (Test-Path $FileName)) {
AE_WriteLog "ERROR" "Executable not found: '$FileName'" $LogFile
Exit 1
}
# ← This now works perfectly with array
$Process = Start-Process -FilePath $FileName -ArgumentList $Arguments -Wait -NoNewWindow -PassThru
}
$ProcessExitCode = $Process.ExitCode
if ($ProcessExitCode -eq 0) {
AE_WriteLog "SUCCESS" "The process '$FileName' ended successfully" $LogFile
}
else {
AE_WriteLog "ERROR" "An error occurred trying to execute the process '$FileName' (exit code: $ProcessExitCode)!" $LogFile
# Optionally capture stderr here in the future
Exit 1
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
#==========================================================================
# FUNCTION AE_RunProcessNoWait
#==========================================================================
Function AE_RunProcessNoWait {
<#
.SYNOPSIS
Execute a process without wait
.DESCRIPTION
Execute a process without wait
.PARAMETER FileName
This parameter contains the full path including the file name and file extension of the executable (for example C:\Temp\MyApp.exe).
.PARAMETER Arguments
This parameter contains the list of arguments to be executed together with the executable.
.EXAMPLE
AE_RunProcessNoWait -FileName "C:\Temp\MyApp.exe" -Arguments "-silent"
Executes the file 'MyApp.exe' with arguments '-silent' with no wait to complete
#>
[CmdletBinding()]
Param(
[Alias("F")]
[Parameter(Mandatory = $true, Position = 0)][String]$FileName,
[Alias("A")]
[Parameter(Mandatory = $false, Position = 1)][String]$Arguments
)
begin {
[string]$FunctionName = $PSCmdlet.MyInvocation.MyCommand.Name
AE_WriteLog "INFO" "START FUNCTION - $FunctionName" $LogFile
}
process {
try {
if ([string]::IsNullOrEmpty($Arguments)) {
AE_WriteLog "INFO" "Execute process '$FileName' with no arguments without wait." $LogFile
if (-not (Test-Path $FileName)) {
AE_WriteLog "ERROR" "Executable not found: '$FileName'" $LogFile
Exit 1
}
Start-Process -FilePath $FileName -NoNewWindow -PassThru
}
else {
AE_WriteLog "INFO" "Execute process '$FileName' with arguments '$Arguments' without wait." $LogFile
if (-not (Test-Path $FileName)) {
AE_WriteLog "ERROR" "Executable not found: '$FileName'" $LogFile
Exit 1
}
Start-Process -FilePath $FileName -ArgumentList $Arguments -NoNewWindow -PassThru
}
AE_WriteLog "SUCCESS" "The process '$FileName' was started successfully." $LogFile
}
catch {
AE_WriteLog "ERROR" "Failed to start process '$FileName'. Exception: $_" $LogFile
Exit 1
}
}
end {
AE_WriteLog "INFO" "END FUNCTION - $FunctionName" $LogFile
}
}
###########################################################################
# #
# WINDOWS \ LOGGING #
# #
###########################################################################
#==========================================================================
# FUNCTION AE_WriteLog
#==========================================================================
Function AE_WriteLog {
<#
.SYNOPSIS
Write text to this script's log file.
.DESCRIPTION
Write text to this script's log file.
This parameter contains the information type prefix. Possible prefixes and information types are:
I = Information
INFO = Information
S = Success
SUCCESS = Success
W = Warning
WARNING = Warning
E = Error
ERROR = Error
T = Trace
TRACE = Trace
- = No status
.PARAMETER Text
This parameter contains the text (the line) you want to write to the log file. If text in the parameter is omitted, an empty line is written.
.PARAMETER LogFile
This parameter contains the full path, the file name and file extension to the log file (e.g. C:\Logs\MyApps\MylogFile.log)
.EXAMPLE
AE_WriteLog -InformationType "I" -Text "Copy files to C:\Temp" -LogFile "C:\Logs\MylogFile.log"
Writes a line containing information to the log file
.Example
AE_WriteLog -InformationType "E" -Text "An error occurred trying to copy files to C:\Temp (error: $($Error[0]))!" -LogFile "C:\Logs\MylogFile.log"
Writes a line containing error information to the log file
.Example
AE_WriteLog -InformationType "-" -Text "" -LogFile "C:\Logs\MylogFile.log"
Writes an empty line to the log file
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet("INFO", "SUCCESS", "WARNING", "ERROR", "TRACE", "I", "S", "W", "E", "T", IgnoreCase = $true)]
[String]$InformationType,
[Parameter(Mandatory = $true, Position = 1)]
[AllowEmptyString()]
[String]$Text,
[Parameter(Mandatory = $false, Position = 2)]
[String]$LogFile
)
begin {}
process {
# Create directory if needed
if ($LogFile) {
$LogDir = Split-Path $LogFile
if (-not (Test-Path $LogDir)) {
New-Item -ItemType Directory -Path $LogDir -Force | Out-Null
}
}
# Create file if missing
if ($LogFile -and -not (Test-Path $LogFile)) {
New-Item $LogFile -ItemType File -Force | Out-Null
}
$DateTime = (Get-Date -format "dd-MM-yyyy HH:mm:ss")
# Normalize type (INFO = I, SUCCESS = S, etc.)
switch ($InformationType.ToUpper()) {
"INFO" { $NormType = "INFO" }
"I" { $NormType = "INFO" }
"SUCCESS" { $NormType = "SUCCESS" }
"S" { $NormType = "SUCCESS" }
"WARNING" { $NormType = "WARNING" }
"W" { $NormType = "WARNING" }
"ERROR" { $NormType = "ERROR" }
"E" { $NormType = "ERROR" }
"TRACE" { $NormType = "TRACE" }
"T" { $NormType = "TRACE" }
}
# Alinhamento
$FormattedType = $NormType.PadRight(7)
if ($Text -eq "") {
Out-File -FilePath $LogFile -Append -Encoding UTF8 -InputObject ""
}
else {
$LogEntry = "$DateTime $FormattedType :....$Text"
Out-File -FilePath $LogFile -Append -Encoding UTF8 -InputObject $LogEntry
}
# Console color output