Public/WebSessions.ps1
function Save-TMWebSessionCache { <# .SYNOPSIS Saves a TMWebSession Cache object from the provided variable .DESCRIPTION This function creates a new CacheStore by name in the current user profile. .EXAMPLE Test-PrimeNumber -Number @(1, 5, 754, 436, 87546, 31) .EXAMPLE (1..100) | Test-PrimeNumber .OUTPUTS A boolean value indicating if the given number is a prime number #> [CmdletBinding()] # Always add CmdletBinding to expose the common Cmdlet variables # [OutputType([Bool])] # Add this if the function has an output param( [String]$Provider, [String]$FilePath = (Join-Path -Path $userFilesRoot -ChildPath 'Cache' -AdditionalChildPath 'Sessions', $Provider, 'Sessions.json'), [String]$SessionVariableName, [psobject[]]$Sessions ) ## Ensure there is a JSON File present $FileExists = Test-Path -Path $FilePath if ($FileExists) { ## Get the Cached Items $CachedSessions = Get-Content -Path $FilePath | ConvertFrom-Json ## Save the Web Session Cache $OutputCachedSessions = @() $CachedSessions | ForEach-Object { ## This should be a WebSession Object if (-Not $_.GetType().toString() -eq 'WebSession') { Write-Warning 'This is not a web session.' } $OutputCachedSessions += [PSCustomObject]@{ Name = $_.name } } } else { ## First test if the containing directory exists Test-FolderPath -FolderPath ($FilePath -replace '\Sessions.json', '') ## Create an empty Cache $OutputCachedSessions = @() ## Then write an empty file $OutputCachedSessions | ConvertTo-Json -Depth 10 | Set-Content -Path $FilePath -Force } ## Save the Cached Sessions Data to a file $OutputCachedSessions | ConvertTo-Json -Depth 10 | Set-Content -Path $FilePath -Force } function Restore-TMWebSessionCache { <# .SYNOPSIS Saves a TMWebSession Cache object from the provided variable .DESCRIPTION This function creates a new CacheStore by name in the current user profile. .EXAMPLE Test-PrimeNumber -Number @(1, 5, 754, 436, 87546, 31) .EXAMPLE (1..100) | Test-PrimeNumber .OUTPUTS A boolean value indicating if the given number is a prime number #> [CmdletBinding()] # Always add CmdletBinding to expose the common Cmdlet variables # [OutputType([Bool])] # Add this if the function has an output param( [String]$Provider, [String]$FilePath = (Join-Path -Path $userFilesRoot -ChildPath 'Cache' -AdditionalChildPath 'Sessions', $Provider, 'Sessions.json'), [String]$SessionVariableName, [psobject]$Sessions ) ## Ensure there is a JSON File present $FileExists = Test-Path -Path $FilePath if (-not $FileExists) { ## First test if the containing directory exists Test-FolderPath -FolderPath ($FilePath -replace '\Sessions.json', '') ## Create an empty Cache $CachedSessions = @() ## Then write an empty file $CachedSessions | ConvertTo-Json -Depth 10 | Set-Content -Path $FilePath -Force } else { $CachedSessions | Get-Content -Path $FilePath | ConvertFrom-Json } $VariableNameParts = $SessionVariableName -split ':' if ($VariableNameParts.Count -gt 1) { ## Variable Scope and name provided $ScopeName = $VariableNameParts[0] $VariableName = $VariableNameParts[1] } else { $ScopeName = 'Script' $VariableName = $SessionVariableName } New-Variable -Name $VariableName -Scope $ScopeName -Force } |