-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-export.ps1
executable file
·80 lines (68 loc) · 3.76 KB
/
10-export.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
#!/usr/bin/env pwsh
# I implement this script with latest Powershell available an do not intend to test older ones. It may or may be not working for Powershell 5.
#Requires -Version 7.0
# https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.core/about/about_character_encoding?view=powershell-7.4
# ensure the output is UTF8
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
. ./00-common.ps1
function New-ExportFolder {
$exportFolderName = "export-$(Get-Date -Format "yyyy-MM-dd-HH-mm-ss")"
$exportDirectory = Join-Path -Path (Get-Location) -ChildPath $exportFolderName
New-Item -Path $exportDirectory -ItemType Directory
}
# check bitwarden unlocked
if ( Get-BitwardenStatusLocked ) {
Write-Error "Bitwarden is not unlocked! Please unlock your Bitwarden CLI."
Pause
return 1
}
# create the export directory
$exportDirectory = New-ExportFolder
# Read data from Bitwarden
## Export private password data
$exportFile = Join-Path -Path $exportDirectory -ChildPath "export-private.json"
$null = Invoke-Expression -Command "bw export --format json --output `"$exportFile`" "
$bitwardenContent = Get-Content -Path $exportFile | ConvertFrom-Json -Depth 10
Write-Output "Exported $($bitwardenContent.items.Count) items from private vault."
## Export organization listing
$exportFile = Join-Path -Path $exportDirectory -ChildPath "export-list-organizations.json"
bw list organizations > $exportFile
$bitwardenOrganizations = Get-Content $exportFile | ConvertFrom-Json -Depth 2
Write-Output "Exported $($bitwardenOrganizations.Count) organizations total."
## Export folder listing
$exportFile = Join-Path -Path $exportDirectory -ChildPath "export-list-folders.json"
bw list folders > $exportFile
$BitwardenFolders = Get-Content $exportFile | ConvertFrom-Json -Depth 2
Write-Output "Exported $($BitwardenFolders.Count) folders total."
## Export items
$exportFile = Join-Path -Path $exportDirectory -ChildPath "export-list-items.json"
bw list items > $exportFile
$BitwardenItems = Get-Content $exportFile | ConvertFrom-Json -Depth 10
$BitwardenItemsCount = $BitwardenItems.Count
Write-Output "Exported $BitwardenItemsCount items total."
## export organization password data
foreach ($organization in $bitwardenOrganizations) {
$exportFile = Join-Path -Path $exportDirectory -ChildPath "export-$($organization.id).json"
$null = Invoke-Expression -Command "bw export --format json --organizationid $($organization.id) --output `"$exportFile`" "
$bitwardenContent = Get-Content -Path $exportFile | ConvertFrom-Json -Depth 10
Write-Output "Exported $($bitwardenContent.items.Count) items from organization $($organization.name) vault."
}
Remove-Variable bitwardenContent
## export attachments
$counter = 0
foreach ($bitwardenElement in $BitwardenItems) {
$counter++
Write-Progress -Activity "Download Attachments" -Status "processing $counter item of $BitwardenItemsCount items total" -Id 0 -PercentComplete ($counter / $BitwardenItemsCount * 100)
$itemDirectory = Join-Path -Path $exportDirectory -ChildPath ($bitwardenElement.id)
if (Get-Member -InputObject $bitwardenElement -Name "attachments") {
Write-Debug "The element $($bitwardenElement.name) has $($bitwardenElement.attachments.Count) attachments."
$null = New-Item -Path $itemDirectory -ItemType Directory
foreach ($attachment in $bitwardenElement.attachments) {
Write-Debug "Found in entry $($bitwardenElement.name) attachment $($attachment.fileName) "
$attachmentPath = Join-Path $itemDirectory ($attachment.fileName)
$null = Invoke-Expression "bw get attachment $($attachment.id) --itemid $($bitwardenElement.id) --output `"$attachmentPath`"" --
}
}
}
Write-Progress -Id 0 -Completed
Write-Output "Processed Downloads from all elements."