|
| 1 | +#This script will run the Kevin Holman steps to Purge Agent Data from the OperationsManager DB: https://kevinholman.com/2018/05/03/deleting-and-purging-data-from-the-scom-database/ |
| 2 | +#---------------------------------------------------------------------------------------------------------------------------------- |
| 3 | +#-Requires: SQL Server Powershell Module (https://docs.microsoft.com/en-us/sql/powershell/download-sql-server-ps-module) |
| 4 | +#Author: Blake Drumm (v-bldrum@microsoft.com) |
| 5 | +#Date Created: 4/10/2021 |
| 6 | +cls |
| 7 | + |
| 8 | +#----------------------------------------------------------- |
| 9 | +#region ScriptVariables |
| 10 | +#----------------------------------------------------------- |
| 11 | +#In the format of: ServerName\SQLInstance |
| 12 | +#ex: SQL01\SCOM2019 |
| 13 | +$SQLServer = "sql01" |
| 14 | +$db1 = "OperationsManager" |
| 15 | + |
| 16 | +#Name of Agent to Erase from SCOM |
| 17 | +#Fully Qualified (FQDN) |
| 18 | +$MachineToRemove = "Agent1.contoso.com" |
| 19 | +#endregion |
| 20 | + |
| 21 | +#----------------------------------------------------------- |
| 22 | + |
| 23 | +<# |
| 24 | + DO NOT EDIT PAST THIS POINT |
| 25 | +#> |
| 26 | + |
| 27 | +if(!(Get-Command Invoke-Sqlcmd -ErrorAction SilentlyContinue)) |
| 28 | +{ |
| 29 | + Write-Warning "Unable to run this script due to missing dependency:`n`t`tSQL Server Powershell Module (https://docs.microsoft.com/en-us/sql/powershell/download-sql-server-ps-module)`n`nTry running this script on a SQL Server if you cannot download the Powershell Module." |
| 30 | + break |
| 31 | +} |
| 32 | + |
| 33 | +$bme_query = @" |
| 34 | +--Query 1 |
| 35 | +--First get the Base Managed Entity ID of the object you think is orphaned/bad/needstogo: |
| 36 | +-- |
| 37 | +DECLARE @name varchar(255) = '%$MachineToRemove%' |
| 38 | +-- |
| 39 | +SELECT BaseManagedEntityId, FullName, DisplayName, IsDeleted, Path, Name |
| 40 | +FROM BaseManagedEntity WHERE (FullName like @name OR DisplayName like @name) AND FullName like '%Linux.Universal%' |
| 41 | +ORDER BY FullName |
| 42 | +"@ |
| 43 | + |
| 44 | +$BME_IDs = (Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query $bme_query -OutputSqlErrors $true) |
| 45 | + |
| 46 | +if(!$BME_IDs) |
| 47 | +{ |
| 48 | +Write-Warning "Unable to find any data in the OperationsManager DB associated with: $MachineToRemove" |
| 49 | +break |
| 50 | +} |
| 51 | + |
| 52 | +Write-Host "Found the following data associated with: " -NoNewline |
| 53 | +Write-Host $MachineToRemove -ForegroundColor Green |
| 54 | +$BME_IDs | Select FullName, DisplayName, Path, IsDeleted, BaseManagedEntityId |
| 55 | +$count = $BME_IDs.Count |
| 56 | +do |
| 57 | +{ |
| 58 | + $answer1 = Read-Host -Prompt "Do you want to delete the above $count item(s) from the OperationsManager DB? (Y/N)" |
| 59 | +} |
| 60 | +until ($answer1 -eq "y" -or $answer1 -eq "n") |
| 61 | +if($answer1 -eq "n") |
| 62 | +{ Write-Host "Exiting Script.."; break } |
| 63 | +foreach($BaseManagedEntityID in $BME_IDs) |
| 64 | +{ |
| 65 | +Write-Host " Gracefully deleting the following from OperationsManager Database:`n`tName: " -NoNewline |
| 66 | +$BaseManagedEntityID.FullName | Write-Host -ForegroundColor Green |
| 67 | +Write-Host " `tBME ID: " -NoNewline |
| 68 | +$BaseManagedEntityID.BaseManagedEntityId | Write-Host -ForegroundColor Gray |
| 69 | + |
| 70 | +Write-Host '' |
| 71 | + |
| 72 | +$current_bme = $BaseManagedEntityID.BaseManagedEntityId.Guid |
| 73 | + |
| 74 | +$delete_query = @" |
| 75 | +--Query 2 |
| 76 | +--Next input that BaseManagedEntityID into the delete statement |
| 77 | +--This will delete specific typedmanagedentities more gracefully than setting IsDeleted=1 |
| 78 | +--change "00000000-0000-0000-0000-000000000000" to the ID of the invalid entity |
| 79 | +-- |
| 80 | +DECLARE @EntityId uniqueidentifier = '$current_bme' |
| 81 | +-- |
| 82 | +DECLARE @TimeGenerated datetime; |
| 83 | +SET @TimeGenerated = getutcdate(); |
| 84 | +BEGIN TRANSACTION |
| 85 | +EXEC dbo.p_TypedManagedEntityDelete @EntityId, @TimeGenerated; |
| 86 | +COMMIT TRANSACTION |
| 87 | +"@ |
| 88 | +Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query $delete_query -OutputSqlErrors $true |
| 89 | +} |
| 90 | + |
| 91 | +$remove_count_query = @" |
| 92 | +--Query 4 |
| 93 | +--Get an idea of how many BMEs are in scope to purge |
| 94 | +SELECT count(*) FROM BaseManagedEntity WHERE IsDeleted = 1 |
| 95 | +"@ |
| 96 | + |
| 97 | +$remove_count = (Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query $remove_count_query -OutputSqlErrors $true).Column1 |
| 98 | + |
| 99 | +"OperationsManager DB has " | Write-Host -NoNewline |
| 100 | +$remove_count | Write-Host -NoNewline -ForegroundColor Green |
| 101 | +" object(s) pending to Delete`n" | Write-Host |
| 102 | + |
| 103 | +do |
| 104 | +{ |
| 105 | + $answer2 = Read-Host -Prompt "Do you want to purge the deleted item(s) from the OperationsManager DB? (Y/N)" |
| 106 | +} |
| 107 | +until ($answer2 -eq "y" -or $answer2 -eq "n") |
| 108 | + |
| 109 | +$remove_pending_management = @" |
| 110 | +exec p_AgentPendingActionDeleteByAgentName "$MachineToRemove" |
| 111 | +"@ |
| 112 | + |
| 113 | +Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query $remove_pending_management -OutputSqlErrors $true |
| 114 | + |
| 115 | +Write-Host "Cleared $MachineToRemove from Pending Management List in SCOM Console." -ForegroundColor DarkGreen |
| 116 | + |
| 117 | +$purge_deleted_query = @" |
| 118 | +--Query 5 |
| 119 | +--This query statement for SCOM 2012 will purge all IsDeleted=1 objects immediately |
| 120 | +--Normally this is a 2-3day wait before this would happen naturally |
| 121 | +--This only purges 10000 records. If you have more it will require multiple runs |
| 122 | +--Purge IsDeleted=1 data from the SCOM 2012 DB: |
| 123 | +DECLARE @TimeGenerated DATETIME, @BatchSize INT, @RowCount INT |
| 124 | +SET @TimeGenerated = GETUTCDATE() |
| 125 | +SET @BatchSize = 10000 |
| 126 | +EXEC p_DiscoveryDataPurgingByRelationship @TimeGenerated, @BatchSize, @RowCount |
| 127 | +EXEC p_DiscoveryDataPurgingByTypedManagedEntity @TimeGenerated, @BatchSize, @RowCount |
| 128 | +EXEC p_DiscoveryDataPurgingByBaseManagedEntity @TimeGenerated, @BatchSize, @RowCount |
| 129 | +"@ |
| 130 | + |
| 131 | +try{ |
| 132 | +Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query $purge_deleted_query -OutputSqlErrors $true |
| 133 | +Write-Host "Successfully Purged the OperationsManager DB of Deleted Data!" -ForegroundColor Green |
| 134 | +} |
| 135 | +catch |
| 136 | +{ |
| 137 | +Write-Error "Unable to Purge the Deleted Items from the OperationsManager DB`n`nCould not run the following command against the OperationsManager DB:`n$purge_deleted_query" |
| 138 | +} |
0 commit comments