Menu Close

Delete SCOM Network Devices using PowerShell

There are times when you may wish to automate deleting objects in SCOM.  The safest method is always using the Console, or the SDK to delete objects in SCOM, in a manner they were designed to be deleted.

There is an option in the SCOM Console to delete discovered Network Devices

image

However, there is not a PowerShell command in SCOM to delete these objects.

Below, is an example of using a method from the Microsoft.EnterpriseManagement.Administration namespace, in PowerShell, which will allow you to do this.

It involves creating a Generic Collection/List, of the device type you wish to delete (Microsoft.EnterpriseManagement.Administration.RemotelyManagedDevice), then adding devices to the collection by using “GetAllRemotelyManagedDevices()”, then calling the method “DeleteRemotelyManagedDevices”

# Begin Network Device Delete #================================================================================= # Get SCOM directory for binaries $SCOMRegKey = "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup" $SCOMPath = (Get-ItemProperty $SCOMRegKey).InstallDirectory $SCOMPath = $SCOMPath.TrimEnd("\") $SCOMSDKPath = "$SCOMPath\SDK Binaries" #Load SDK binaries $dummy = [System.Reflection.Assembly]::LoadFrom("$SCOMSDKPath\Microsoft.EnterpriseManagement.Core.dll") $dummy = [System.Reflection.Assembly]::LoadFrom("$SCOMSDKPath\Microsoft.EnterpriseManagement.OperationsManager.dll") $dummy = [System.Reflection.Assembly]::LoadFrom("$SCOMSDKPath\Microsoft.EnterpriseManagement.Runtime.dll") # Connect to management group and load the Administration namespace $MG = [Microsoft.EnterpriseManagement.ManagementGroup]::Connect("localhost") $Admin = $MG.GetAdministration() # Define generic collection list which is the required parameter for the SDK delete command $RemotelyManagedDeviceType = [Microsoft.EnterpriseManagement.Administration.RemotelyManagedDevice]; $GenericListType = [System.Collections.Generic.List``1] $GenericList = $GenericListType.MakeGenericType($RemotelyManagedDeviceType) $NDList = new-object $GenericList.FullName # Get all Network Devices $NetworkDevices = $Admin.GetAllRemotelyManagedDevices() #Loop through each Network Device and add the ones we want to the generic list based on some criteria FOREACH ($NetworkDevice in $NetworkDevices) { #Example of using a specific displayname as the criteria to add to the list IF ($NetworkDevice.DisplayName -eq "PRN") { # Add our network device to the collection for deletion $NDList.Add($NetworkDevice) } } #Delete network devices $Admin.DeleteRemotelyManagedDevices($NDList) #================================================================================= # End Network Device Delete

You can adapt this to other device types available in the same namespace, documented here:

https://docs.microsoft.com/en-us/previous-versions/system-center/developer/bb438221(v=msdn.10)

1 Comment

  1. Adam N

    Thanks for sharing, unfortunately I tried testing this for a single network device in SCOM 2019, and while it did remove the device from Administration workspace, orphaned bits were left in Monitoring, Network Devices view and when searching using PowerShell and SQL.

    I regret not attempting Remove-SCOMDisabledClassInstance and perhaps that’s all that was needed. I did manage to remove the objects using SQL. Just thought I’d let you know, perhaps there is a fix or important caveat.

    Was hoping to adopt this for a PowerShell GUI I’ve created.

Leave a Reply

Your email address will not be published.