-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitwardenDuplicateCheck.ps1
More file actions
80 lines (66 loc) · 2.35 KB
/
bitwardenDuplicateCheck.ps1
File metadata and controls
80 lines (66 loc) · 2.35 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
Write-Host "Please drag and drop the Bitwarden JSON export file here, then press Enter:"
$jsonFilePath = Read-Host
# Remove quotes if added by the drag-and-drop operation
$jsonFilePath = $jsonFilePath -replace '"',''
# Check if the file exists
if (-not (Test-Path -Path $jsonFilePath)) {
Write-Host "File not found. Please ensure the file path is correct."
exit
}
# Read and parse the JSON file
try {
$jsonData = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json
} catch {
Write-Host "Failed to read or parse the JSON file. Please ensure the file is a valid JSON format."
exit
}
# Create a hashtable to track duplicates and a list for unique items
$duplicateTracker = @{}
$uniqueItems = @()
$duplicateEntries = 0
# Iterate over each item in the JSON data
foreach ($item in $jsonData.items) {
# Extract item name, URI, username, and password
$itemName = $item.name
$itemUsername = $null
$itemPassword = $null
if ($item.login) {
$itemUsername = $item.login.username
$itemPassword = $item.login.password
}
# Create a unique key based on item name, username, and password
$uniqueKey = "$itemName|$itemUsername|$itemPassword"
# Check if this key already exists in the hashtable
if ($duplicateTracker.ContainsKey($uniqueKey)) {
$duplicateTracker[$uniqueKey] += 1
} else {
$duplicateTracker.Add($uniqueKey, 1)
$uniqueItems += $item
}
}
# Display the duplicates
foreach ($key in $duplicateTracker.Keys) {
if ($duplicateTracker[$key] -gt 1) {
Write-Host "Duplicate found: $key (Count: $($duplicateTracker[$key]))"
$duplicateEntries += 1
}
}
if ($duplicateEntries -eq 0) {
Write-Host "Congratulations! No duplicates found."
exit
} else {
Write-Host "Do you want to remove the duplicates? (Y/N)"
$removeDuplicates = Read-Host
}
if ($removeDuplicates -like "Y") {
# Update JSON data with unique items only
$jsonData.items = $uniqueItems
# Define the new file path
$newFilePath = [System.IO.Path]::ChangeExtension($jsonFilePath, ".nodupes.json")
# Save the updated JSON data to a new file
$jsonData | ConvertTo-Json -Depth 100 | Set-Content -Path $newFilePath
Write-Host "Duplicates removed. New file saved as: $newFilePath"
} else {
Write-Host "No changes made to the original file."
}
Write-Host "Process complete."