【问题标题】:how to delete all google chrome cookies using powershell如何使用 powershell 删除所有 google chrome cookie
【发布时间】:2018-07-26 23:06:26
【问题描述】:

首先,我没有得到 google chrome 存储 cookie 的特定文件。 其次,我可能认为它存储在google\chrome\User Data\Default 在这个路径中我找到了cookie 名称文件。我删除了该文件,但谷歌浏览器仍然在他们的浏览器中包含 cookie。 那么,使用Powershell删除cookie文件的方法是什么,这样它就不会在浏览器中显示相同的cookie了。

【问题讨论】:

  • 一种方法是使用 Selenium,正如 C# sample 所示。你为什么要删除cookies?根据根本需求,也许有更好的方法。
  • 感谢您的回复。完全同意你的建议。但我需要使用 PowerShell。有什么办法可以做到吗?
  • 为什么只使用 Powershell?为什么提议的解决方案太长了?似乎有隐藏的限制,这在很大程度上暗示了XY problem。请编辑您的问题和更多详细信息。

标签: powershell google-chrome cookies


【解决方案1】:

好的,这是我清除 Chrome 历史记录(包括 cookie)的尝试

function Clear-ChromeHistory {
    <#
    .SYNOPSIS
        Removes Google Chrome history entries.
    .DESCRIPTION
        Removes Google Chrome history entries.
    .NOTES
        Author: Theo Ros
    .PARAMETER DaysToKeep
        Specifies the number of days to keep history. Everything older that
        the given number of days (as seen from now) will be removed.
        Defaults to 7
    .PARAMETER Recommended
        This is a shorthand switch. When set, all ArchivedHistory, BrowsingHistory,
        Cookies, Favicons, MediaData and TemporaryFiles will be cleared.
        FormData, Passwords and TopSites will be left alone.
    .PARAMETER All
        This is a shorthand switch. When set, ALL history items will be cleared.
    .PARAMETER ArchivedHistory
        When set, Archived History (BrowsingHistory older than 90 days) will be removed.
    .PARAMETER BrowsingHistory
        When set, History and History-journal and Visited Links will be removed.
    .PARAMETER Cookies
        When set, Cookies and Cookies-journal will be removed.
    .PARAMETER Favicons
        When set, Favicons and Favicons-journal will be removed.
    .PARAMETER Passwords
        When set, Login Data and Login Data-journal will be removed.
    .PARAMETER MediaData
        When set, the Media Cache will be emptied.
    .PARAMETER TemporaryFiles
        When set, the Temporary Cache will be emptied.
    .PARAMETER TopSites
        When set, Top Sites and Top Sites-journal will be removed.
    .PARAMETER FormData
        When set, Web Data en Web Data-journal (among others Autocomplete) will be removed.
    .EXAMPLE
        Clear-ChromeHistory -Recommended -DaysToKeep 14
        Will remove all Recommended items older than 14 days.
    .EXAMPLE
        Clear-ChromeHistory -Cookies -DaysToKeep 0
        Will remove all cookies.
    #>
    [CmdletBinding(DefaultParameterSetName = 'Recommended', ConfirmImpact = 'None')]
    param (
        [Parameter(Mandatory = $false, Position = 0)]
        [int] $DaysToKeep = 7,

        [Parameter(ParameterSetName='Recommended')]
        [switch] $Recommended,

        [Parameter(ParameterSetName='ByAll')]
        [switch] $All,

        [Parameter(ParameterSetName='ByItem')]
        [switch] $ArchivedHistory,  # Archived History (BrowsingHistory older than 90 days)
        [Parameter(ParameterSetName='ByItem')]
        [switch] $BrowsingHistory,  # file: History and History-journal and Visited Links
        [Parameter(ParameterSetName='ByItem')]
        [switch] $Cookies,          # file: Cookies and Cookies-journal
        [Parameter(ParameterSetName='ByItem')]
        [switch] $Favicons,         # file: Favicons and Favicons-journal
        [Parameter(ParameterSetName='ByItem')]
        [switch] $Passwords,        # file: Login Data and Login Data-journal
        [Parameter(ParameterSetName='ByItem')]
        [switch] $MediaData,        # folder: Media Cache
        [Parameter(ParameterSetName='ByItem')]
        [switch] $TemporaryFiles,   # folder: Cache
        [Parameter(ParameterSetName='ByItem')]
        [switch] $TopSites,         # file: Top Sites and Top Sites-journal
        [Parameter(ParameterSetName='ByItem')]
        [switch] $FormData          # file: Web Data en Web Data-journal (among others Autocomplete)
    )

    if (Get-Process -Name chrome -ErrorAction SilentlyContinue) {
        Write-Warning ("Chrome process(es) are still running. Please close all before clearing the history.`r`n" +
                       "Use Get-Process -Name chrome -ErrorAction SilentlyContinue | Stop-Process -ErrorAction SilentlyContinue")
        return
    }

    $path = "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Default"
    if (!(Test-Path -Path $path -PathType Container)) {
        Write-Error "Chrome history path '$path' not found"
        return
    }

    if ($psCmdlet.ParameterSetName -eq 'Recommended') {
        # remove all these:
        $ArchivedHistory = $BrowsingHistory = $Cookies = $Favicons = $MediaData = $TemporaryFiles = $true
        # but leave these intact:$FormData = $Passwords = $TopSites
         = $false
    }

    $msg = @()
    $items = @()
    if ($ArchivedHistory -or $All) { $items += "Archived History*"             ; $msg += "Archived History" }
    if ($BrowsingHistory -or $All) { $items += @("History*", "Visited Links*") ; $msg += @("History", "Visited Links") }
    if ($Cookies -or $All)         { $items += "Cookies*"                      ; $msg += "Cookies" }
    if ($Favicons -or $All)        { $items += "Favicons*"                     ; $msg += "Favicons" }
    if ($FormData -or $All)        { $items += "Web Data*"                     ; $msg += "Form Data" }
    if ($MediaData -or $All)       { $items += "Media Cache*"                  ; $msg += "Media Cache" }
    if ($Passwords -or $All)       { $items += "Login Data*"                   ; $msg += "Passwords" }
    if ($TemporaryFiles -or $All)  { $items += "Cache*"                        ; $msg += "Temporary Files Cache" }
    if ($TopSites -or $All)        { $items += "Top Sites*"                    ; $msg += "Top Sites" }

    $oldErrorActionPreference = $ErrorActionPreference
    $ErrorActionPreference = 'SilentlyContinue'

    $refdate = (Get-Date).AddDays(-([Math]::Abs($DaysToKeep)))
    $allItems = @()
    if ($items.Length) {
        $allItems += $items | ForEach-Object {
            $name = $_
            Get-ChildItem $path -Recurse -Force |
            Where-Object { ($_.CreationTime -lt $refdate) -and $_.Name -like $name }
        }
    }

    if ($allItems.Length) {
        # type the bullet character with Alt-7
        $join = "`r`n         • "
        $msg = ($msg | Sort-Object) -join $join
        Write-Verbose ("$($MyInvocation.MyCommand):$join$msg")

        $allItems | ForEach-Object { Remove-Item $_.FullName -Force -Recurse }
    }
    else {
        Write-Verbose "$($MyInvocation.MyCommand): Nothing selected or older than $($refdate.ToString()) found"
    }
    $ErrorActionPreference = $oldErrorActionPreference
}

仅删除 cookie 而不删除其他任何内容,请像这样使用它:

Clear-ChromeHistory -DaysToKeep 1 -Cookies -Verbose

但是,您还可以使用更多开关来删除其他 Chrome 数据。

【讨论】:

  • 感谢您的回复。这个方法太长了。我想要一个简短的方法来执行查询以删除 cookie。你知道吗?
  • 缩短函数 调用 的一种方法是将其作为 Powershell 模块存储在您的 Modules 路径中。假设您将模块称为“CleanChrome”。在模块路径中创建一个名为 CleanChrome 的文件夹。接下来将函数保存为名为@9​​87654324@ 的文件。然后你所要做的就是只用这个代码Import-Module CleanChrome; Clear-ChromeHistory -DaysToKeep 0 -Cookies 来使用它,而不必查看函数本身。当然,另一种方法是删除你不需要的所有功能,只保留 cookie 的东西..
  • 第二个选项是删除除删除 cookie 的代码之外的所有内容。你最终可以简单地做$path = "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Default"; gci $path -Recurse -Force | ? {$_.Name -like 'Cookies*'} | % {ri $_.FullName -Force -Recurse}。当然,这会让你没有任何错误处理。就我个人而言,我建议使用上面的Module 选项,而不是简单地使用尽可能短的代码(如果只是为了学习 Powershell 编码)
猜你喜欢
  • 2015-04-06
  • 1970-01-01
  • 2010-10-10
  • 2011-01-16
  • 2020-12-27
  • 2011-05-27
  • 2012-11-18
  • 2013-03-15
相关资源
最近更新 更多