json之使用 PowerShell 将 Chrome 书签导出到 CSV 文件

程序猿 阅读:135 2025-02-15 21:57:57 评论:0

我正在尝试使用 Powershell 将 Chrome 书签导出到 CSV/Excel。关于创建不正确的 JSON 文件,我遇到了障碍。这是由于 Chrome 中有两组名为 的文件夹。书签栏已同步 .

BookMark_Bar 创建一个带括号的 JSON,如下所示:

[ 
{ 
 
}, 
{ 
 
} 
] 

同步文件夹还在同一文件中创建一组数据,如下所示:
[ 
{ 
 
}, 
{ 
 
} 
] 

代码如下。
$isodate = Get-Date -Format yyyymmmdd_hhmmss 
$logFilePath = "d:\PowerShell_$isodate.log" 
# ############################ 
Start-Transcript -Path $logFilePath -Append 
######################################### 
 
#Declare Variables 
 
$pathToJsonFile = "$env:localappdata\Google\Chrome\User Data\Default\Bookmarks" 
#$JsonFilePath = "D:\04. PowerShell\BookMarks\ChromeBookMarx.json" 
#$OutputFilePath = "D:\BookMarx.csv"  
 
$data = Get-content $pathToJsonFile | out-string | ConvertFrom-Json 
 
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)" 
 
 
#A nested function to enumerate bookmark folders 
Function Get-BookmarkFolder { 
[cmdletbinding()] 
Param( 
[Parameter(Position=0,ValueFromPipeline=$True)] 
$Node 
) 
 
Process  
{ 
 
 foreach ($child in $node.children)  
 { 
   #get parent folder name 
   $parent = $node.Name 
   if ($child.type -eq 'Folder')  
   { 
     Write-Verbose "Processing $($child.Name)" 
     Get-BookmarkFolder $child 
   } 
   else  
   { 
        $hash= [ordered]@{ 
          Folder = $parent 
          Name = $child.name 
          URL = $child.url 
          Added = [datetime]::FromFileTime(([double]$child.Date_Added)*10) 
        } 
      #write custom object 
        New-Object -TypeName PSobject -Property $hash 
  } #else url 
 } #foreach 
 } #process 
} #end function 
 
#create a new JSON file 
$text | Set-Content 'file.json' 
$test1 = '{ 
  "markers":' | Add-Content 'file.json' 
 
#these should be the top level "folders" 
$data.roots.bookmark_bar | Get-BookmarkFolder |ConvertTo-Json |Add-Content 'D:\file.json' 
$data.roots.other | Get-BookmarkFolder  |ConvertTo-Json |Add-Content 'D:\file.json' 
$data.roots.synced | Get-BookmarkFolder  |ConvertTo-Json |Add-Content 'D:\file.json' 
 
$EndBraceClose = ']}' | Add-Content 'file.json' 
 
Get-Content 'D:\file.json' -Raw | 
ConvertFrom-Json | select -ExpandProperty markers| 
Export-CSV $env:USERPROFILE\desktop\ChromeBookMarx_$isodate.csv -NoTypeInformation 
 
    # end logging  
    ########################### 
    Stop-Transcript 
    ########################### 

JSON 文件的结果如下所示: -
{ 
  "BookMarks": 
[ 
    { 
        "Folder":  "Sanddance", 
        "Name":  "Microsoft Garage", 
        "URL":  "https://www.microsoft.com/en-us/garage/workbench/", 
        "Added":  "\/Date(1504585556230)\/" 
    }, 
    { 
        "Folder":  "Sanddance", 
        "Name":  "Microsoft has a new data visualization tool you can use for free", 
        "URL":  "https://thenextweb.com/", 
        "Added":  "\/Date(1504584050296)\/" 
    } 
] 
[ 
   { 
        "Folder":  "Amazon", 
        "Name":  "Cycling Equipment: Buy Cycling Accessories, Bike Tools \u0026 Equipment Online at Low Prices in India", 
        "URL":  "https://www.amazon.in/", 
        "Added":  "\/Date(1504859500323)\/" 
    }, 
 
    { 
        "Folder":  "Other bookmarks", 
        "Name":  "Zakir Khan-Fan Honge Dusron Ke Apne To Dost Hote Hai - YouTube", 
        "URL":  "https://www.youtube.com/watch?v=n4WKiwhn29o", 
        "Added":  "\/Date(1497416694729)\/" 
    } 
] 
] 
} 

第 16 行和第 17 行的括号似乎是导致问题的原因。我需要一些帮助来纠正编写的代码。

请您参考如下方法:

如果您查看 JSON 示例的基本结构,则没有 []
http://json.org/example.html

所以我稍微调整了你的脚本我的 chrome 路径也与你的不同,所以你必须改变它。

    $isodate = Get-Date -Format yyyymmmdd_hhmmss 
$file = "C:\temp\file.json" 
#Declare Variables 
 
$pathToJsonFile = "$env:localappdata\Google\Chrome\User Data\Profile 1\Bookmarks" 
#$JsonFilePath = "D:\04. PowerShell\BookMarks\ChromeBookMarx.json" 
#$OutputFilePath = "D:\BookMarx.csv"  
 
$data = Get-content $pathToJsonFile | out-string | ConvertFrom-Json 
 
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)" 
 
 
#A nested function to enumerate bookmark folders 
Function Get-BookmarkFolder { 
[cmdletbinding()] 
Param( 
[Parameter(Position=0,ValueFromPipeline=$True)] 
$Node 
) 
 
Process  
{ 
 
 foreach ($child in $node.children)  
 { 
   #get parent folder name 
   $parent = $node.Name 
   if ($child.type -eq 'Folder')  
   { 
     Write-Verbose "Processing $($child.Name)" 
     Get-BookmarkFolder $child 
   } 
   else  
   { 
        $hash= [ordered]@{ 
          Folder = $parent 
          Name = $child.name 
          URL = $child.url 
          Added = [datetime]::FromFileTime(([double]$child.Date_Added)*10) 
        } 
      #write custom object 
        New-Object -TypeName PSobject -Property $hash 
  } #else url 
 } #foreach 
 } #process 
} #end function 
 
#create a new JSON file 
$text | Set-Content $file -Force 
$test1 = '{ 
  "markers":' | Add-Content $file 
 
#these should be the top level "folders" 
$data.roots.bookmark_bar | Get-BookmarkFolder |ConvertTo-Json |Add-Content $file 
$data.roots.other | Get-BookmarkFolder  |ConvertTo-Json |Add-Content $file 
$data.roots.synced | Get-BookmarkFolder  |ConvertTo-Json |Add-Content $file 
 
 '}' | Add-Content $file 
 
Get-Content $file -Raw | 
ConvertFrom-Json | select -ExpandProperty markers| 
Export-CSV $env:USERPROFILE\desktop\ChromeBookMarx_$isodate.csv -NoTypeInformation 

这在我的桌面上为我提供了一个有效的 CSV 文件。有趣的想法顺便说一句。

所以这条线是我发现的最大问题:
$EndBraceClose = ']}' | Add-Content 'file.json' 


标签:Shell
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号