Skip to content

Commit e2c084f

Browse files
Update Erase-BaseManagedEntity.ps1
1 parent 4f41db6 commit e2c084f

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

Powershell/Erase-BaseManagedEntity.ps1

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@ cls
1010
#-----------------------------------------------------------
1111
#In the format of: ServerName\SQLInstance
1212
#ex: SQL01\SCOM2019
13-
$SQLServer = "sql01"
13+
$SQLServer = "SQL01"
1414
$db1 = "OperationsManager"
1515

1616
#Name of Agent to Erase from SCOM
1717
#Fully Qualified (FQDN)
18-
$MachineToRemove = "Agent1.contoso.com"
18+
$MachineToRemove = "Agent1.contoso.com", "Agent2.contoso.com"
19+
#If you want to assume yes on all the questions asked typically.
20+
$yes = $false
1921
#endregion
2022

2123
#-----------------------------------------------------------
2224

2325
<#
24-
DO NOT EDIT PAST THIS POINT
26+
DO NOT EDIT PAST THIS POINT
2527
#>
2628

2729
if(!(Get-Command Invoke-Sqlcmd -ErrorAction SilentlyContinue))
2830
{
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+
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."
32+
break
3133
}
32-
34+
foreach($machine in $MachineToRemove)
35+
{
3336
$bme_query = @"
3437
--Query 1
3538
--First get the Base Managed Entity ID of the object you think is orphaned/bad/needstogo:
3639
--
37-
DECLARE @name varchar(255) = '%$MachineToRemove%'
40+
DECLARE @name varchar(255) = '%$machine%'
3841
--
3942
SELECT BaseManagedEntityId, FullName, DisplayName, IsDeleted, Path, Name
4043
FROM BaseManagedEntity WHERE FullName like @name OR DisplayName like @name
@@ -45,19 +48,24 @@ $BME_IDs = (Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query $bme_
4548

4649
if(!$BME_IDs)
4750
{
48-
Write-Warning "Unable to find any data in the OperationsManager DB associated with: $MachineToRemove"
51+
Write-Warning "Unable to find any data in the OperationsManager DB associated with: $machine"
4952
break
5053
}
5154

5255
Write-Host "Found the following data associated with: " -NoNewline
53-
Write-Host $MachineToRemove -ForegroundColor Green
56+
Write-Host $machine -ForegroundColor Green
5457
$BME_IDs | Select FullName, DisplayName, Path, IsDeleted, BaseManagedEntityId
5558
$count = $BME_IDs.Count
59+
if(!$yes)
60+
{
5661
do
5762
{
58-
$answer1 = Read-Host -Prompt "Do you want to delete the above $count item(s) from the OperationsManager DB? (Y/N)"
63+
$answer1 = Read-Host -Prompt "Do you want to delete the above $count item(s) from the OperationsManager DB? (Y/N)"
5964
}
6065
until ($answer1 -eq "y" -or $answer1 -eq "n")
66+
}
67+
else
68+
{$answer1 = 'y'}
6169
if($answer1 -eq "n")
6270
{ Write-Host "Exiting Script.."; break }
6371
foreach($BaseManagedEntityID in $BME_IDs)
@@ -99,26 +107,28 @@ $remove_count = (Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query
99107
"OperationsManager DB has " | Write-Host -NoNewline
100108
$remove_count | Write-Host -NoNewline -ForegroundColor Green
101109
" object(s) pending to Delete`n" | Write-Host
102-
110+
if(!$yes)
111+
{
103112
do
104113
{
105-
$answer2 = Read-Host -Prompt "Do you want to purge the deleted item(s) from the OperationsManager DB? (Y/N)"
114+
$answer2 = Read-Host -Prompt "Do you want to purge the deleted item(s) from the OperationsManager DB? (Y/N)"
106115
}
107116
until ($answer2 -eq "y" -or $answer2 -eq "n")
117+
}
108118

109119
$remove_pending_management = @"
110-
exec p_AgentPendingActionDeleteByAgentName "$MachineToRemove"
120+
exec p_AgentPendingActionDeleteByAgentName "$machine"
111121
"@
112122

113123
Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db1 -Query $remove_pending_management -OutputSqlErrors $true
114124

115-
Write-Host "Cleared $MachineToRemove from Pending Management List in SCOM Console." -ForegroundColor DarkGreen
125+
Write-Host "Cleared $machine from Pending Management List in SCOM Console." -ForegroundColor DarkGreen
116126

117127
$purge_deleted_query = @"
118128
--Query 5
119129
--This query statement for SCOM 2012 will purge all IsDeleted=1 objects immediately
120130
--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
131+
--This only purges 10000 records. If you have more it will require multiple runs
122132
--Purge IsDeleted=1 data from the SCOM 2012 DB:
123133
DECLARE @TimeGenerated DATETIME, @BatchSize INT, @RowCount INT
124134
SET @TimeGenerated = GETUTCDATE()
@@ -136,3 +146,4 @@ catch
136146
{
137147
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"
138148
}
149+
}

0 commit comments

Comments
 (0)