![](/img/trans.png)
[英]Jenkins does not detect GIT credentials from Windows Credential manager
[英]Remove an entry from credential manager for all users on Windows
我目前正在為 Windows 卸載程序中的所有用戶實施“刪除設置”,並遇到了一個我什至不確定是否可以解決的問題。
應用程序使用 CredentialManager (
keymgr.dll<\/code> ) 為當前用戶存儲憑證條目。
我們將憑證的目標稱為“X”。 卸載時,應從所有用戶上刪除所有使用目標“X”存儲的憑據。 卸載程序當然需要管理員權限,但我仍然覺得很難做到這一點。
對於當前用戶,該命令通常通過命令提示符下的
cmdkey \/delete=:X<\/code>來解決。
據我所知,
cmdkey.exe \/list<\/code>僅有助於列出當前用戶的條目,不能刪除其他用戶的本地條目。
我了解到憑據存儲為
C:\\Users\\_user_\\AppData\\Local\\Microsoft\\Credentials<\/code>文件夾下的操作系統文件,但我不知道哪些文件是我要刪除的條目,刪除所有文件會很危險用於其他應用程序。
另外我認為刪除操作系統文件會很危險,並且可能有限制(額外的 UAC 提示?)。
Runas<\/code>命令是我得到的最接近的命令,但是因為它需要用戶的密碼,所以它變得非常困難,而不是我在卸載程序中想要的。
我還需要一種方法來獲取每個用戶的用戶名和域並對其進行迭代。
我更願意為此使用
cmd<\/code>或
powershell<\/code> 。
不想死掉一個舊帖子,但我需要自己做,所以我想我會添加這個以防其他人需要它:
cmdkey /list | ForEach-Object{if($_ -like "*Target:*" -and $_ -like "*microsoft*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}
Powershell 一個襯墊,將刪除字符串中與 Microsoft 的任何憑據。
參考: https : //gist.github.com/janikvonrotz/7819990
我運行了它並在本地清除了它而無需以管理員身份運行(但我是本地管理員)
從批處理文件或 PowerShell 命令運行時 cmdkey.exe 實用程序可能會遇到兩個與特殊字符相關的問題。 1. 如果從批處理文件運行,如果憑證有沒有雙引號的“(”或“)”,即左右括號,則不會刪除該憑證。 2. 如果憑據名稱又名目標名稱,有一個由空格包圍的連字符,則 cmdkey 將不會刪除或創建帶有該字符串“-”的憑據。
有一些 powershell 模塊可以嘗試執行此操作,但我發現唯一一個處理此異常的模塊是在 Github https://github.com/bamcisnetworks/BAMCIS.CredentialManager 上
BAMCIS.CredentialManager
使用它,我能夠創建憑據以設置帶有括號或連字符的測試環境,但更重要的是通過使用 modules 命令收集緩存憑據的用戶列表然后將命令中的信息傳遞給 remove 命令來刪除它們刪除所有緩存的憑據。
一個警告。 刪除該命令后,一段時間后會動態重新出現兩個緩存的憑據。
因此,為了解決頻繁的用戶鎖定問題,我將嘗試在注銷時在用戶上下文下使用 SCCM 進行部署。 否則,可能需要在刪除憑據后重新啟動系統。 這是一個原型腳本,它導入模塊,然后使用它來刪除所有緩存的憑據,與往常一樣,測試、測試、測試和使用風險自負!
Clear-host
import-Module "$PSScriptRoot\BAMCIS.CredentialManager\BAMCIS.CredentialManager.psd1"
$L = Get-CredManCredentialList -ErrorAction SilentlyContinue
If($L -eq $null)
{
Write-host "No Cached Credentials found to remove, no action taken"
$LASTEXITCODE = 0
Write-host "The last exit code is $LASTEXITCODE"
}
Else
{
ForEach($cred in $L)
{
Write-host "`tProcessing...`n"
Write-host "$($cred.TargetName.ToString())`n"
Write-host "$($cred.Type.ToString())`n`n"
$R = Remove-CredManCredential -TargetName $($cred.TargetName.ToString()) -Type $($cred.Type.ToString()) -Force
}
$L = Get-CredManCredentialList -ErrorAction SilentlyContinue -ErrorVariable $Cred_Error
If($L -eq $null)
{
Write-host "All Cached Credentials removed, program Complete"
$LASTEXITCODE = 0
Write-host "The last exit code is $LASTEXITCODE"
}
Else
{
Write-host "WARNING: One or more Cached Credentials were not removed, program Complete"
$LASTEXITCODE = 1
}
}
批處理,提升的 cmd 提示:
要找到主要的,請列出任何帶有MS.O、Micro 和團隊的:
for /f "tokens=1-4 Delims=:=" %A in ('cmdkey /list ^| findstr Target: ^| findstr /i "MS.O Micro Teams"') do @echo %D
這將刪除團隊的所有條目:
for /f "tokens=1-4 Delims=:=" %A in ('cmdkey /list ^| findstr /i teams') do @cmdkey /delete:%D
如果您想將其作為腳本包含在內,則語法會略有不同。 將 %%A %%D 變量加倍
遇到類似問題,清除未加入域的設備的舊憑據。 創建登錄任務以清除憑據。 就我而言,我正在尋找特定的文件服務器,因此不要意外清除所有憑據。 由於管道字符串表達式到任務時的語法問題,創建引用 .ps1 然后在任務中引用更容易。 通過 Intune 推送此腳本....
# Full path of the file
$file = 'C:\script\clearcreds.ps1'
#If the file does not exist, create it.
if (-not(Test-Path -Path $file -PathType Leaf)) {
try {
$null = New-Item -Path "C:\ProgramData\CentraStage\Packages" -Name
"clearcreds.ps1" -ItemType "file" -Value 'cmdkey /list | ForEach-Object{if($_ -
like "*Target:*" -and $_ -like "*fileserver*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}'
Write-Host "The file [$file] has been created."
}
catch {
throw $_.Exception.Message
}
}
#######################################
#Create ScheduledTask to Run at log on.
#######################################
$schtaskName = "Clear Cached Creds "
$schtaskDescription = "Clears Cached Creds for each user at logon."
$trigger = New-ScheduledTaskTrigger -AtLogOn
$class = cimclass MSFT_TaskEventTrigger root/Microsoft/Windows/TaskScheduler
#Execute task in users context
$principal = New-ScheduledTaskPrincipal -GroupId "S-1-5-32-545" -Id "Author"
$action3 = New-ScheduledTaskAction -Execute 'Powershell.exe' "-File C:\script\clearcreds.ps1"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$null=Register-ScheduledTask -TaskName $schtaskName -Trigger $trigger -Action $action3 -Principal $principal -Settings $settings -Description
$schtaskDescription -Force
Start-ScheduledTask -TaskName $schtaskName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.