Private/Export-AtwsDiskCache.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
57
Function Export-AtwsDiskCache
{
  [CmdLetBinding()]
  
  Param()

  Begin
  {
    If (-not ($Script:Cache)) {
      Write-Error -Message 'The diskcache has not been imported yet. Noting to save.'
      Return
    }
     
    $CacheFile = 'AutotaskFieldInfoCache.xml'
    $PersonalCacheDir = '{0}\WindowsPowershell\Cache' -f $([environment]::GetFolderPath('MyDocuments'))
    $PersonalCache = '{0}\{1}' -F $PersonalCacheDir, $CacheFile

    Write-Verbose -Message ('{0}: Personal cache location is {1}.' -F $MyInvocation.MyCommand.Name, $PersonalCache)   
  }
  
  Process
  {
    # If the module variable UseDiskCache is $False or does not exist, this function does nothing...
    
    If ($Script:UseDiskCache) { 
      # This REALLY should be there, but better safe than sorry
      # Create Personalcache directory if it doesn't exist
      If (-not (Test-Path $PersonalCacheDir)) {
      
        $Caption = $MyInvocation.MyCommand.Name
        $VerboseDescrition = '{0}: Creating a new personal cache dir {1}' -F $Caption, $PersonalCacheDir
        $VerboseWarning = '{0}: About to create a new personal cache dir {1}. Do you want to continue?' -F $Caption, $PersonalCacheDir
          
        If ($PSCmdlet.ShouldProcess($VerboseDescrition, $VerboseWarning, $Caption)) { 
          New-Item -Path $PersonalCacheDir -ItemType Directory
        }
    
      }
               
      $Caption = $MyInvocation.MyCommand.Name
      $VerboseDescrition = '{0}: Saving updated cache info to {1}' -F $Caption, $PersonalCache
      $VerboseWarning = '{0}: About to save updated cache info to {1}. Do you want to continue?' -F $Caption, $PersonalCache
          
      If ($PSCmdlet.ShouldProcess($VerboseDescrition, $VerboseWarning, $Caption)) { 
            
        $Script:Cache | Export-Clixml -Path $PersonalCache -Force
    
      }
    }
  }
  
  End
  {
    Write-Verbose ('{0}: End of function' -F $MyInvocation.MyCommand.Name)    
  }
}