使用 PowerShell 将 Chrome 和 Edge Chromium 中的书签 JSON 文件合并到一个文件中?

Posted

技术标签:

【中文标题】使用 PowerShell 将 Chrome 和 Edge Chromium 中的书签 JSON 文件合并到一个文件中?【英文标题】:Combining the bookmark JSON files from Chome and Edge Chromium into one file using PowerShell? 【发布时间】:2022-01-03 22:00:56 【问题描述】:

我们正在从 Chrome 迁移到 Edge Chromium(当微软也从 IE 11 将最终钉子钉入 IE 11 棺材时)。这是在更换 PC 时完成的。

我们正在使用 USMT 复制用户文件。所以旧电脑可能有也可能没有 Chrome,但如果有,我想复制书签文件。如果它也安装了 Edge(大多数都安装了),我也想复制这些书签。恢复 USMT 数据时,我想在我们的恢复脚本中添加一个步骤,将两个书签文件的结果合并到一个 JSON 文件中,并将其导入 Edge Chromium(我想我可以直接添加“书签”文件进入“C:\Users$UserID\AppData\Local\Microsoft\Edge\User Data\Default”)。

我在脚本的其他部分调用了这三个函数:

Function Backup-ChromeBookmarks 
  $pathToChromeJsonFile = -join("C:\Users\", $UserID, "\AppData\local\Google\Chrome\User Data\Default\Bookmarks")
  $global:chromeBookmarkExists = Test-Path $pathToChromeJsonFile
  if ($chromeBookmarkExists -eq $true)
    Copy-Item -Path $pathToChromeJsonFile -Destination $BackupStore/Bookmarks-chrome.json
  


Function Backup-EdgeBookmarks 
  $pathToEdgeJsonFile = -join("C:\Users\", $UserID, "\AppData\Local\Microsoft\Edge\User Data\Default\Bookmarks")
  $global:edgeBookmarkExists = Test-Path $pathToEdgeJsonFile
  if ($edgeBookmarkExists -eq $true)
    Copy-Item -Path $pathToEdgeJsonFile -Destination $BackupStore/Bookmarks-edge.json

  


Function Combine-Bookmarks 
  # referenced https://www.jonathanmedd.net/2020/04/combine-two-json-files-with-powershell.html
  #https://web.archive.org/web/20200517182000/https://www.jonathanmedd.net/2020/04/combine-two-json-files-with-powershell.html
  if($chromeBookmarkExists -eq $true -And $edgeBookmarkExists -eq $true)

    $data1 = Get-Content $BackupStore/Bookmarks-chrome.json -Raw | ConvertFrom-Json
    $data2 = Get-Content $BackupStore/Bookmarks-edge.json -Raw | ConvertFrom-Json
  
   
    
    $data1.psobject.Properties | ForEach-Object 
      $data2 | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value -Force
       
    
    @($data2) | ConvertTo-Json | Out-File $BackupStore/Bookmarks
  

它可以从 Chrome 和 Edge 复制“书签”文件,但是当它结合它们并写入文件时,所有内容都是:

    "checksum":  "0f9bf8e97b9ac6cd3654c15c673b8cb8",
    "roots":  
                  "bookmark_bar":  
                                       "children":  "                                                                              ",
                                       "date_added":  "13252082948039886",
                                       "date_modified":  "13268156238031255",
                                       "guid":  "0bc5d13f-2cba-5d74-951f-3f233fe6c908",
                                       "id":  "1",
                                       "name":  "Bookmarks bar",
                                       "type":  "folder"
                                   ,
                  "other":  
                                "children":  "",
                                "date_added":  "13252082948040297",
                                "date_modified":  "0",
                                "guid":  "82b081ec-3dd3-529c-8475-ab6c344590dd",
                                "id":  "2",
                                "name":  "Other bookmarks",
                                "type":  "folder"
                            ,
                  "synced":  
                                 "children":  "",
                                 "date_added":  "13252082948040301",
                                 "date_modified":  "0",
                                 "guid":  "4cf2e351-0e85-532b-bb37-df045d8f8d0f",
                                 "id":  "3",
                                 "name":  "Mobile bookmarks",
                                 "type":  "folder"
                             
              ,
    "version":  1

有人知道我在合并两个 JSON 文件时做错了什么吗?我对此没有太多经验,如果有任何反馈,我将不胜感激。

如果我缺少任何信息,请告诉我。谢谢!

以下是来自 Chrome 和 Edge 的一些随机书签列表,用于示例数据: 铬

   "checksum": "0e3450f30154cec188275de0e1eed2a5",
   "roots": 
      "bookmark_bar": 
         "children": [ 
            "date_added": "13282342589274138",
            "guid": "dff3def7-9693-443c-a44c-2b1047687b33",
            "id": "5",
            "name": "Google News",
            "type": "url",
            "url": "https://news.google.com/topstories?hl=en-CA&gl=CA&ceid=CA:en"
         , 
            "children": [ 
               "date_added": "13282342602339055",
               "guid": "3a6114ed-d13a-491e-a049-d47a174a349b",
               "id": "7",
               "name": "reddit - Google Search",
               "type": "url",
               "url": "https://www.google.com/search?q=reddit&oq=reddit&aqs=chrome..69i57j46i131i199i433i465i512j0i131i433i512l3j0i131i433j0i131i433i512l3.1205j0j7&sourceid=chrome&ie=UTF-8"
            , 
               "date_added": "13282342613505854",
               "guid": "0da478c1-a91b-4af9-80db-cae8dc6927c0",
               "id": "8",
               "name": "Reddit - Dive into anything",
               "type": "url",
               "url": "https://www.reddit.com/"
            , 
               "date_added": "13282342620306591",
               "guid": "b0311e2e-a41f-47fa-a1ef-214dd3964729",
               "id": "9",
               "name": "Facebook - Log In or Sign Up",
               "type": "url",
               "url": "https://www.facebook.com/"
            , 
               "date_added": "13282342631549806",
               "guid": "a06637af-50be-458e-95bc-730ffc1cda36",
               "id": "10",
               "name": "Instagram",
               "type": "url",
               "url": "https://www.instagram.com/?hl=en"
             ],
            "date_added": "13282342597429656",
            "date_modified": "13282342644382518",
            "guid": "a5b18126-11f7-40df-809f-2d96f311f1f6",
            "id": "6",
            "name": "Test Folder",
            "type": "folder"
         , 
            "date_added": "13282342644382518",
            "guid": "39ef0ac5-b70c-406e-acea-96c3f90935ef",
            "id": "11",
            "name": "Dogpile.com",
            "type": "url",
            "url": "https://www.dogpile.com/"
         , 
            "date_added": "13282342655028948",
            "guid": "d152f027-2aae-47b1-bae6-f9d7e617b639",
            "id": "12",
            "name": "Ask.com - What's Your Question?",
            "type": "url",
            "url": "https://www.ask.com/"
          ],
         "date_added": "13282342511050735",
         "date_modified": "13282342655028948",
         "guid": "0bc5d13f-2cba-5d74-951f-3f233fe6c908",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      ,
      "other": 
         "children": [  ],
         "date_added": "13282342511050739",
         "date_modified": "0",
         "guid": "82b081ec-3dd3-529c-8475-ab6c344590dd",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      ,
      "synced": 
         "children": [  ],
         "date_added": "13282342511050740",
         "date_modified": "0",
         "guid": "4cf2e351-0e85-532b-bb37-df045d8f8d0f",
         "id": "3",
         "name": "Mobile bookmarks",
         "type": "folder"
      
   ,
   "version": 1

边缘


   "checksum": "c26640ae81258f8bb73935ccd2bb1e91",
   "roots": 
      "bookmark_bar": 
         "children": [ 
            "date_added": "13282342518391441",
            "guid": "00f514ec-8d15-45b1-9c26-ba258d59c688",
            "id": "9",
            "name": "Google News",
            "show_icon": false,
            "source": "user_add",
            "type": "url",
            "url": "https://news.google.com/topstories?hl=en-CA&gl=CA&ceid=CA:en"
         , 
            "date_added": "13282342527623442",
            "guid": "f690f411-6f29-4e22-bee4-d57b225a9287",
            "id": "10",
            "name": "Stack Overflow - Where Developers Learn, Share, & Build Careers",
            "show_icon": false,
            "source": "user_add",
            "type": "url",
            "url": "https://***.com/"
         , 
            "children": [ 
               "date_added": "13282342544239807",
               "guid": "bf6d87ae-9cd1-4d02-b2de-5083e9b487f1",
               "id": "12",
               "name": "Amazon.ca: Low Prices – Fast Shipping – Millions of Items",
               "show_icon": false,
               "source": "user_add",
               "type": "url",
               "url": "https://www.amazon.ca/"
            , 
               "date_added": "13282342551951302",
               "guid": "d504da5d-4217-4a79-998c-f3dd5b0c95e0",
               "id": "13",
               "name": "Best Buy: Shop Online For Deals & Save | Best Buy Canada",
               "show_icon": false,
               "source": "user_add",
               "type": "url",
               "url": "https://www.bestbuy.ca/en-ca"
            , 
               "date_added": "13282342564797804",
               "guid": "cad2d5d7-c72e-477e-854a-d58df397574c",
               "id": "15",
               "name": "Laptops, Desktops, Tablets, Computer Components, Printers, TVs, Video Games & Appliances - Canada Computers & Electronics",
               "show_icon": false,
               "source": "user_add",
               "type": "url",
               "url": "https://www.canadacomputers.com/"
             ],
            "date_added": "13282342538649786",
            "date_modified": "13282342564797804",
            "guid": "3616a33e-7bf6-47de-8142-673cc19f6d64",
            "id": "11",
            "name": "Shopping",
            "source": "unknown",
            "type": "folder"
          ],
         "date_added": "13282342417969383",
         "date_modified": "13282342544239807",
         "guid": "0bc5d13f-2cba-5d74-951f-3f233fe6c908",
         "id": "1",
         "name": "Favorites bar",
         "source": "unknown",
         "type": "folder"
      ,
      "other": 
         "children": [  ],
         "date_added": "13282342417969428",
         "date_modified": "0",
         "guid": "82b081ec-3dd3-529c-8475-ab6c344590dd",
         "id": "2",
         "name": "Other favorites",
         "source": "unknown",
         "type": "folder"
      ,
      "synced": 
         "children": [  ],
         "date_added": "13282342417969430",
         "date_modified": "0",
         "guid": "4cf2e351-0e85-532b-bb37-df045d8f8d0f",
         "id": "3",
         "name": "Mobile favorites",
         "source": "unknown",
         "type": "folder"
      
   ,
   "version": 1


【问题讨论】:

不应该是@($data2) | ConvertTo-Json吗?在Add-Member 行中,您将属性从data1 添加到data2,因此后者将包含合并的数据。 谢谢@zett42,很好。我尝试了该建议,但结果不包括这两个文件。我得到完全相同的输出文件。仅供参考,我对上面的代码进行了编辑,因为我的 $pathToEdgeJsonFile 和 $pathToChromeJsonFile 变量都引用了 Chrome 书签 JSON 文件。 还请更正@zett42 在您的问题代码中指出的逻辑错误。请注意,ConvertTo-Json - 不幸的是 - 默认情况下将序列化深度限制为 2 个子级别,这可能导致数据安静截断。尝试添加,例如,-Depth 10。另外,两个输入 JSON 文件是否真的每个都只包含 一个 对象? 再次查看您的代码后,您只合并了 JSON 的***属性(“校验和”、“根”和“版本”)。所以$data2 将被$data1 完全覆盖。正确合并文件需要递归。您可以添加展示嵌套结构的输入数据样本吗? 在上面添加了一些示例。这只有书签栏文件夹,其下方有 1 级文件夹,但大概用户可以将更多书签埋得更深。这两个文件都有很多很多书签(比我上面的示例还多)——不仅仅是一个对象。我更正了其他逻辑错误并添加了一些示例数据。我将尝试使用 -Depth 10 参数,看看它是否有所作为。 【参考方案1】:

正如@zetta42 指出的那样,您可能只需要添加更多深度:

@($data2) | ConvertTo-Json -Depth 10 | Out-File $BackupStore/Bookmarks

以及覆盖roots 属性(实际上并未合并)。

但是,从用户体验来看,您可能需要考虑其他更新。现在如果这两个包含相同name 的书签,您的预期 合并逻辑将用另一个覆盖一个。用户(您)可能会更高兴知道给定书签来自哪个浏览器,并且如果不需要,可以轻松地从新浏览器中重新组织(或者,因为我不熟悉它,希望它这样做)。所以我建议抓住这两个书签并将它们放在新书签中自己的文件夹下。类似于以下内容:

Function Combine-Bookmarks 
  # referenced https://www.jonathanmedd.net/2020/04/combine-two-json-files-with-powershell.html
  #https://web.archive.org/web/20200517182000/https://www.jonathanmedd.net/2020/04/combine-two-json-files-with-powershell.html
  if($chromeBookmarkExists -eq $true -And $edgeBookmarkExists -eq $true)

    $data1 = Get-Content $BackupStore/Bookmarks-chrome.json -Raw | ConvertFrom-Json
    $data2 = Get-Content $BackupStore/Bookmarks-edge.json -Raw | ConvertFrom-Json

    $dataNew = @
      'roots'=@'Chrome'=$data1.Roots; 'IE'=$data2.Roots
      #whatever the structure of the new json file should be
    
    $dataNew | ConvertTo-Json -Depth 10 | Out-File $BackupStore/Bookmarks_new.json
  

如果他们没有该浏览器,您也可以将文件夹留空,或者您可以将其从$dataNew 中排除(通过使用一些我不记得的不同语法)。现在它只会在两者都存在的情况下尝试合并。

【讨论】:

我真的很喜欢将它们放在两个单独的文件夹中的想法,这样他们就知道它们来自哪个浏览器。当我尝试你的第二个代码块时,我得到了这个“null”输出“roots”:“Chrome”:null,“IE”:null知道我在那里缺少什么吗? 嗯...我没有机会测试我的代码,对此感到抱歉。假设您将代码放在脚本的正确部分,我想我误解了json 文件的结构。也许是$data1.value.roots$data1.roots.bookmark_bar.children 什么的;树路径取决于文件的树结构以及您要抓取的值。 您的json 示例中有一条线被切断... 当我将它与我的原始文件(Chrome 书签:rentry.co/pye46 Edge 书签:rentry.co/c5ixu)进行比较时,我没有看到任何被截断的行。我真的对 JSON 不太熟悉。这个格式可能不符合 JSON 标准吗?编辑:对不起,我的回复延迟了,同时我还有其他更紧急的工作弹出 啊,起始大括号 () 没有为我渲染。该文件看起来不错,我用您的rentry.co/pye46 的内容手动进行了冒烟测试,一切正常,就像我拥有它一样。也许$BackupStore 没有填充?我现在不确定。

以上是关于使用 PowerShell 将 Chrome 和 Edge Chromium 中的书签 JSON 文件合并到一个文件中?的主要内容,如果未能解决你的问题,请参考以下文章

使用 Powershell 静默卸载 Chrome

text 从Powershell安装Google Chrome

powershell 安装Chrome

如何使用 PowerShell 或 C# 将网页保存到 HTML 文件中?

使用 PowerShell 和 Selenium 4 查找元素

如何在没有错误消息或异常的情况下调试 PowerShell 进程