functions/taskengine/Set-PSFTaskEngineCache.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function Set-PSFTaskEngineCache
{
    <#
  .SYNOPSIS
   Sets the cache for a task engine task.
   
  .DESCRIPTION
   Sets the cache for a task engine task.
   Tasks executed by the task engine have no way to directly transfer output to the main runspace.
   This function is designed to work around this by providing a central storage.
   This function should only be called tasks scheduled to execute within the task engine.
   
  .PARAMETER Module
   The name of the module that generated the task.
   Use scriptname in case of using this within a script.
   
  .PARAMETER Name
   The name of the task for which the cache is.
   
  .PARAMETER Value
   The value to set this cache to.
   
  .EXAMPLE
   PS C:\> Set-PSFTaskEngineCache -Module 'mymodule' -Name 'maintenancetask' -Value $results
  
   Stores the content of $results in the cache 'mymodule / maintenancetask'
   These values can now be retrieved using Get-PSFTaskEngineCache.
 #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Module,
        
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,
        
        [Parameter(Mandatory = $true)]
        [AllowNull()]
        $Value
    )
    
    $tempModule = $Module.ToLower()
    $tempName = $Name.ToLower()
    
    if (-not ([PSFramework.TaskEngine.TaskHost]::Cache.ContainsKey($tempModule)))
    {
        [PSFramework.TaskEngine.TaskHost]::Cache[$tempModule] = @{ }
    }
    
    [PSFramework.TaskEngine.TaskHost]::Cache[$tempModule][$tempName] = $Value
}