Using this method my Get-user went from 86 seconds down to 0.0009 seconds the second time around.
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
if (-not $CashedQuerys) { $CashedQuerys = @{} } function Cached-Get-ADUser { [CmdletBinding()] param( [Parameter(Mandatory = $false, Position = 0)] #[ValidateNotNullOrEmpty()] [Alias('ID','Alias', 'DistinguishedName', 'Guid', 'PrimarySmtpAddress', 'SamAccountName')] [string]$Identity, [Parameter(Position = 1)] [ValidateSet('Negotiate', 'Baisc')] [string]$AuthType = 'Negotiate', [Parameter()] [System.Management.Automation.PSCredential]$Credential = $null, [Parameter()] [string]$Partition, [Parameter()] [string]$Properties, [Parameter()] [string]$Server = $null, [Parameter()] [string]$Filter, [Parameter()] [int]$ResultSetSize, [Parameter()] [string]$SearchBase, [Parameter()] [ValidateSet($null, 'Base', 'OneLevel', 'SubTree')] [string]$SearchScope = $null, [Parameter()] [ValidateSet($false, $true)] [string]$SetInsteadofGetCache = $false ) $Parameters = @{ Identity = $Identity AuthType = $AuthType Credential = $Credential Partition = $Partition Properties = $Properties Server = $Server Filter = $Filter ResultSetSize = $ResultSetSize SearchBase = $SearchBase SearchScope = $SearchScope } #Get-ADUser @Parameters $start = Get-Date $filteredParameters = @{} foreach ($key in $Parameters.Keys) { $value = $Parameters[$key] if ($value -ne $null -and $value -ne '') { $filteredParameters.Add($key, $value) } } $GeneratedJSON = $filteredParameters | ConvertTo-Json #$hash = ($GeneratedJSON | ConvertTo-SecureString -AsPlainText -Force | Get-Hash -Algorithm SHA256).Hash $hasher = [System.Security.Cryptography.HashAlgorithm]::Create('sha256') $hash = $hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($GeneratedJSON)) $hashString = [System.BitConverter]::ToString($hash).Replace('-', '') if ($SetInsteadofGetCache -eq $false) { if ( $CashedQuerys.ContainsKey($hashString) ) { $end = Get-Date $time = ($end - $start).TotalMilliseconds write-host "Cashed Result returned in: " $($time / 1000 / 60) $($GeneratedJSON.Replace("`n", '')) return $CashedQuerys.Item($hashString) } } #The command below flattens the hashtable to an array and slatting does not work as expected. #$Parameters = $Parameters.GetEnumerator() | Where-Object { $_.Value -ne $null -and $_.Value -ne '' } #Splatting $Result = Get-ADUser @filteredParameters if ($CashedQuerys.ContainsKey($hashString)) { $CashedQuerys.Item($hashString) = $Result } else { $CashedQuerys.Add($hashString, $Result) } $end = Get-Date $time = ($end - $start).TotalMilliseconds write-host "Result returned in: " $($time / 1000 / 60) $($GeneratedJSON.Replace('`n', '')) return $Result } #Get-Item Function:Get-ADUser | Select-Object -ExpandProperty Definition #$test = Get-ADUser -Identity EID #PS P:\> $test.GetType() #IsPublic IsSerial Name BaseType #-------- -------- ---- -------- #True False ADUser Microsoft.ActiveDirectory.Management.ADAccount #[Microsoft.ActiveDirectory.Management.ADAccount] | gm $First = Cached-Get-ADUser -Filter * $Second = Cached-Get-ADUser -Identity EID #$First = Cached-Get-ADUser -Filter * -SetInsteadofGetCache True $First = Cached-Get-ADUser -Filter * write-host $First.Count $Second.Name exit #There is an issue here where a new query created at a later time could have a different result set. This can only work with testing? |