Last active
May 29, 2021 16:03
-
-
Save serban-petrescu/66b80e5a7a72b56033dddf763e7e04a8 to your computer and use it in GitHub Desktop.
BW-AWS: Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$path = ".\$($id).xml" #id = BW item name | |
function Write-Cache() { | |
param ($value) | |
$secure = ConvertTo-SecureString -AsPlainText -Force -String $value | |
$obj = New-Object -TypeName System.Management.Automation.PSCredential ` | |
-ArgumentList "myusername", $secure | |
return $obj | Export-Clixml $path | |
} | |
function Read-Cache() { | |
$obj = Import-Clixml -Path $path | |
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($obj.Password) | |
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | |
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR) | |
return $password | |
} | |
function Test-Cache() { | |
if (-not(Test-Path -Path $path -PathType Leaf)) { | |
return 0 | |
} | |
else { | |
$lastWrite = (Get-Item $path).LastWriteTime | |
$timespan = New-Timespan -seconds ($sessionLifetime - $sessionBuffer) | |
return (((Get-Date) - $lastWrite) -lt $timespan) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment