我一直在寻找一种更有效的方法来删除存储在凭据管理器中的所有凭据,而不必逐一删除凭据。经过几个小时的浏览,我终于偶然发现了这个命令提示符字符串,它正是我所需要的:
for /F "tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do cmdkey /delete %H
由于我对这个语法的了解非常有限,我想知道它的每个部分的实际含义。我已经知道 cmdkey /list、findstr 和 cmdkey /delete 的作用,但我不确定其余的。
此外,我想知道如何例外。例如,在本例中,该行删除具有 cmdkey 列表中显示的目标的所有字符串: 如果我想破例并仅删除某些凭据而不删除其他凭据,该怎么办?我可以使用类型值而不是目标值来执行此操作,例如通过要求命令提示符仅删除通用类型凭据而不删除通用证书和域密码类型凭据吗?
预先感谢您的帮助。
KR, 安迪
当使用字符串“for /F”tokens=1,2 delims=“%G in ('cmdkey /list ^| findstr Target') do cmdkey /delete %H”时,我不断收到“CMDKEY: Element”没有找到。”
因此,我编写了一个非常原始的解决方法,因为我不太了解批处理语法。下面的脚本将必须在 cmd 中运行以删除相应的 CM 密钥的所有行放在一起。脚本中的最后一个命令调用包含所有行的文件。您只需将它们复制/粘贴到 cmd 中并按 Enter 键,它就会删除其中的大部分:
$cachedCredentials = cmdkey /list
$CMCredentialsTxt = "C:\temp\CMCredentials.txt"
$cachedCredentials | Out-File $CMCredentialsTxt
(Get-Content $CMCredentialsTxt) | ? {$_.trim() -ne "" } | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Trim() | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("Currently stored credentials:","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("Type: Generic", "") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("User: App Info","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("Local machine persistence","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("Target: ","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("User: <Certificate>","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("Certificate","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("User: User DT","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt).Replace("User: User OS Info","") | Set-Content $CMCredentialsTxt
(Get-Content $CMCredentialsTxt) | ? {$_.trim() -ne "" } | Set-Content $CMCredentialsTxt
$cleanTargets = Get-Content $CMCredentialsTxt
Remove-Item $CMCredentialsTxt
forEach ($cleanTarget in $cleanTargets){"cmdkey /delete:" + $cleanTarget | Out-File $CMCredentialsTxt -Append}
Invoke-Item $CMCredentialsTxt
我知道这是一篇非常旧的帖子,但我想展示我如何修改你的第一个命令以处理包含空格和冒号的名称。
for /F "tokens=1,* delims= " %G in ('cmdkey /list ^| findstr Target') do cmdkey /delete:"%H"
在标记中用 * 替换 2 将返回该行的其余部分,而不仅仅是下一个标记(在本例中以空格分隔)
包装 cmdkey 的删除参数应确保删除正确的条目。