forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-CachedCredential.ps1
29 lines (24 loc) · 1021 Bytes
/
Remove-CachedCredential.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<#
.SYNOPSIS
Removes a credential from secure storage.
.LINK
ConvertTo-Base64.ps1
.LINK
Stop-ThrowError.ps1
.EXAMPLE
Remove-CachedCredential.ps1 exampleuser 'OpenTV API login'
The credential is removed from secure storage.
#>
#Requires -Version 3
[CmdletBinding()][OutputType([pscredential])] Param(
# Specifies a user or account name that was used to create the credential.
[Parameter(Position=0,Mandatory=$true)][string] $UserName,
# Provides a login prompt for the user that should be a globally unique description of the purpose of the login.
[Parameter(Position=1,Mandatory=$true)][string] $Message
)
$credcache = Join-Path $env:LOCALAPPDATA .credcache
if(!(Test-Path $credcache -Type Container)) {return}
$hashalg = New-Object Security.Cryptography.SHA256Managed
$entry = ConvertTo-Base64.ps1 -Data $hashalg.ComputeHash([Text.Encoding]::UTF8.GetBytes("$UserName@$Message")) -UriStyle
$file = Join-Path $credcache $entry
if(Test-Path $file -Type Leaf) {Remove-Item $file -Force}