Private/Get-AtwsCacheInfo.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Function Get-AtwsCacheInfo
{
  [CmdLetBinding()]
  
  Param
  (
    [String]
    $Prefix = 'Atws'
  )

  Begin
  {
    Write-Verbose -Message ('{0}: Trying to determine correct location for dynamic module cache.' -F $MyInvocation.MyCommand.Name)    
    
    $ModuleAutotask = Get-Module -Name Autotask
    $ModuleVersionFileName = 'ModuleVersionInfo.xml'
    $ModuleVersionInfo = New-Object -TypeName PSObject -Property @{
      APIversion = $AtwsConnection[$Prefix].GetWsdlVersion()
      ModuleVersion = $ModuleAutotask.Version.ToString()
      CI = ($AtwsConnection[$Prefix].getZoneInfo($AtwsConnection[$Prefix].Credentials.UserName)).CI
    }
    If ($ModuleAutotask.ModuleBase -like "$Env:ProgramFiles*")
    { 
      $ModuleBase = Split-Path $ModuleAutotask.ModuleBase -Parent
    }
    Else
    {
      $ModuleBase = '{0}\WindowsPowerShell\Modules\{1}' -F $Env:ProgramFiles, $ModuleAutotask.Name
    }

    $CentralCache = '{0}\Autotask.{1}\{2}' -F $ModuleBase, $Prefix, $ModuleVersionFileName

    Write-Verbose -Message ('{0}: Machine cache location is {1}. Loaded module version is {2} and API version is {3}.' -F $MyInvocation.MyCommand.Name, $CentralCache, $ModuleVersionInfo.ModuleVersion, $ModuleVersionInfo.APIversion)    
    
    $PersonalModules = '{0}\WindowsPowershell\Modules' -f $([environment]::GetFolderPath('MyDocuments'))
    $PersonalCache = '{0}\{1}\Autotask.{2}\{3}' -F $PersonalModules, $ModuleAutotask.Name, $Prefix, $ModuleVersionFileName
    
    Write-Verbose -Message ('{0}: Personal cache location is {1}. Loaded module version is {2} and API version is {3}.' -F $MyInvocation.MyCommand.Name, $CentralCache, $ModuleVersionInfo.ModuleVersion, $ModuleVersionInfo.APIversion)   
    
    $CacheDirty = $True
  }
  
  Process
  {
  
    If (Test-Path $PersonalCache)
    {
      Write-Verbose -Message ('{0}: A personal cache already exists for prefix {1}' -F $MyInvocation.MyCommand.Name, $Prefix)
      
      $CurrentVersionInfo = Import-Clixml -Path $PersonalCache
      $CachePath = $PersonalCache 
      If ($CurrentVersionInfo.APIversion -eq $ModuleVersionInfo.APIversion `
      -and $CurrentVersionInfo.ModuleVersion -eq $ModuleVersionInfo.ModuleVersion`
      -and $CurrentVersionInfo.CI -eq $ModuleVersionInfo.CI)
      {
        Write-Verbose -Message ('{0}: Personal cache is clean. Loading from personal cache.' -F $MyInvocation.MyCommand.Name)
                
        $CacheDirty = $False
      }
      Else
      {
        Write-Verbose -Message ('{0}: Personal cache exists, but is not up to date. Recreating cache.' -F $MyInvocation.MyCommand.Name)
      }
  
    }
    ElseIf (Test-Path $CentralCache)
    {
      Write-Verbose -Message ('{0}: There is no personal cache. Checking central cache location.' -F $MyInvocation.MyCommand.Name)
            
      $CurrentVersionInfo = Import-Clixml -Path $CentralCache
      Try 
      { 
        [io.file]::OpenWrite($CentralCache).close()
        $CentralPathWritable = $True
      }
      Catch 
      { 
        $CentralPathWritable = $False
      }

      If ($CurrentVersionInfo.APIversion -eq $ModuleVersionInfo.APIversion `
        -and $CurrentVersionInfo.ModuleVersion -eq $ModuleVersionInfo.ModuleVersion`
        -and $CurrentVersionInfo.CI -eq $ModuleVersionInfo.CI)
      {
        Write-Verbose -Message ('{0}: Central cache is clean. Loading from central cache.' -F $MyInvocation.MyCommand.Name)
                
        $CacheDirty = $False
        $CachePath = $CentralCache
        
      }
      ElseIf ($CentralPathWritable)
      {
        Write-Verbose -Message ('{0}: Central cache exists, but is not up to date. It is writable for current user. Recreating cache.' -F $MyInvocation.MyCommand.Name)
                
        $CachePath = $CentralCache
      }
      Else
      {
        Write-Verbose -Message ('{0}: Central cache exists, but is not up to date. It is NOT writable for current user. Using personal cache.' -F $MyInvocation.MyCommand.Name)
        Write-Warning -Message ('{0}: Central cache exists in location {1}, but is not up to date. It is NOT writable for current user. Using personal cache {1}.' -F $MyInvocation.MyCommand.Name, $CentralCache, $PersonalCache)
        $CachePath = $PersonalCache  
      }
  
    }
    Else
    {
      Write-Verbose -Message ('{0}: No cache exists, checking if a central cache location is writable.' -F $MyInvocation.MyCommand.Name)
            
      Try 
      { 
        $CacheDir = Split-Path $CentralCache -Parent
        $Null = New-Item -Path $CacheDir -ItemType Directory -Force -ErrorAction Stop
        $CachePath = $CentralCache
        
        Write-Verbose -Message ('{0}: A central cache location for all users will be created in {1}' -F $MyInvocation.MyCommand.Name, $CacheDir)
                
      }
      Catch 
      { 
        Write-Verbose -Message ('{0}: A personal cache location for current user will be created along {1}' -F $MyInvocation.MyCommand.Name, $PersonalCache)
                
        $CachePath = $PersonalCache
      }
      Finally
      {
        $CacheDirty = $True
      }
    }

    $CacheDir = Split-Path $CachePath -Parent
    If (-not (Test-Path $CacheDir))
    {
      $Null = New-Item -Path $CacheDir -ItemType Directory -Force -ErrorAction Stop
    }
  }
  
  End
  {
  

    $CacheInfo = New-Object -TypeName PSObject -Property @{
      CachePath = $CachePath
      CacheDir = $CacheDir
      CacheDirty = $CacheDirty
      ModuleVersion = $ModuleVersionInfo.ModuleVersion
      APIversion = $ModuleVersionInfo.APIversion 
      CI = $ModuleVersionInfo.CI
    }

    Return $CacheInfo
  }
}