1818
1919 PS C:\> #Get the grey agents
2020 PS C:\> $objects = Get-SCOMMonitoringObject -class:$agent | where {$_.IsAvailable -eq $false}
21- PS C:\> .\Clear-SCOMCache.ps1 -Servers $objects.DisplayName
21+ PS C:\> .\Clear-SCOMCache.ps1 -Servers $objects
2222
2323 Clear SCOM cache on every Management Server in Management Group.
24- PS C:\> Get-SCOMManagementServer | %{ .\Clear-SCOMCache.ps1 -Servers $_.DisplayName}
24+ PS C:\> Get-SCOMManagementServer | .\Clear-SCOMCache.ps1
2525
2626 Clear SCOM cache on every Agent in the in Management Group.
27- PS C:\> Get-SCOMAgent | %{.\Clear-SCOMCache.ps1 -Servers $_.DisplayName}
28-
27+ PS C:\> Get-SCOMAgent | .\Clear-SCOMCache.ps1
28+
29+ Clear SCOM cache on the Servers specified.
30+ PS C:\> .\Clear-SCOMCache.ps1 -Servers IIS-Server.contoso.com, MS2.contoso.com
31+
2932 Clear SCOM cache and reboot the Servers specified.
3033 PS C:\> .\Clear-SCOMCache.ps1 -Servers IIS-Server.contoso.com, MS2.contoso.com -Reboot
3134
3437 Blake Drumm (blakedrumm@microsoft.com)
3538
3639 .MODIFIED
37- September 10th , 2021
40+ September 13th , 2021
3841#>
3942[OutputType ([string ])]
4043param
4952 HelpMessage = ' Optionally reboot the server after stopping the SCOM Services and clearing SCOM Cache. This will always perform on the local server last.' )]
5053 [Switch ]$Reboot
5154)
52-
5355BEGIN
5456{
55- if ($Servers -match ' Microsoft.EnterpriseManagement.Administration.ManagementServer' )
56- {
57- $Servers = $Servers.DisplayName
58- }
59- elseif ($Servers -match ' Microsoft.EnterpriseManagement.Administration.AgentManagedComputer' )
60- {
61- $Servers = $Servers.DisplayName
62- }
63-
6457 Write-Host ' ===================================================================' - ForegroundColor DarkYellow
6558 Write-Host ' ========================== Start of Script =======================' - ForegroundColor DarkYellow
6659 Write-Host ' ===================================================================' - ForegroundColor DarkYellow
@@ -84,11 +77,47 @@ BEGIN
8477 $permissiongranted = " Currently running as administrator - proceeding with script execution..."
8578 Write-Host $permissiongranted - ForegroundColor Green
8679 }
80+
81+ Function Time-Stamp
82+ {
83+ $TimeStamp = Get-Date - Format " MM/dd/yyyy hh:mm:ss tt"
84+ write-host " $TimeStamp - " - NoNewline
85+ }
8786}
8887PROCESS
8988{
90-
91-
89+ $setdefault = $false
90+ foreach ($Server in $input )
91+ {
92+ if ($Server.GetType ().Name -eq ' ManagementServer' )
93+ {
94+ if (! $setdefault )
95+ {
96+ $Servers = @ ()
97+ $setdefault = $true
98+ }
99+ $Servers += $Server.DisplayName
100+ }
101+ elseif ($Server.GetType ().Name -eq ' AgentManagedComputer' )
102+ {
103+ if (! $setdefault )
104+ {
105+ $Servers = @ ()
106+ $setdefault = $true
107+ }
108+ $Servers += $Server.DisplayName
109+ }
110+ elseif ($Server.GetType ().Name -eq ' MonitoringObject' )
111+ {
112+ if (! $setdefault )
113+ {
114+ $Servers = @ ()
115+ $setdefault = $true
116+ }
117+ $Servers += $Server.DisplayName
118+ }
119+
120+ }
92121 Function Clear-SCOMCache
93122 {
94123 param
@@ -257,7 +286,7 @@ PROCESS
257286 }
258287 catch
259288 {
260- Write-Host " Issue clearing the 'Health Service State' folder." - ForegroundColor Yellow
289+ Write-Host " Issue removing the 'Health Service State' folder. Maybe attempt to clear the cache again, or a process is using the Health Service State Folder. " - ForegroundColor Red
261290 # $healthservice = $null
262291 }
263292 }
@@ -308,7 +337,15 @@ PROCESS
308337 {
309338 try
310339 {
311- Time- Stamp; Write-Host " Clearing Operations Manager Console Cache." ; Get-ChildItem " $env: SystemDrive \Users\*\AppData\Local\Microsoft\Microsoft.EnterpriseManagement.Monitoring.Console\momcache.mdb" | % { Remove-Item $_ - Force - ErrorAction Stop }
340+ Time- Stamp
341+ Write-Host " Clearing Operations Manager Console Cache for the following users:" ;
342+ $cachePath = Get-ChildItem " $env: SystemDrive \Users\*\AppData\Local\Microsoft\Microsoft.EnterpriseManagement.Monitoring.Console\momcache.mdb"
343+ foreach ($consolecachefolder in $cachePath )
344+ {
345+ Time- Stamp
346+ Write-Host " $ ( $consolecachefolder.FullName.Split (" \" )[2 ]) "
347+ Remove-Item $consolecachefolder - Force - ErrorAction Stop
348+ }
312349 }
313350 catch { Write-Warning $_ }
314351 }
@@ -431,9 +468,6 @@ PROCESS
431468
432469 }
433470 }
434- }
435- end
436- {
437471 if ($Servers -or $Reboot -or $All )
438472 {
439473 Clear-SCOMCache - Servers $Servers - Reboot:$Reboot - All:$All
448482 Clear-SCOMCache
449483 }
450484}
485+ end
486+ {
487+ Time- Stamp
488+ Write-Host " Script has Completed!" - ForegroundColor Gray
489+ Write-Host " ==================================================================="
490+ }
0 commit comments