Skip to content

Instantly share code, notes, and snippets.

@serban-petrescu
Last active May 29, 2021 16:03
Show Gist options
  • Save serban-petrescu/66b80e5a7a72b56033dddf763e7e04a8 to your computer and use it in GitHub Desktop.
Save serban-petrescu/66b80e5a7a72b56033dddf763e7e04a8 to your computer and use it in GitHub Desktop.
BW-AWS: Cache
$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