【问题标题】:Retreive Mozilla Firefox browser history with powershell使用 powershell 检索 Mozilla Firefox 浏览器历史记录
【发布时间】:2019-06-09 09:57:59
【问题描述】:

我正在尝试使用 powershell 从 places.sqlite 获取 Mozilla Firefox 浏览器历史记录。

这是我正在运行的查询,$mozillapath 变量包含places.sqlite 文件的位置。

".open $mozillapath
 SELECT datetime(moz_historyvisits.visit_date/1000000,'unixepoch'), moz_places.url FROM moz_places, moz_historyvisits WHERE moz_places.id = moz_historyvisits.place_id
" | C:\Users\Admin\sqlite-tools-win32-x86-3260000\sqlite-tools-win32-x86-3260000\sqlite3.exe

我得到以下格式的输出:

用户名:管理员 C:\\Users\\Admin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\nr0o1s57.default\\places.sqlite 2019-01-11 15:00:07|https://www.mozilla.org/privacy/firefox/ 2019-01-11 15:00:07|https://www.mozilla.org/en-US/privacy/firefox/ 2019-01-11 15:02:28|https://twitter.com/ 2019-01-12 12:01:09|https://twitter.com/ 2019-01-12 11:36:28|http://google.com/ 2019-01-12 11:36:28|http://www.google.com/ 2019-01-12 11:36:28|https://www.google.com/ 2019-01-12 12:01:03|https://www.amazon.com

在变量中保存输出后,我无法对其进行格式化,我希望将其作为最近 7 天的历史记录,包含日期和网站:

日期:2019-01-11 15:00:07 网站:https://www.mozilla.org/privacy/firefox/

【问题讨论】:

  • 到目前为止,您尝试过什么格式的输出?始终包含您的代码,即使它不起作用。这让我们了解您想要做什么。
  • 变量中的输出是单个字符串还是字符串数组?

标签: sqlite powershell firefox


【解决方案1】:

Select-Object - 跳过前两行,因为它们不是历史信息。

ConvertFrom-Csv - 从每一行创建一个对象,使用管道符号作为属性之间的分隔符。

Select-Object - 只需使用计算属性将 Date 从字符串转换为 datetime,这样您就可以使用日期计算来仅获取最后 7 天。

Select-Object -Skip 2 |
    ConvertFrom-Csv -Delimiter '|' -Header 'Date','Site' |
    Select-Object -Property @{Name = 'Date'; Expression = {[datetime]$_.Date}},Site

有一些测试数据:

$output = @"
Username : Admin
C:\\Users\\Admin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\nr0o1s57.default\\places.sqlite
2019-01-11 15:00:07|https://www.mozilla.org/privacy/firefox/
2019-01-11 15:00:07|https://www.mozilla.org/en-US/privacy/firefox/
2019-01-11 15:02:28|https://twitter.com/
2019-01-12 12:01:09|https://twitter.com/
2019-01-12 11:36:28|http://google.com/
2019-01-12 11:36:28|http://www.google.com/
2019-01-12 11:36:28|https://www.google.com/
2019-01-12 12:01:03|https://www.amazon.com
2019-01-01 12:01:03|https://www.stackoverflow.com
2019-01-02 12:01:03|https://www.superuser.com
"@

# not be needed if your output is an array of strings
$output = $output.Split([Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries)

# create object from output
$history = $output | Select-Object -Skip 2 | ConvertFrom-Csv -Delimiter '|' -Header 'Date','Site' | Select-Object -Property @{Name = 'Date'; Expression = {[datetime]$_.Date}},Site

# get only last 7 days
$history | Where-Object -Property Date -GT (Get-Date).AddDays(-7)

输出:

日期网站 ---- ---- 11/01/2019 15:00:07 https://www.mozilla.org/privacy/firefox/ 11/01/2019 15:00:07 https://www.mozilla.org/en-US/privacy/firefox/ 11/01/2019 15:02:28 https://twitter.com/ 12/01/2019 12:01:09 https://twitter.com/ 2019 年 12 月 1 日 11:36:28 http://google.com/ 2019 年 12 月 1 日 11:36:28 http://www.google.com/ 12/01/2019 11:36:28 https://www.google.com/ 12/01/2019 12:01:03 https://www.amazon.com

然后您可以将其保存到文件中:

$history | Where-Object -Property Date -GT (Get-Date).AddDays(-7) | Out-File history.txt

或使用Format-List 将其显示为列表(然后使用 Out-File 将其保存为文件):

$history | Where-Object -Property Date -GT (Get-Date).AddDays(-7) | Format-List
日期:11/01/2019 15:00:07 网站:https://www.mozilla.org/privacy/firefox/ 日期:11/01/2019 15:00:07 网站:https://www.mozilla.org/en-US/privacy/firefox/ 日期:11/01/2019 15:02:28 网站:https://twitter.com/ 日期 : 12/01/2019 12:01:09 网站:https://twitter.com/ 日期:12/01/2019 11:36:28 网站:http://google.com/ 日期:12/01/2019 11:36:28 网站:http://www.google.com/ 日期:12/01/2019 11:36:28 网站:https://www.google.com/ 日期:12/01/2019 12:01:03 网站:https://www.amazon.com

【讨论】:

    【解决方案2】:

    提供的工作并不多:

    • 前导信息总是只有两行
    • 除分隔符外,文本中没有条形 (|) 符号

    以下代码只是将文本拆分为换行符,跳过前两个,然后对结果进行基本解析。

    $Str = "Username : Admin
    C:\\Users\\Admin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\nr0o1s57.default\\places.sqlite
    2019-01-11 15:00:07|https://www.mozilla.org/privacy/firefox/
    2019-01-11 15:00:07|https://www.mozilla.org/en-US/privacy/firefox/
    2019-01-11 15:02:28|https://twitter.com/
    2019-01-12 12:01:09|https://twitter.com/"
    
    $Str -split '[\r\n]+' | Select -Skip 2 | % {
        $D,$S = $_ -split '\|'
        [PSCustomObject]@{
            Date = [DateTime]::ParseExact($D,'yyyy-MM-dd HH:mm:ss',[CultureInfo]::InvariantCulture)
            Site = $S
        }
    }
    

    你会得到这样的东西。

    Date                Site                                          
    ----                ----                                          
    11/01/2019 15:00:07 https://www.mozilla.org/privacy/firefox/      
    11/01/2019 15:00:07 https://www.mozilla.org/en-US/privacy/firefox/
    11/01/2019 15:02:28 https://twitter.com/                          
    12/01/2019 12:01:09 https://twitter.com/      
    

    【讨论】:

      【解决方案3】:

      另一个选择是使用 SQLite 程序集从 sqlite 数据库中查询数据。

      [Reflection.Assembly]::LoadFile("$($PSScriptRoot)\System.Data.SQLite.dll") | Out-Null
      

      为了选择“地点”数据库,我使用的是这个:

      $Path = "$Env:systemdrive\Users\$ENV:USERNAME\AppData\Roaming\Mozilla\Firefox\Profiles\*places.sqlite"
      $HistFile = Get-ChildItem $Path -Recurse | Sort-Object -Descending -Property Length | Select-Object FullName -First 1
      

      在数据库文件上打开一个连接:

      $conn = New-Object -TypeName System.Data.SQLite.SQLiteConnection
      $conn.ConnectionString = "Data Source=$($HistFile.FullName)"
      $conn.Open()
      

      选择结果并将结果输出到网格:

      $channelList = New-Object System.Collections.ArrayList
      
      $cmd = $conn.CreateCommand()
      $cmd.CommandText = "SELECT datetime(moz_historyvisits.visit_date/1000000,'unixepoch') AS VisitDate, moz_places.url FROM moz_places, moz_historyvisits WHERE moz_places.id = moz_historyvisits.place_id"
      $sqlite_datareader = $cmd.ExecuteReader();
      while ($sqlite_datareader.Read()) {
          $dataRow = [pscustomobject]@{
              Url          = $sqlite_datareader["url"].ToString()
              VisitDate    = [datetime]$sqlite_datareader["VisitDate"]
          }
          $channelList.Add($dataRow) | Out-Null
      }
      
      $channelList | Out-GridView
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-14
        • 2013-01-31
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 2012-07-22
        • 1970-01-01
        • 2017-10-20
        相关资源
        最近更新 更多