Skip to content

Commit 6e96fdf

Browse files
authored
Add Auto Detection of Management Server and SQL Server / DB
1 parent 97eaa11 commit 6e96fdf

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

Powershell/Erase-BaseManagedEntity.ps1

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,21 +335,29 @@ Function Erase-BaseManagedEntity
335335
}
336336
if (!$SqlServer)
337337
{
338-
$SqlServer = "MSSQL-2019\SCOM2019"
338+
$sqlInput = Get-ItemProperty -Path 'HKLM:\Software\Microsoft\System Center\2010\Common\Database\' | Select DatabaseName, DatabaseServerName
339+
$SqlServer = $sqlInput.DatabaseServerName
339340
}
340341

341342
if (!$Database)
342343
{
343-
$Database = "OperationsManager"
344+
if (!$sqlInput)
345+
{
346+
$sqlInput = Get-ItemProperty -Path 'HKLM:\Software\Microsoft\System Center\2010\Common\Database\' | Select DatabaseName, DatabaseServerName
347+
}
348+
$Database = $sqlInput.DatabaseName
344349
}
345-
<#
346350
if (!$Servers)
347351
{
348-
#Name of Agent to Erase from SCOM
352+
<#
353+
#Name of Server to Erase from SCOM DB
349354
#Fully Qualified (FQDN)
350355
[array]$Servers = "IIS-Server"
356+
#>
357+
Write-Warning "Missing Servers to be removed, run the script with -Servers argument. (ex: -Servers Agent1.contoso.com)"
358+
break
351359
}
352-
#>
360+
353361
if (!$AssumeYes)
354362
{
355363
#If you want to assume yes on all the questions asked typically.
@@ -376,7 +384,7 @@ DO NOT EDIT PAST THIS POINT
376384
$genericListType = [System.Collections.Generic.List``1]
377385
$genericList = $genericListType.MakeGenericType($agentManagedComputerType)
378386
$agentList = new-object $genericList.FullName
379-
foreach ($serv in $using:Agents)
387+
foreach ($serv in $using:Servers)
380388
{
381389
Write-Host "Deleting SCOM Agent: `'$serv`' from Agent Managed Computers"
382390
$agent = Get-SCOMAgent *$serv*
@@ -385,9 +393,11 @@ DO NOT EDIT PAST THIS POINT
385393
$genericReadOnlyCollectionType = [System.Collections.ObjectModel.ReadOnlyCollection``1]
386394
$genericReadOnlyCollection = $genericReadOnlyCollectionType.MakeGenericType($agentManagedComputerType)
387395
$agentReadOnlyCollection = new-object $genericReadOnlyCollection.FullName @( ,$agentList);
388-
try{
389-
$administration.DeleteAgentManagedComputers($agentReadOnlyCollection);
390-
}catch{Write-Host 'Unable to delete from Agent Managed Computers' -ForegroundColor Cyan}
396+
try
397+
{
398+
$administration.DeleteAgentManagedComputers($agentReadOnlyCollection);
399+
}
400+
catch { Write-Host 'Unable to delete from Agent Managed Computers' -ForegroundColor Cyan }
391401
} -ErrorAction Stop
392402
}
393403
catch
@@ -530,7 +540,7 @@ if ($ManagementServer -or $SqlServer -or $Database -or $Servers -or $AssumeYes -
530540
}
531541
else
532542
{
533-
<# Edit line 537 to modify the default command run when this script is executed.
543+
<# Edit line 547 to modify the default command run when this script is executed.
534544
Example:
535545
Erase-BaseManagedEntity -ManagementServer MS1-2019.contoso.com -SqlServer SQL-2019\SCOM2019 -Database OperationsManager -Servers Agent1.contoso.com, Agent2.contoso.com
536546
#>

0 commit comments

Comments
 (0)