SCOM – Add network device to network discovery rule via PowerShell


I posted a powershell script here to create a new network discovery rule. You can only create one rule per management server, so once created you just update with new devices.

The script bellows doesn't create the discovery rule, just adds a single device to a current rule.

 


# Name: Add-Device-to-Network-Discovery-Rule.ps1
# Written by: Rafaela Brownlie
# Date: 20/06/17
# Description: The script programmatically builds SNMP run as account, and adds a new host/ip to an existing network discovery rule
# $OMFQDN stores the FQDN for the SCOM server
# $HostIP = Network device ip address
# $networkdiscoveryname = stores the name of the discovery rule to be updated.
# I wrote it for SCOM 2012 R2. I haven't tested on SCOM 2016, but I believe it should work.


$OMFQDN = ""
$hostIP = ""
$networkdiscoveryname = ""


New-SCOMManagementGroupConnection -ComputerName $OMFQDN

Get-SCOMManagementGroupConnection


$mg = Get-SCOMManagementGroup -ComputerName $OMFQDN

$discovery = $mg.NetworkDiscovery.GetNetworkDiscovery($NetworkDiscoveryName)


$Account = Get-SCOMrunAsAccount -Name "SNMPv2 Account"

If($Account -eq $null)
{


Write-Host "Creating account" -ForegroundColor Magenta
# $a = Get-Credential -Message "Enter community string in password field"
Write-Host "Creating account" -ForegroundColor Magenta
Add-SCOMRunAsAccount -Name "SNMPv2 Account" -Description "Account used for Network device monitoring" -CommunityString -String $a.Password
$Account = Get-SCOMrunAsAccount -Name "SNMPv2 Account"
Write-Host "distributing account" -ForegroundColor Magenta
Set-SCOMRunAsDistribution -RunAsAccount $Account -LessSecure

Write-Host "Waiting for account to be created" -backgroundColor Magenta


sleep -Seconds 60
}


$Profile = Get-SCOMRunAsProfile -DisplayName "SNMP Monitoring Account"
Set-SCOMRunAsProfile -Action "Add" -Profile $Profile -Account $Account



Write-Host "creating SNMP host object" -ForegroundColor Magenta
$SNMPCommunity =[Microsoft.EnterpriseManagement.NetworkMonitoring.Snmpv1Community]::create($mg, $account
$snmpHost = New-Object Microsoft.EnterpriseManagement.NetworkMonitoring.SnmpHost


#my snmp host IP addresses.
$snmpHost.Host = $hostIP
$snmpHost.Community = $SNMPCommunity
#you could add more than one host here:
$discovery.Configuration.Seeds.add($snmphost)
$discovery.configuration.Communities.add($SNMPCommunity)


$mg.NetworkDiscovery.UpdateNetworkDiscovery($discovery)
#allow some time for discovery creation
sleep -Seconds 40
#Run the discovery once it's created:
Start-SCOMTask -Task (get-scomtask -name System.NetworkManagement.FullOnDemandDiscovery) -Instance (Get-SCOMMonitoringObject -Class (Get-SCOMClass -DisplayName "Network Discovery Server"))


Comments (0)

Skip to main content