我正在尝试使用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
我正在以以下格式输出:
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
将输出保存为变量后,我无法设置其格式,我希望将其作为具有日期和网站的最近7天的历史记录:
Date : 2019-01-11 15:00:07 Site : https://www.mozilla.org/privacy/firefox/
答案 0 :(得分:0)
没有太多的工作涉及:
|
)符号下面的代码只是在换行符上分割文本,跳过前两个,然后对结果进行基本解析。
$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/
答案 1 :(得分:0)
Select-Object
-要跳过前两行,因为它们不是历史记录信息。
ConvertFrom-Csv
-使用管道synbol作为属性之间的分隔符,从每一行创建一个对象。
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)
输出:
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/ 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
然后您可以将其保存到文件中
$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
Date : 11/01/2019 15:00:07 Site : https://www.mozilla.org/privacy/firefox/ Date : 11/01/2019 15:00:07 Site : https://www.mozilla.org/en-US/privacy/firefox/ Date : 11/01/2019 15:02:28 Site : https://twitter.com/ Date : 12/01/2019 12:01:09 Site : https://twitter.com/ Date : 12/01/2019 11:36:28 Site : http://google.com/ Date : 12/01/2019 11:36:28 Site : http://www.google.com/ Date : 12/01/2019 11:36:28 Site : https://www.google.com/ Date : 12/01/2019 12:01:03 Site : https://www.amazon.com
答案 2 :(得分:0)
另一个选择是使用 SQLite 程序集从 sqlite 数据库中查询数据。
{
"payload": [
{
"id": 104,
"externalId": "e5b93978-0960-4c01-a201-b15c9e6afb49",
"shortName": "FRA",
"name": "France",
"countryCode": "en",
"leagueId": 1,
"logoUrl": "http://www.example.com/logo.png"
},
{
"id": 110,
"externalId": "7f1f76c1-483c-4c14-b5f9-61e58a5d9328",
"shortName": "Swi",
"name": "Switzerland",
"countryCode": "en",
"leagueId": 1,
"logoUrl": "http://www.example.com/logo.png"
},
{
"id": 2189,
"externalId": "8d23300c-85f4-41b4-b0f1-65e5948f2c80",
"shortName": "Cro",
"name": "Croatia",
"countryCode": "en",
"leagueId": 1,
"logoUrl": "http://www.sdfgsdf.com/image.png"
}
],
"errorCode": 0,
"errorTimeStamp": null,
"errorMessage": null,
"hasError": false
}
为了选择“地点”数据库,我使用了这个:
[Reflection.Assembly]::LoadFile("$($PSScriptRoot)\System.Data.SQLite.dll") | Out-Null
在 DB 文件上打开一个连接:
$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()