Client/Start-PsFCIV.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
function Start-PsFCIV {
<#
.ExternalHelp PSPKI.Help.xml
#>

[CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [IO.DirectoryInfo]$Path,
        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = '__xml')]
        [string]$XML,
        [Parameter(Position = 2)]
        [string]$Include = "*",
        [Parameter(Position = 3)]
        [string[]]$Exclude,
        [ValidateSet("Rename", "Delete")]
        [string]$Action,
        [ValidateSet("Bad", "Locked", "Missed", "New", "Ok", "Unknown", "All")]
        [String[]]$Show,
        [ValidateSet("MD5", "SHA1", "SHA256", "SHA384", "SHA512")]
        [AllowEmptyCollection()]
        [String[]]$HashAlgorithm = "SHA1",
        [switch]$Recurse,
        [switch]$Rebuild,
        [switch]$Quiet,
        [switch]$NoStatistic,
        [Parameter(ParameterSetName = '__online')]
        [switch]$Online
    )

#region C# wrappers
Add-Type @"
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace PsFCIV {
 public class StatTable {
  public List<String> Total = new List<String>();
  public List<String> New = new List<String>();
  public List<String> Ok = new List<String>();
  public List<String> Bad = new List<String>();
  public List<String> Missed = new List<String>();
  public List<String> Locked = new List<String>();
  public List<String> Unknown = new List<String>();
  public int Del;
 }
 public class IntStatTable {
  public Int32 Total;
  public Int32 New;
  public Int32 Ok;
  public Int32 Bad;
  public Int32 Missed;
  public Int32 Locked;
  public Int32 Unknown;
  public Int32 Del;
 }
 [XmlType(AnonymousType = true)]
 [XmlRoot(Namespace = "", IsNullable = false)]
 public class FCIV {
  public FCIV() { FILE_ENTRY = new List<FCIVFILE_ENTRY>(); }
   
  [XmlElement("FILE_ENTRY")]
  public List<FCIVFILE_ENTRY> FILE_ENTRY { get; set; }
 }
 [XmlType(AnonymousType = true)]
 public class FCIVFILE_ENTRY {
  public FCIVFILE_ENTRY() { }
  public FCIVFILE_ENTRY(string path) { name = path; }
 
  public String name { get; set; }
  public UInt64 Size { get; set; }
  public String TimeStamp { get; set; }
  public String MD5 { get; set; }
  public String SHA1 { get; set; }
  public String SHA256 { get; set; }
  public String SHA384 { get; set; }
  public String SHA512 { get; set; }
 
  public override Int32 GetHashCode() { return name.GetHashCode(); }
  public override Boolean Equals(Object other) {
   if (ReferenceEquals(null, other) || other.GetType() != GetType()) { return false; }
   return other.GetType() == GetType() && String.Equals(name, ((FCIVFILE_ENTRY)other).name);
  }
 }
}
"@
 -Debug:$false -Verbose:$false -ReferencedAssemblies "System.Xml"
Add-Type -AssemblyName System.Xml
#endregion
    
    if ($PSBoundParameters.Verbose) {$VerbosePreference = "continue"}
    if ($PSBoundParameters.Debug) {$DebugPreference = "continue"}
    $oldverb = $host.PrivateData.VerboseForegroundColor
    $olddeb = $host.PrivateData.DebugForegroundColor
    # preserving current path
    $oldpath = $pwd.Path
    $Exclude += $XML

    if (Test-Path -LiteralPath $path) {
        Set-Location -LiteralPath $path
        if ($pwd.Provider.Name -ne "FileSystem") {
            Set-Location $oldpath
            throw "Specified path is not filesystem path. Try again!"
        }
    } else {throw "Specified path not found."}
    
    # statistic variables
    $sum = $new = New-Object PsFCIV.FCIV
    # creating statistics variable with properties. Each property will contain file names (and paths) with corresponding status.
    $global:stats = New-Object PsFCIV.StatTable
    $script:statcount = New-Object PsFCIV.IntStatTable
    
    # lightweight proxy function for Get-ChildItem cmdlet
    function dirx ([string]$Path, [string]$Filter, [string[]]$Exclude, $Recurse, [switch]$Force) {
        Get-ChildItem @PSBoundParameters -ErrorAction SilentlyContinue | Where-Object {!$_.psiscontainer}
    }    
    # internal function that will check whether the file is locked. All locked files are added to a group with 'Unknown' status.
    function __filelock ($file) {
        $locked = $false
        trap {Set-Variable -name locked -value $true -scope 1; continue}
        $inputStream = New-Object IO.StreamReader $file.FullName
        if ($inputStream) {$inputStream.Close()}
        if ($locked) {
            $host.PrivateData.VerboseForegroundColor = "Yellow"
            $host.PrivateData.DebugForegroundColor = "Yellow"
            Write-Verbose "File $($file.Name) is locked. Skipping this file.."
            Write-Debug "File $($file.Name) is locked. Skipping this file.."
            __statcounter $filename Locked
        }
        $locked
    }    
    # internal function to generate UI window with results by using Out-GridView cmdlet.
    function __formatter ($props, $max) {
        $total = @($input)
        foreach ($property in $props) {
            $(for ($n = 0; $n -lt $max; $n++) {
                $total[0] | Select-Object @{n = $property; e = {$_.$property[$n]}}
            }) | Out-GridView -Title "File list by category: $property"
        }
    }
    # internal hasher
    function __hashbytes ($type, $file) {
        $hasher = [Security.Cryptography.HashAlgorithm]::Create($type)
        $inputStream = New-Object IO.StreamReader $file.FullName
        $hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
        $hasher.Clear()
        $inputStream.Close()
        $hashBytes
    }
    # internal function which reads the XML file (if exist).
    function __fromxml ($xml) {
    # reading existing XML file and selecting required properties
        if (!(Test-Path -LiteralPath $XML)) {return New-Object PsFCIV.FCIV}
        try {
            $fs = New-Object IO.FileStream $XML, "Open"
            $xmlser = New-Object System.Xml.Serialization.XmlSerializer ([Type][PsFCIV.FCIV])
            $sum = $xmlser.Deserialize($fs)
            $fs.Close()
            $sum
        } catch {
            Write-Error -Category InvalidData -Message "Input XML file is not valid FCIV XML file."
        } finally {
            if ($fs -ne $null) {$fs.Close()}
        }
        
    }
    # internal xml writer
    function __writexml ($sum) {
        if ($sum.FILE_ENTRY.Count -eq 0) {
            $host.PrivateData.VerboseForegroundColor = "Yellow"
            $host.PrivateData.DebugForegroundColor = "Yellow"
            Write-Verbose "There is no data to write to XML database."
            Write-Debug "There is no data to write to XML database."
        } else {
            $host.PrivateData.DebugForegroundColor = "Cyan"
            Write-Debug "Preparing to DataBase file creation..."
            try {
                $fs = New-Object IO.FileStream $XML, "Create"
                $xmlser = New-Object System.Xml.Serialization.XmlSerializer ([Type][PsFCIV.FCIV])
                $xmlser.Serialize($fs,$sum)
            } finally {
                if ($fs -ne $null) {$fs.Close()}
            }
            Write-Debug "DataBase file created..."
        }
    }
    # internal function to create XML entry object for a file.
    function __makeobject ($file, [switch]$NoHash, [switch]$hex) {
        $host.PrivateData.DebugForegroundColor = "Yellow"
        Write-Debug "Starting object creation for '$($file.FullName)'..."
        $object = New-Object PsFCIV.FCIVFILE_ENTRY
        $object.name = $file.FullName -replace [regex]::Escape($($pwd.ProviderPath + "\"))
        $object.Size = $file.Length
        # use culture-invariant date/time format.
        $object.TimeStamp = "$($file.LastWriteTime.ToUniversalTime())"
        if (!$NoHash) {
        # calculating appropriate hash and convert resulting byte array to a Base64 string
            foreach ($hash in "MD5", "SHA1", "SHA256", "SHA384", "SHA512") {
                if ($HashAlgorithm -contains $hash) {
                    Write-Debug "Calculating '$hash' hash..."
                    $hashBytes = __hashbytes $hash $file
                    if ($hex) {
                        $object.$hash = -join ($hashBytes | Foreach-Object {"{0:X2}" -f $_})
                    } else {
                        Write-Debug ("Calculated hash value: " + (-join ($hashBytes | Foreach-Object {"{0:X2}" -f $_})))
                        $object.$hash = [System.Convert]::ToBase64String($hashBytes)
                    }
                }
            }
        }
        Write-Debug "Object created!"
        $object
    }    
    # internal function that calculates current file hash and formats it to an octet string (for example, B926D7416E8235E6F94F756E9F3AE2F33A92B2C4).
    function __precheck ($entry, $file, $HashAlgorithm) {
        $host.PrivateData.DebugForegroundColor = "Yellow"
        if ($HashAlgorithm.Length -gt 0) {
            $SelectedHash = $HashAlgorithm
        } else {
            :outer foreach ($hash in "SHA512", "SHA384", "SHA256", "SHA1", "MD5") {
                if ($entry.$hash) {$SelectedHash = $hash; break outer}
            }
        }
        $host.PrivateData.DebugForegroundColor = "Green"
        Write-Debug "Selected hash: $hash"
        -join ($(__hashbytes $SelectedHash $file) | ForEach-Object {"{0:X2}" -f $_})
        $SelectedHash
    }
    # process -Action parameter to perform an action against bad file (if actual file properties do not match the record in XML).
    function __takeaction ($file, $Action) {
        switch ($Action) {
            "Rename" {Rename-Item $file $($file.FullName + ".bad")}
            "Delete" {Remove-Item $file -Force}
        }
    }    
    # core file verification function.
    function __checkfiles ($entry, $file, $Action) {
        if (($file.Length -eq $entry.Size) -and ("$($file.LastWriteTime.ToUniversalTime())" -eq $entry.TimeStamp)) {
            $hexhash = __precheck $entry $file $HashAlgorithm
            $ActualHash = -join ([Convert]::FromBase64String($entry.($hexhash[1])) | ForEach-Object {"{0:X2}" -f $_})
            if (!$ActualHash) {
                $host.PrivateData.VerboseForegroundColor = "Red"
                Write-Verbose "XML database entry does not contains '$($hexhash[1])' hash value for the entry '$($entry.name)'."
                __statcounter $entry.name Unknown
                return
            } elseif ($ActualHash -eq $hexhash[0]) {
                $host.PrivateData.VerboseForegroundColor = $Host.PrivateData.DebugForegroundColor = "Green"
                Write-Debug "File hash: $ActualHash"
                Write-Verbose "File '$($file.name)' is ok."
                __statcounter $entry.name Ok
                return
            } else {
                $host.PrivateData.DebugForegroundColor = "Red"
                Write-Debug "File '$($file.name)' failed hash verification.
     Expected hash: $hexhash
     Actual hash: $ActualHash"

                __statcounter $entry.name Bad
                if ($Action) {__takeaction $file $Action}
            }
        } else {
            $host.PrivateData.VerboseForegroundColor = $Host.PrivateData.DebugForegroundColor = "Red"
            Write-Verbose "File '$($file.FullName)' size or Modified Date/Time mismatch."
            Write-Debug "Expected file size is: $($entry.Size) byte(s), actual size is: $($file.Length) byte(s)."
            Write-Debug "Expected file modification time is: $($entry.TimeStamp), actual file modification time is: $($file.LastWriteTime.ToUniversalTime())"
            __statcounter $entry.name Bad
            if ($Action) {__takeaction $file $Action}
        }
    }
    # internal function to calculate resulting statistics and show if if necessary.    
    function __stats {
    # if -Show parameter is presented we display selected groups (Total, New, Ok, Bad, Missed, Unknown)
        if ($show -and !$NoStatistic) {
            if ($Show -eq "All" -or $Show.Contains("All")) {
                $global:stats | __formatter "Bad", "Locked", "Missed", "New", "Ok", "Unknown" $script:statcount.Total
            } else {
                $global:stats | Select-Object $show | __formatter $show $script:statcount.Total
            }            
        }
        # script work in numbers
        if (!$Quiet) {
            Write-Host ----------------------------------- -ForegroundColor Green
            if ($Rebuild) {
                Write-Host Total entries processed: $script:statcount.Total -ForegroundColor Cyan
                Write-Host Total removed unused entries: $script:statcount.Del -ForegroundColor Yellow
            } else {Write-Host Total files processed: $script:statcount.Total -ForegroundColor Cyan}
            Write-Host Total new added files: $script:statcount.New -ForegroundColor Green
            Write-Host Total good files: $script:statcount.Ok -ForegroundColor Green
            Write-Host Total bad files: $script:statcount.Bad -ForegroundColor Red
            Write-Host Total unknown status files: $script:statcount.Unknown -ForegroundColor Yellow
            Write-Host Total missing files: $script:statcount.Missed -ForegroundColor Yellow
            Write-Host Total locked files: $script:statcount.Locked -ForegroundColor Yellow
            Write-Host ----------------------------------- -ForegroundColor Green
        }
        # restore original variables
        Set-Location -LiteralPath $oldpath
        $host.PrivateData.VerboseForegroundColor = $oldverb
        $host.PrivateData.DebugForegroundColor = $olddeb
        $exit = 0
        # create exit code depending on check status
        if ($Rebuild) {$exit = [int]::MaxValue} else {
            if ($script:statcount.Bad -ne 0) {$exit += 1}
            if ($script:statcount.Missed -ne 0) {$exit += 2}
            if ($script:statcount.Unknown -ne 0) {$exit += 4}
            if ($script:statcount.Locked -ne 0) {$exit += 8}
        }
        if ($Quiet) {exit $exit}
    }
    # internal function to update statistic counters.
    function __statcounter ($filename, $status) {
        $script:statcount.$status++
        $script:statcount.Total++
        if (!$NoStatistic) {
            $global:stats.$status.Add($filename)
        }
    }
    if ($Online) {
        $host.PrivateData.DebugForegroundColor = "White"
        Write-Debug "Online mode ON"
        dirx -Path .\* -Filter $Include -Exclude $Exclude $Recurse -Force | ForEach-Object {
            $host.PrivateData.VerboseForegroundColor = $Host.UI.RawUI.ForegroundColor
            Write-Verbose "Perform file '$($_.fullName)' checking."
            $file = Get-Item -LiteralPath $_.FullName -Force -ErrorAction SilentlyContinue
            if (__filelock $file) {return}
            __makeobject $file -hex
        }
        return
    }

    <#
 in this part we perform XML file update by removing entries for non-exist files and
 adding new entries for files that are not in the database.
 #>

    if ($Rebuild) {
        $host.PrivateData.DebugForegroundColor = "White"
        Write-Debug "Rebuild mode ON"
        if (Test-Path -LiteralPath $xml) {
            $old = __fromxml $xml
        } else {
            Set-Location $oldpath
            throw "Unable to find XML file. Please, run the command without '-Rebuild' switch."
        }
        $interm = New-Object PsFCIV.FCIV
        # use foreach-object instead of where-object to keep original types.
        $host.PrivateData.VerboseForegroundColor = $host.UI.RawUI.ForegroundColor
        Write-Verbose "Perform DB file cleanup from non-existent items."
        $old.FILE_ENTRY | ForEach-Object {
            if ((Test-Path -LiteralPath $_.name)) {
                if ($_.name -eq $xml) {
                    $host.PrivateData.DebugForegroundColor = "Yellow"
                    Write-Debug "File '$($_.name)' is DB file. Removed."
                } else {
                    $interm.FILE_ENTRY.Add($_)
                }
            } else {
                $host.PrivateData.DebugForegroundColor = "Yellow"
                Write-Debug "File '$($_.name)' does not exist. Removed."
            }
        }
        $script:statcount.Del = $interm.Length
        $script:statcount.Total = $old.FILE_ENTRY.Count - $interm.Length
        dirx -Path .\* -Filter $Include -Exclude $Exclude $Recurse -Force | ForEach-Object {
            $host.PrivateData.VerboseForegroundColor = $host.UI.RawUI.ForegroundColor
            Write-Verbose "Perform file '$($_.FullName)' checking."
            $file = Get-Item -LiteralPath $_.FullName -Force
            if (__filelock $file) {return}
            $filename = $file.FullName -replace [regex]::Escape($($pwd.providerpath + "\"))
            $host.PrivateData.VerboseForegroundColor = "Green"
            if ($interm.FILE_ENTRY.Contains((New-Object PsFCIV.FCIVFILE_ENTRY $filename))) {
                Write-Verbose "File '$filename' already exist in XML database. Skipping."
                return
            } else {
                $new.FILE_ENTRY.Add((__makeobject $file))
                Write-Verbose "File '$filename' is added."
                __statcounter $filename New
            }
        }
        $interm.FILE_ENTRY.AddRange($new.FILE_ENTRY)
        __writexml $interm
        __stats
        return
    }
    
    # this part contains main routine
    $sum = __fromxml $xml
    <#
 check XML file format. If Size property of the first element is zero, then the file was generated by
 original FCIV.exe tool. In this case we transform existing XML to a new PsFCIV format by adding new
 properties. Each record is checked against hashes stored in the source XML file. If hash check fails,
 an item is removed from final XML.
 #>

    if ($sum.FILE_ENTRY.Count -gt 0 -and $sum.FILE_ENTRY[0].Size -eq 0) {
        #
        if ($PSBoundParameters.ContainsKey("HashAlgorithm")) {$HashAlgorithm = $HashAlgorithm[0].ToUpper()} else {$HashAlgorithm = @()}
        $host.PrivateData.DebugForegroundColor = "White"
        Write-Debug "FCIV (compatibility) mode ON"
        if ($HashAlgorithm -and $HashAlgorithm -notcontains "sha1" -and $HashAlgorithm -notcontains "md5") {
            throw "Specified hash algorithm (or algorithms) is not supported. For native FCIV source, use MD5 and/or SHA1."
        }
        for ($index = 0; $index -lt $sum.FILE_ENTRY.Count; $index++) {
            $host.PrivateData.VerboseForegroundColor = $host.UI.RawUI.ForegroundColor
            Write-Verbose "Perform file '$($sum.FILE_ENTRY[$index].name)' checking."
            $filename = $sum.FILE_ENTRY[$index].name
            # check if the path is absolute and matches current path. If the path is absolute and does not belong to
            # current path -- skip this entry.
            if ($filename.Contains(":") -and $filename -notmatch [regex]::Escape($pwd.ProviderPath)) {return}
            # if source file name record contains absolute path, and belongs to the current pathe,
            # just strip base path. New XML format uses relative paths only.
            if ($filename.Contains(":")) {$filename = $filename -replace ([regex]::Escape($($pwd.ProviderPath + "\")))}
            # Test if the file exist. If the file does not exist, skip the current entry and process another record.
            if (!(Test-Path -LiteralPath $filename)) {
                $host.PrivateData.VerboseForegroundColor = "Yellow"
                Write-Verbose "File '$filename' not found. Skipping."
                __statcounter $filename Missed
                return
            }
            # get file item and test if it is not locked by another application
            $file = Get-Item -LiteralPath $filename -Force -ErrorAction SilentlyContinue
            if (__filelock $file) {return}
            # create new-style entry record that stores additional data: file length and last modification timestamp.
            $entry = __makeobject $file -NoHash
            $entry.name = $filename
            # process current hash entries and copy required hash values to a new entry object.
            "SHA1", "MD5" | ForEach-Object {$entry.$_ = $sum.FILE_ENTRY[$index].$_}
            $sum.FILE_ENTRY[$index] = $entry
            __checkfiles $newentry $file $Action
        }
        # we are done. Overwrite XML, display stats and exit.
        __writexml $sum
        # display statistics and exit right now.
        __stats
    }
    # if XML file exist, proccess and check all records. XML file will not be modified.
    if ($sum.FILE_ENTRY.Count -gt 0) {
        $host.PrivateData.DebugForegroundColor = "White"
        Write-Debug "Native PsFCIV mode ON"
        # this part is executed only when we want to process certain file. Wildcards are not allowed.
        if ($Include -ne "*") {
            $sum.FILE_ENTRY | Where-Object {$_.name -like $Include} | ForEach-Object {
                $host.PrivateData.VerboseForegroundColor = $host.UI.RawUI.ForegroundColor
                Write-Verbose "Perform file '$($_.name)' checking."
                $entry = $_
                # calculate the hash if the file exist.
                if (Test-Path -LiteralPath $entry.name) {
                    # and check file integrity
                    $file = Get-Item -LiteralPath $entry.name -Force -ErrorAction SilentlyContinue
                    __checkfiles $entry $file $Action
                } else {
                    # if there is no record for the file, skip it and display appropriate message
                    $host.PrivateData.VerboseForegroundColor = "Yellow"
                    Write-Verbose "File '$filename' not found. Skipping."
                    __statcounter $entry.name Missed
                }
            }
        } else {
            $sum.FILE_ENTRY | ForEach-Object {
                <#
    to process files only in the current directory (without subfolders), we remove items
    that contain slashes from the process list and continue regular file checking.
    #>

                if (!$Recurse -and $_.name -match "\\") {return}
                $host.PrivateData.VerboseForegroundColor = $host.UI.RawUI.ForegroundColor
                Write-Verbose "Perform file '$($_.name)' checking."
                $entry = $_
                if (Test-Path -LiteralPath $entry.name) {
                    $file = Get-Item -LiteralPath $entry.name -Force -ErrorAction SilentlyContinue
                    __checkfiles $entry $file $Action
                } else {
                    $host.PrivateData.VerboseForegroundColor = "Yellow"
                    Write-Verbose "File '$($entry.name)' not found. Skipping."
                    __statcounter $entry.name Missed
                }
            }
        }
    } else {
        # if there is no existing XML DB file, start from scratch and create a new one.
        $host.PrivateData.DebugForegroundColor = "White"
        Write-Debug "New XML mode ON"

        dirx -Path .\* -Filter $Include -Exclude $Exclude $Recurse -Force | ForEach-Object {
            $_
            # $host.PrivateData.VerboseForegroundColor = $Host.UI.RawUI.ForegroundColor
            # Write-Verbose "Perform file '$($_.fullName)' checking."
            # $file = Get-Item -LiteralPath $_.FullName -Force -ErrorAction SilentlyContinue
            # if (__filelock $file) {return}
            # $entry = __makeobject $file
            # $sum.FILE_ENTRY.Add($entry)
            # __statcounter $entry.name New
        }
        __writexml $sum
    }
    __stats
}
# SIG # Begin signature block
# MIIcgAYJKoZIhvcNAQcCoIIccTCCHG0CAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCCh54G+7BnjUc2T
# VlNgo0WdRcbYHLAtcSrMGdoBXdwbhqCCF4owggUTMIID+6ADAgECAhAJwnVp5a70
# RHscglFEfEqLMA0GCSqGSIb3DQEBCwUAMHIxCzAJBgNVBAYTAlVTMRUwEwYDVQQK
# EwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xMTAvBgNV
# BAMTKERpZ2lDZXJ0IFNIQTIgQXNzdXJlZCBJRCBDb2RlIFNpZ25pbmcgQ0EwHhcN
# MTcwNDE3MDAwMDAwWhcNMjAwNDIxMTIwMDAwWjBQMQswCQYDVQQGEwJMVjENMAsG
# A1UEBxMEUmlnYTEYMBYGA1UEChMPU3lzYWRtaW5zIExWIElLMRgwFgYDVQQDEw9T
# eXNhZG1pbnMgTFYgSUswggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCo
# NCCuzEogktL+1+lvPHu7ct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