-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-DOS83Path.ps1
1692 lines (1597 loc) · 83.3 KB
/
Get-DOS83Path.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
function Get-DOS83Path {
# .SYNOPSIS
# Retrieves the DOS 8.3 path (short path) for a given file or folder path.
#
# .DESCRIPTION
# Given a path to a folder or file, translates the path to its DOS-
# compatible "8.3" formatted path. DOS did not support long file/folder
# paths, so, since long file paths were introduced, by default, Windows
# maintains a DOS-compatible 8.3 file name side-by-side with modern long
# file/folder names. This function gets the short 8.3 path.
#
# .PARAMETER ReferenceToDOS8Dot3Path
# This parameter is required; it is a reference to a string. If the process
# was successful, the referenced string will be updated to contain the
# short DOS 8.3 path. If the process is not successful, then the contents
# of the string are undefined.
#
# .PARAMETER Path
# This parameter is required; it is a string containing the path of the
# folder or file for which we want to retrieve its DOS 8.3 file path.
#
# .PARAMETER ReferenceToScriptingFileSystemObject
# This parameter is optional; if specified, it is a reference to a
# Scripting.FileSystemObject object. Supplying this parameter can speed up
# performance by avoiding to have to create the Scripting.FileSystemObject
# every time this function is called.
#
# .EXAMPLE
# $strPath = 'D:\Shares\Human Resources\Personnel Information\Employee Files\John Doe.docx'
# $strDOS83Path = ''
# $boolSuccess = Get-DOS83Path -ReferenceToDOS8Dot3Path ([ref]$strDOS83Path) -Path $strPath
#
# .EXAMPLE
# $objFSO = New-Object -ComObject Scripting.FileSystemObject
# $strPath = 'D:\Shares\Human Resources\Personnel Information\Employee Files\John Doe.docx'
# $strDOS83Path = ''
# $boolSuccess = Get-DOS83Path -ReferenceToDOS8Dot3Path ([ref]$strDOS83Path) -Path $strPath -ReferenceToScriptingFileSystemObject ([ref]$objFSO)
#
# .EXAMPLE
# $strPath = 'D:\Shares\Human Resources\Personnel Information\Employee Files\John Doe.docx'
# $strDOS83Path = ''
# $boolSuccess = Get-DOS83Path ([ref]$strDOS83Path) $strPath
#
# .INPUTS
# None. You can't pipe objects to Get-DOS83Path.
#
# .OUTPUTS
# System.Boolean. Get-DOS83Path returns a boolean value indiciating
# whether the process completed successfully. $true means the process
# completed successfully; $false means there was an error.
#
# .NOTES
# This function also supports the use of positional parameters instead of
# named parameters. If positional parameters are used intead of named
# parameters, then two or three positional parameters are required:
#
# The first positional parameter is a reference to a string. If the process
# was successful, the referenced string will be updated to contain the
# short DOS 8.3 path. If the process is not successful, then the contents
# of the string are undefined.
#
# The second positional parameter is a string containing the path of the
# folder or file for which we want to retrieve its DOS 8.3 file path.
#
# The third positional parameter is optional. If supplied, it is a
# reference to a Scripting.FileSystemObject object. Supplying this
# parameter can speed up performance by avoiding to have to create the
# Scripting.FileSystemObject every time this function is called.
#
# Version: 1.1.20241223.0
#region License ########################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion License ########################################################
param (
[ref]$ReferenceToDOS8Dot3Path = ([ref]$null),
[string]$Path = '',
[ref]$ReferenceToScriptingFileSystemObject = ([ref]$null)
)
#region FunctionsToSupportErrorHandling ####################################
function Get-ReferenceToLastError {
# .SYNOPSIS
# Gets a reference (memory pointer) to the last error that occurred.
#
# .DESCRIPTION
# Returns a reference (memory pointer) to $null ([ref]$null) if no
# errors on on the $error stack; otherwise, returns a reference to the
# last error that occurred.
#
# .EXAMPLE
# # Intentionally empty trap statement to prevent terminating errors
# # from halting processing
# trap { }
#
# # Retrieve the newest error on the stack prior to doing work:
# $refLastKnownError = Get-ReferenceToLastError
#
# # Store current error preference; we will restore it after we do some
# # work:
# $actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
#
# # Set ErrorActionPreference to SilentlyContinue; this will suppress
# # error output. Terminating errors will not output anything, kick to
# # the empty trap statement and then continue on. Likewise, non-
# # terminating errors will also not output anything, but they do not
# # kick to the trap statement; they simply continue on.
# $global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
#
# # Do something that might trigger an error
# Get-Item -Path 'C:\MayNotExist.txt'
#
# # Restore the former error preference
# $global:ErrorActionPreference = $actionPreferenceFormerErrorPreference
#
# # Retrieve the newest error on the error stack
# $refNewestCurrentError = Get-ReferenceToLastError
#
# $boolErrorOccurred = $false
# if (($null -ne $refLastKnownError.Value) -and ($null -ne $refNewestCurrentError.Value)) {
# # Both not $null
# if (($refLastKnownError.Value) -ne ($refNewestCurrentError.Value)) {
# $boolErrorOccurred = $true
# }
# } else {
# # One is $null, or both are $null
# # NOTE: $refLastKnownError could be non-null, while
# # $refNewestCurrentError could be null if $error was cleared;
# # this does not indicate an error.
# # So:
# # If both are null, no error
# # If $refLastKnownError is null and $refNewestCurrentError is
# # non-null, error
# # If $refLastKnownError is non-null and $refNewestCurrentError is
# # null, no error
# if (($null -eq $refLastKnownError.Value) -and ($null -ne $refNewestCurrentError.Value)) {
# $boolErrorOccurred = $true
# }
# }
#
# .INPUTS
# None. You can't pipe objects to Get-ReferenceToLastError.
#
# .OUTPUTS
# System.Management.Automation.PSReference ([ref]).
# Get-ReferenceToLastError returns a reference (memory pointer) to the
# last error that occurred. It returns a reference to $null
# ([ref]$null) if there are no errors on on the $error stack.
#
# .NOTES
# Version: 2.0.20241223.0
#region License ####################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ####################################################
if ($Error.Count -gt 0) {
return ([ref]($Error[0]))
} else {
return ([ref]$null)
}
}
function Test-ErrorOccurred {
# .SYNOPSIS
# Checks to see if an error occurred during a time period, i.e., during
# the execution of a command.
#
# .DESCRIPTION
# Using two references (memory pointers) to errors, this function
# checks to see if an error occurred based on differences between the
# two errors.
#
# To use this function, you must first retrieve a reference to the last
# error that occurred prior to the command you are about to run. Then,
# run the command. After the command completes, retrieve a reference to
# the last error that occurred. Pass these two references to this
# function to determine if an error occurred.
#
# .PARAMETER ReferenceToEarlierError
# This parameter is required; it is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack earlier in time, i.e., prior to running the
# command for which you wish to determine whether an error occurred.
#
# If no error was on the stack at this time, ReferenceToEarlierError
# must be a reference to $null ([ref]$null).
#
# .PARAMETER ReferenceToLaterError
# This parameter is required; it is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack later in time, i.e., after to running the command
# for which you wish to determine whether an error occurred.
#
# If no error was on the stack at this time, ReferenceToLaterError
# must be a reference to $null ([ref]$null).
#
# .EXAMPLE
# # Intentionally empty trap statement to prevent terminating errors
# # from halting processing
# trap { }
#
# # Retrieve the newest error on the stack prior to doing work
# if ($Error.Count -gt 0) {
# $refLastKnownError = ([ref]($Error[0]))
# } else {
# $refLastKnownError = ([ref]$null)
# }
#
# # Store current error preference; we will restore it after we do some
# # work:
# $actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
#
# # Set ErrorActionPreference to SilentlyContinue; this will suppress
# # error output. Terminating errors will not output anything, kick to
# # the empty trap statement and then continue on. Likewise, non-
# # terminating errors will also not output anything, but they do not
# # kick to the trap statement; they simply continue on.
# $global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
#
# # Do something that might trigger an error
# Get-Item -Path 'C:\MayNotExist.txt'
#
# # Restore the former error preference
# $global:ErrorActionPreference = $actionPreferenceFormerErrorPreference
#
# # Retrieve the newest error on the error stack
# if ($Error.Count -gt 0) {
# $refNewestCurrentError = ([ref]($Error[0]))
# } else {
# $refNewestCurrentError = ([ref]$null)
# }
#
# if (Test-ErrorOccurred -ReferenceToEarlierError $refLastKnownError -ReferenceToLaterError $refNewestCurrentError) {
# # Error occurred
# } else {
# # No error occurred
# }
#
# .INPUTS
# None. You can't pipe objects to Test-ErrorOccurred.
#
# .OUTPUTS
# System.Boolean. Test-ErrorOccurred returns a boolean value indicating
# whether an error occurred during the time period in question. $true
# indicates an error occurred; $false indicates no error occurred.
#
# .NOTES
# This function also supports the use of positional parameters instead
# of named parameters. If positional parameters are used intead of
# named parameters, then two positional parameters are required:
#
# The first positional parameter is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack earlier in time, i.e., prior to running the
# command for which you wish to determine whether an error occurred. If
# no error was on the stack at this time, the first positional
# parameter must be a reference to $null ([ref]$null).
#
# The second positional parameter is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack later in time, i.e., after to running the command
# for which you wish to determine whether an error occurred. If no
# error was on the stack at this time, ReferenceToLaterError must be
# a reference to $null ([ref]$null).
#
# Version: 2.0.20241223.0
#region License ####################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ####################################################
param (
[ref]$ReferenceToEarlierError = ([ref]$null),
[ref]$ReferenceToLaterError = ([ref]$null)
)
# TODO: Validate input
$boolErrorOccurred = $false
if (($null -ne $ReferenceToEarlierError.Value) -and ($null -ne $ReferenceToLaterError.Value)) {
# Both not $null
if (($ReferenceToEarlierError.Value) -ne ($ReferenceToLaterError.Value)) {
$boolErrorOccurred = $true
}
} else {
# One is $null, or both are $null
# NOTE: $ReferenceToEarlierError could be non-null, while
# $ReferenceToLaterError could be null if $error was cleared; this
# does not indicate an error.
# So:
# - If both are null, no error
# - If $ReferenceToEarlierError is null and $ReferenceToLaterError
# is non-null, error
# - If $ReferenceToEarlierError is non-null and
# $ReferenceToLaterError is null, no error
if (($null -eq $ReferenceToEarlierError.Value) -and ($null -ne $ReferenceToLaterError.Value)) {
$boolErrorOccurred = $true
}
}
return $boolErrorOccurred
}
#endregion FunctionsToSupportErrorHandling ####################################
function Get-ScriptingFileSystemObjectSafely {
# .SYNOPSIS
# Creates a COM object for Scripting.FileSystemObject.
#
# .DESCRIPTION
# Creates a COM object for Scripting.FileSystemObject. If the object cannot be
# created, then the function will return $false. If the object is created
# successfully, then the function will return $true.
#
# .PARAMETER ReferenceToStoreObject
# This parameter is required; it is a reference to an object that will become
# the FileSystemObject COM object. If the object is created successfully, then
# the referenced object will be updated, storing the FileSystemObject COM
# object. If the object is not created successfully, then the referenced
# variable becomes undefined.
#
# .EXAMPLE
# $objScriptingFileSystemObject = $null
# $boolSuccess = Get-ScriptingFileSystemObjectSafely -ReferenceToStoreObject ([ref]$objScriptingFileSystemObject)
#
# .EXAMPLE
# $objScriptingFileSystemObject = $null
# $boolSuccess = Get-ScriptingFileSystemObjectSafely ([ref]$objScriptingFileSystemObject)
#
# .INPUTS
# None. You can't pipe objects to Get-ScriptingFileSystemObjectSafely.
#
# .OUTPUTS
# System.Boolean. Get-ScriptingFileSystemObjectSafely returns a boolean value
# indiciating whether the Scripting.FileSystemObject object was created
# successfully. $true means the object was created successfully; $false means
# there was an error.
#
# .NOTES
# This function also supports the use of a positional parameter instead of a
# named parameter. If a positional parameter is used intead of a named
# parameter, then exactly one positional parameter is required:
#
# The first and only positional parameter is a reference to an object that will
# become the FileSystemObject COM object. If the object is created
# successfully, then the referenced object will be updated, storing the
# FileSystemObject COM object. If the object is not created successfully, then
# the referenced variable becomes undefined.
#
# Version: 1.1.20241223.0
#region License ############################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ############################################################
param (
[ref]$ReferenceToStoreObject = ([ref]$null)
)
#region FunctionsToSupportErrorHandling ####################################
function Get-ReferenceToLastError {
# .SYNOPSIS
# Gets a reference (memory pointer) to the last error that occurred.
#
# .DESCRIPTION
# Returns a reference (memory pointer) to $null ([ref]$null) if no
# errors on on the $error stack; otherwise, returns a reference to the
# last error that occurred.
#
# .EXAMPLE
# # Intentionally empty trap statement to prevent terminating errors
# # from halting processing
# trap { }
#
# # Retrieve the newest error on the stack prior to doing work:
# $refLastKnownError = Get-ReferenceToLastError
#
# # Store current error preference; we will restore it after we do some
# # work:
# $actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
#
# # Set ErrorActionPreference to SilentlyContinue; this will suppress
# # error output. Terminating errors will not output anything, kick to
# # the empty trap statement and then continue on. Likewise, non-
# # terminating errors will also not output anything, but they do not
# # kick to the trap statement; they simply continue on.
# $global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
#
# # Do something that might trigger an error
# Get-Item -Path 'C:\MayNotExist.txt'
#
# # Restore the former error preference
# $global:ErrorActionPreference = $actionPreferenceFormerErrorPreference
#
# # Retrieve the newest error on the error stack
# $refNewestCurrentError = Get-ReferenceToLastError
#
# $boolErrorOccurred = $false
# if (($null -ne $refLastKnownError.Value) -and ($null -ne $refNewestCurrentError.Value)) {
# # Both not $null
# if (($refLastKnownError.Value) -ne ($refNewestCurrentError.Value)) {
# $boolErrorOccurred = $true
# }
# } else {
# # One is $null, or both are $null
# # NOTE: $refLastKnownError could be non-null, while
# # $refNewestCurrentError could be null if $error was cleared;
# # this does not indicate an error.
# # So:
# # If both are null, no error
# # If $refLastKnownError is null and $refNewestCurrentError is
# # non-null, error
# # If $refLastKnownError is non-null and $refNewestCurrentError is
# # null, no error
# if (($null -eq $refLastKnownError.Value) -and ($null -ne $refNewestCurrentError.Value)) {
# $boolErrorOccurred = $true
# }
# }
#
# .INPUTS
# None. You can't pipe objects to Get-ReferenceToLastError.
#
# .OUTPUTS
# System.Management.Automation.PSReference ([ref]).
# Get-ReferenceToLastError returns a reference (memory pointer) to the
# last error that occurred. It returns a reference to $null
# ([ref]$null) if there are no errors on on the $error stack.
#
# .NOTES
# Version: 2.0.20241223.0
#region License ####################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ####################################################
if ($Error.Count -gt 0) {
return ([ref]($Error[0]))
} else {
return ([ref]$null)
}
}
function Test-ErrorOccurred {
# .SYNOPSIS
# Checks to see if an error occurred during a time period, i.e., during
# the execution of a command.
#
# .DESCRIPTION
# Using two references (memory pointers) to errors, this function
# checks to see if an error occurred based on differences between the
# two errors.
#
# To use this function, you must first retrieve a reference to the last
# error that occurred prior to the command you are about to run. Then,
# run the command. After the command completes, retrieve a reference to
# the last error that occurred. Pass these two references to this
# function to determine if an error occurred.
#
# .PARAMETER ReferenceToEarlierError
# This parameter is required; it is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack earlier in time, i.e., prior to running the
# command for which you wish to determine whether an error occurred.
#
# If no error was on the stack at this time, ReferenceToEarlierError
# must be a reference to $null ([ref]$null).
#
# .PARAMETER ReferenceToLaterError
# This parameter is required; it is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack later in time, i.e., after to running the command
# for which you wish to determine whether an error occurred.
#
# If no error was on the stack at this time, ReferenceToLaterError
# must be a reference to $null ([ref]$null).
#
# .EXAMPLE
# # Intentionally empty trap statement to prevent terminating errors
# # from halting processing
# trap { }
#
# # Retrieve the newest error on the stack prior to doing work
# if ($Error.Count -gt 0) {
# $refLastKnownError = ([ref]($Error[0]))
# } else {
# $refLastKnownError = ([ref]$null)
# }
#
# # Store current error preference; we will restore it after we do some
# # work:
# $actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
#
# # Set ErrorActionPreference to SilentlyContinue; this will suppress
# # error output. Terminating errors will not output anything, kick to
# # the empty trap statement and then continue on. Likewise, non-
# # terminating errors will also not output anything, but they do not
# # kick to the trap statement; they simply continue on.
# $global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
#
# # Do something that might trigger an error
# Get-Item -Path 'C:\MayNotExist.txt'
#
# # Restore the former error preference
# $global:ErrorActionPreference = $actionPreferenceFormerErrorPreference
#
# # Retrieve the newest error on the error stack
# if ($Error.Count -gt 0) {
# $refNewestCurrentError = ([ref]($Error[0]))
# } else {
# $refNewestCurrentError = ([ref]$null)
# }
#
# if (Test-ErrorOccurred -ReferenceToEarlierError $refLastKnownError -ReferenceToLaterError $refNewestCurrentError) {
# # Error occurred
# } else {
# # No error occurred
# }
#
# .INPUTS
# None. You can't pipe objects to Test-ErrorOccurred.
#
# .OUTPUTS
# System.Boolean. Test-ErrorOccurred returns a boolean value indicating
# whether an error occurred during the time period in question. $true
# indicates an error occurred; $false indicates no error occurred.
#
# .NOTES
# This function also supports the use of positional parameters instead
# of named parameters. If positional parameters are used intead of
# named parameters, then two positional parameters are required:
#
# The first positional parameter is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack earlier in time, i.e., prior to running the
# command for which you wish to determine whether an error occurred. If
# no error was on the stack at this time, the first positional
# parameter must be a reference to $null ([ref]$null).
#
# The second positional parameter is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack later in time, i.e., after to running the command
# for which you wish to determine whether an error occurred. If no
# error was on the stack at this time, ReferenceToLaterError must be
# a reference to $null ([ref]$null).
#
# Version: 2.0.20241223.0
#region License ####################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ####################################################
param (
[ref]$ReferenceToEarlierError = ([ref]$null),
[ref]$ReferenceToLaterError = ([ref]$null)
)
# TODO: Validate input
$boolErrorOccurred = $false
if (($null -ne $ReferenceToEarlierError.Value) -and ($null -ne $ReferenceToLaterError.Value)) {
# Both not $null
if (($ReferenceToEarlierError.Value) -ne ($ReferenceToLaterError.Value)) {
$boolErrorOccurred = $true
}
} else {
# One is $null, or both are $null
# NOTE: $ReferenceToEarlierError could be non-null, while
# $ReferenceToLaterError could be null if $error was cleared; this
# does not indicate an error.
# So:
# - If both are null, no error
# - If $ReferenceToEarlierError is null and $ReferenceToLaterError
# is non-null, error
# - If $ReferenceToEarlierError is non-null and
# $ReferenceToLaterError is null, no error
if (($null -eq $ReferenceToEarlierError.Value) -and ($null -ne $ReferenceToLaterError.Value)) {
$boolErrorOccurred = $true
}
}
return $boolErrorOccurred
}
#endregion FunctionsToSupportErrorHandling ####################################
trap {
# Intentionally left empty to prevent terminating errors from halting
# processing
}
# TODO: Validate input
# Retrieve the newest error on the stack prior to doing work
$refLastKnownError = Get-ReferenceToLastError
# Store current error preference; we will restore it after we do the work of
# this function
$actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
# Set ErrorActionPreference to SilentlyContinue; this will suppress error
# output. Terminating errors will not output anything, kick to the empty trap
# statement and then continue on. Likewise, non-terminating errors will also
# not output anything, but they do not kick to the trap statement; they simply
# continue on.
$global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
# Do the work of this function...
$ReferenceToStoreObject.Value = New-Object -ComObject Scripting.FileSystemObject
# Restore the former error preference
$global:ErrorActionPreference = $actionPreferenceFormerErrorPreference
# Retrieve the newest error on the error stack
$refNewestCurrentError = Get-ReferenceToLastError
if (Test-ErrorOccurred -ReferenceToEarlierError $refLastKnownError -ReferenceToLaterError $refNewestCurrentError) {
# Error occurred; return failure indicator:
return $false
} else {
# No error occurred; return success indicator:
return $true
}
}
function Get-FolderObjectSafelyUsingScriptingFileSystemObject {
# .SYNOPSIS
# Get's a Folder object using the Scripting.FileSystemObject COM object.
#
# .DESCRIPTION
# This function gets a Folder object using the Scripting.FileSystemObject
# COM object. If the Folder object is successfully created, then the
# function returns $true; otherwise, the function returns $false. If the
# function returns $false, then the Folder object is not created, and the
# referenced Folder object is undefined.
#
# .PARAMETER ReferenceToFolderObject
# This parameter is required; it is a reference to an object that will
# become the Folder COM object created using Scripting.FileSystemObject. If
# the object is created successfully, then the referenced object will be
# updated, storing the Folder COM object. If the object is not created
# successfully, then the referenced variable becomes undefined.
#
# .PARAMETER ReferenceToScriptingFileSystemObject
# This parameter is required; it is a reference to a
# Scripting.FileSystemObject COM object, which has already been
# initialized.
#
# .PARAMETER Path
# This parameter is required; it is a string containing the path to the
# folder for which this function will obtain the Folder COM object.
#
# .EXAMPLE
# $strPath = 'D:\Shares\Human Resources\Personnel Information\Employee Files\John Doe'
# $objScriptingFileSystemObject = New-Object -ComObject Scripting.FileSystemObject
# $objFSOFolderObject = $null
# $boolSuccess = Get-FolderObjectSafelyUsingScriptingFileSystemObject -ReferenceToFolderObject ([ref]$objFSOFolderObject) -ReferenceToScriptingFileSystemObject ([ref]$objScriptingFileSystemObject) -Path $strPath
#
# .EXAMPLE
# $strPath = 'D:\Shares\Human Resources\Personnel Information\Employee Files\John Doe'
# $objScriptingFileSystemObject = New-Object -ComObject Scripting.FileSystemObject
# $objFSOFolderObject = $null
# $boolSuccess = Get-FolderObjectSafelyUsingScriptingFileSystemObject ([ref]$objFSOFolderObject) ([ref]$objScriptingFileSystemObject) $strPath
#
# .INPUTS
# None. You can't pipe objects to
# Get-FolderObjectSafelyUsingScriptingFileSystemObject.
#
# .OUTPUTS
# System.Boolean. Get-FolderObjectSafelyUsingScriptingFileSystemObject
# returns a boolean value indiciating whether the process completed
# successfully. $true means the process completed successfully; $false
# means there was an error.
#
# .NOTES
# This function also supports the use of positional parameters instead of
# named parameters. If positional parameters are used intead of named
# parameters, then three positional parameters are required:
#
# The first positional parameter is a reference to an object that will
# become the Folder COM object created using Scripting.FileSystemObject. If
# the object is created successfully, then the referenced object will be
# updated, storing the Folder COM object. If the object is not created
# successfully, then the referenced variable becomes undefined.
#
# The second positional parameter is a reference to a
# Scripting.FileSystemObject COM object, which has already been
# initialized.
#
# The third positional parameter is a string containing the path to the
# folder for which this function will obtain the Folder COM object.
#
# Version: 1.1.20241223.0
#region License ########################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion License ########################################################
param (
[ref]$ReferenceToFolderObject = ([ref]$null),
[ref]$ReferenceToScriptingFileSystemObject = ([ref]$null),
[string]$Path = ''
)
#region FunctionsToSupportErrorHandling ################################
function Get-ReferenceToLastError {
# .SYNOPSIS
# Gets a reference (memory pointer) to the last error that occurred.
#
# .DESCRIPTION
# Returns a reference (memory pointer) to $null ([ref]$null) if no
# errors on on the $error stack; otherwise, returns a reference to the
# last error that occurred.
#
# .EXAMPLE
# # Intentionally empty trap statement to prevent terminating errors
# # from halting processing
# trap { }
#
# # Retrieve the newest error on the stack prior to doing work:
# $refLastKnownError = Get-ReferenceToLastError
#
# # Store current error preference; we will restore it after we do some
# # work:
# $actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
#
# # Set ErrorActionPreference to SilentlyContinue; this will suppress
# # error output. Terminating errors will not output anything, kick to
# # the empty trap statement and then continue on. Likewise, non-
# # terminating errors will also not output anything, but they do not
# # kick to the trap statement; they simply continue on.
# $global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
#
# # Do something that might trigger an error
# Get-Item -Path 'C:\MayNotExist.txt'
#
# # Restore the former error preference
# $global:ErrorActionPreference = $actionPreferenceFormerErrorPreference
#
# # Retrieve the newest error on the error stack
# $refNewestCurrentError = Get-ReferenceToLastError
#
# $boolErrorOccurred = $false
# if (($null -ne $refLastKnownError.Value) -and ($null -ne $refNewestCurrentError.Value)) {
# # Both not $null
# if (($refLastKnownError.Value) -ne ($refNewestCurrentError.Value)) {
# $boolErrorOccurred = $true
# }
# } else {
# # One is $null, or both are $null
# # NOTE: $refLastKnownError could be non-null, while
# # $refNewestCurrentError could be null if $error was cleared;
# # this does not indicate an error.
# # So:
# # If both are null, no error
# # If $refLastKnownError is null and $refNewestCurrentError is
# # non-null, error
# # If $refLastKnownError is non-null and $refNewestCurrentError is
# # null, no error
# if (($null -eq $refLastKnownError.Value) -and ($null -ne $refNewestCurrentError.Value)) {
# $boolErrorOccurred = $true
# }
# }
#
# .INPUTS
# None. You can't pipe objects to Get-ReferenceToLastError.
#
# .OUTPUTS
# System.Management.Automation.PSReference ([ref]).
# Get-ReferenceToLastError returns a reference (memory pointer) to the
# last error that occurred. It returns a reference to $null
# ([ref]$null) if there are no errors on on the $error stack.
#
# .NOTES
# Version: 2.0.20241223.0
#region License ####################################################
# Copyright (c) 2024 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ####################################################
if ($Error.Count -gt 0) {
return ([ref]($Error[0]))
} else {
return ([ref]$null)
}
}
function Test-ErrorOccurred {
# .SYNOPSIS
# Checks to see if an error occurred during a time period, i.e., during
# the execution of a command.
#
# .DESCRIPTION
# Using two references (memory pointers) to errors, this function
# checks to see if an error occurred based on differences between the
# two errors.
#
# To use this function, you must first retrieve a reference to the last
# error that occurred prior to the command you are about to run. Then,
# run the command. After the command completes, retrieve a reference to
# the last error that occurred. Pass these two references to this
# function to determine if an error occurred.
#
# .PARAMETER ReferenceToEarlierError
# This parameter is required; it is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack earlier in time, i.e., prior to running the
# command for which you wish to determine whether an error occurred.
#
# If no error was on the stack at this time, ReferenceToEarlierError
# must be a reference to $null ([ref]$null).
#
# .PARAMETER ReferenceToLaterError
# This parameter is required; it is a reference (memory pointer) to a
# System.Management.Automation.ErrorRecord that represents the newest
# error on the stack later in time, i.e., after to running the command
# for which you wish to determine whether an error occurred.
#
# If no error was on the stack at this time, ReferenceToLaterError
# must be a reference to $null ([ref]$null).
#
# .EXAMPLE
# # Intentionally empty trap statement to prevent terminating errors
# # from halting processing
# trap { }
#
# # Retrieve the newest error on the stack prior to doing work
# if ($Error.Count -gt 0) {
# $refLastKnownError = ([ref]($Error[0]))
# } else {
# $refLastKnownError = ([ref]$null)
# }
#
# # Store current error preference; we will restore it after we do some
# # work:
# $actionPreferenceFormerErrorPreference = $global:ErrorActionPreference
#
# # Set ErrorActionPreference to SilentlyContinue; this will suppress
# # error output. Terminating errors will not output anything, kick to
# # the empty trap statement and then continue on. Likewise, non-
# # terminating errors will also not output anything, but they do not
# # kick to the trap statement; they simply continue on.
# $global:ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
#
# # Do something that might trigger an error
# Get-Item -Path 'C:\MayNotExist.txt'
#