diff --git a/DuplicatePairing.sql b/DuplicatePairing.sql index 332be70..3d86b78 100644 --- a/DuplicatePairing.sql +++ b/DuplicatePairing.sql @@ -1,3 +1,5 @@ +DROP TABLE OfficeDuplicates; +Create Table OfficeDuplicates AS SELECT * from FolderData AS Origin INNER JOIN FolderData AS Dup ON Dup.Project = Origin.Project diff --git a/Get-ProjectFolders.ps1 b/Get-ProjectFolders.ps1 index f2bcbe7..783cf94 100644 --- a/Get-ProjectFolders.ps1 +++ b/Get-ProjectFolders.ps1 @@ -11,12 +11,19 @@ Output File should show: X:\CG | ProjectNumber | FullPath | filecount #> + + +$Refresh = $false #refresh all data - causes the DB to be wiped clean and all file folders to be re-analyzed. + + $PathToDB = "M:\IT\Egnyte\DuplicateFiles\WorkingRun\Dedupe.SQLite" +$OutputFolder = "M:\IT\Egnyte\DuplicateFiles\WorkingRun\Output" $BaseDrive = 'X:\' $ProjectRegex = '^X:\\[A-Z]{2}\W\d{2}\\\d{2}' $OfficeList = Get-ChildItem -Path $BaseDrive -Directory -Depth 0 $OfficeList | ForEach-Object -parallel { + $Refesh_Copy_Main = $USING:Refresh $PathToDB_Copy_Main = $USING:PathToDB $ServerProgress = @{ ID = 1 @@ -35,7 +42,7 @@ $OfficeList | ForEach-Object -parallel { # We need to get a file count for each "last" folder - IE, 001 in the example 1234-567-001. $ProjectFolders | ForEach-Object -ThrottleLimit 15 -Parallel { - # Write-Host "Processing " $_.FullName -ForegroundColor Cyan + $Refresh_Thread = $USING:Refresh_Copy_Main $PathToDB_Copy = $USING:PathToDB_Copy_Main $JobID = Get-Random @@ -47,6 +54,17 @@ $OfficeList | ForEach-Object -parallel { # Write-Progress @CurentLoopProgress + if ($Refresh_Thread) { + # Reset the DB, start fresh + $DBConnect = New-SqliteConnection -DataSource $PathToDB_Copy + $ClearQuery = 'DELETE FROM FolderData;' + Invoke-SqliteQuery -DataSource $PathToDB_Copy -Query $ClearQuery + + + } + + + $projectNumber = '' # thank you ChatGPT $string = $_.FullName @@ -81,7 +99,7 @@ $OfficeList | ForEach-Object -parallel { $Query = "Select Project FROM FolderData where Project = '$ProjectNumber'" $sql_result = Invoke-SqliteQuery -DataSource $PathToDB_Copy -Query $Query - if ($sql_result.project -eq $null) { + if (($sql_result.project -eq $null)) { $CurentLoopProgress = @{ ID = $JobID Activity = "Loading Telemetry for " + $_.FullName @@ -107,7 +125,7 @@ $OfficeList | ForEach-Object -parallel { $out = [PSCustomObject]@{ Server = $_.FullName.Substring(1, 4) -replace (':|\\', '') - Project = $ProjectNumber + Project = $ProjectNumber.ToString() Parent = $folder_Parent Path = $_.FullName FileCount = $folder_FileCount @@ -125,4 +143,49 @@ $OfficeList | ForEach-Object -parallel { Write-Progress @CurentLoopProgress -Completed } Write-Progress @ServerProgress -Completed -} -ThrottleLimit 5 \ No newline at end of file +} -ThrottleLimit 5 + +$PathToDB_Copy_Main = $USING:PathToDB +$ServerProgress = @{ + ID = 1 + Activity = "Database Work" + PercentComplete = 80 + # CurrentOperation = $_.Project +} +Write-Progress @ServerProgress + + +## Kick off GenTables + +$SQLFile_to_Run = $PSScriptRoot + '\GenTables.sql' +Invoke-SqliteQuery -DataSource $PathToDB -InputFile $SQLFile_to_Run +$SQLFile_to_Run = $PSScriptRoot + '\DuplicatePairing.sql' +Invoke-SqliteQuery -DataSource $PathToDB -InputFile $SQLFile_to_Run + +$DBConnect = New-SqliteConnection -DataSource $PathToDB + +foreach ($office in $OfficeList) { + $OfficeServer = $office.Substring(1, 4) -replace (':|\\', '') + + $Query = "select * from OfficeDuplicates where Server = '$OfficeServer'" + $sql_result = Invoke-SqliteQuery -DataSource $PathToDB -Query $Query + + if ($sql_result) { + $OutFile = $OutputFolder + "\$OfficeServer Duplicates.xlsx" + $sql_result | Export-Excel -Path $OutFile -AutoFilter + + } +} +# Generate MPE All Xlsx +$Query = "select * from OfficeDuplicates" +$sql_result = Invoke-SqliteQuery -DataSource $PathToDB -Query $Query + +if ($sql_result) { + $OutFile = $OutputFolder + "\MPE All Duplicates.xlsx" + $sql_result | Export-Excel -Path $OutFile -AutoFilter + +} + +$DBConnect.Close() + +