Create Discovery for Agent via API

Hey Thwackers,

I'm looking to do targeted re-discoveries of some of our server nodes automatically via the API which includes both SNMPv2c, and agent nodes. The primary goal is to rediscover and add new volumes, however another goal as well would be to initially add volumes and interfaces once the nodes get created automatically via the API as well (or an agent is registered).

All the documentation is essentially for discovery of SNMP or WMI nodes. Does anyone happen to know how to adjust the XML for the script up on GitHub to accommodate for Agents? In my own digging, I've taken a look at some of the XML from creating a discovery via the web UI and have been attempting to inject it into the CorePluginConfiguration (with little success).

What I found are these settings but I'm not sure where they may go or which ones are required and the appropriate syntax either (or better yet... if this is even technically supported via the API yet)

<AgentsFilterDefinition>filter:/Orion.Nodes</AgentsFilterDefinition>
<AgentsFilterQuery>1=1</AgentsFilterQuery>

https://github.com/solarwinds/OrionSDK/blob/master/Samples/PowerShell/DiscoverSnmpV3Node.ps1

Any and all help and/or suggestions appreciated, thanks!

Parents
  • So I have not figured out the appropriate XML mark for including agents within the discovery through the API... that said I went a different route and am hitting this straight at the source in SQL. Since the discovery can be created in the Web UI to target and poll agents I thought why not just try copying the exact XML markup from the database in the PluginConfigurations column in the DiscoveryProfiles table... and insert my own discovery profile with that same (slightly edited) XML markup.

    Low and behold it works if you edit some key areas. Below is an example script that works to discover 1 SNMP node and 1 Agent node. I'll be building on this to make it better and for our needs but thought it'd be worth providing the concept out there.

    <# finding the default values SQL query:
    
    SELECT dp.Name, dp.IsHidden, dp.IsAutoImport, dp.Status, dp.StatusDescription
    , SUBSTRING(dp.PluginConfigurations, CHARINDEX('PreferredPollingMethod', dp.PluginConfigurations) + 23, 4) AS [PreferredPollingMethod]
    , SUBSTRING(dp.PluginConfigurations, CHARINDEX('DiscoverAgentNodes', dp.PluginConfigurations) + 19, 5) AS [DiscoverAgentNodes]
    , dp.PluginConfigurations
    FROM SolarWindsOrion.dbo.DiscoveryProfiles AS dp
    WHERE ( dp.Name IS NOT NULL AND dp.Name != '' )
    ORDER BY dp.Name DESC
    
    #>
    
    Import-Module -Name SwisPowerShell
    
    $hostname = 'x.x.y.y'
    $creds = Get-Credential
    
    $swis = Connect-Swis -Hostname $hostname -credential $creds
    
    $engineId = 'x'
    
    $agentIP = "z.x.c.v"
    $serverIP = "z.x.c.b"
    
    $rawXml = @"
    <?xml version="1.0" encoding="utf-16"?><PluginItems><knownTypes><ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><string>SolarWinds.Orion.Core.Models.Discovery.CoreDiscoveryPluginConfiguration,SolarWinds.Orion.Core.Models.V1</string><string>SolarWinds.VIM.Common.Configuration.VimDiscoveryPluginConfiguration,SolarWinds.VIM.Common</string><string>SolarWinds.NCM.Contracts.Discovery.NcmDiscoveryPluginConfiguration,SolarWinds.NCM.Contracts</string><string>SolarWinds.Interfaces.Common.Models.Discovery.InterfacesDiscoveryPluginConfiguration,SolarWinds.Interfaces.Common</string><string>SolarWinds.APM.BlackBox.Sql.Common.Models.Discovery.ApmBlackBoxSqlDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.Sql.Common</string><string>SolarWinds.APM.BlackBox.Exchg.Common.Models.Discovery.ExchangeDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.Exchg.Common</string><string>SolarWinds.APM.BlackBox.Wstm.Common.Models.Discovery.DiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.Wstm.Common</string><string>SolarWinds.APM.BlackBox.IIS.Common.Models.Discovery.IISDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.IIS.Common</string><string>SolarWinds.APM.BlackBox.ActiveDirectory.Common.Models.Discovery.ActiveDirectoryDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.ActiveDirectory.Common</string><string>SolarWinds.Orion.SCM.Models.Discovery.ScmDiscoveryPluginConfiguration,SolarWinds.Orion.SCM.Models</string></ArrayOfstring></knownTypes><pluginItem><ArrayOfDiscoveryPluginConfigurationBase xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SolarWinds.Orion.Core.Models.Discovery"><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/Orion" i:type="d2p1:CoreDiscoveryPluginConfiguration"><d2p1:ActiveDirectoryList /><d2p1:AddressRange /><d2p1:AgentsAddresses xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /><d2p1:AgentsFilterDefinition>filter:/Orion.Nodes[IP_Address=''$agentIP'']</d2p1:AgentsFilterDefinition><d2p1:AgentsFilterQuery>Nodes.IP_Address = ''$agentIP''</d2p1:AgentsFilterQuery><d2p1:AutoImportVolumeTypes xmlns:d3p1="http://schemas.datacontract.org/2004/07/SolarWinds.Common.Snmp"><d3p1:VolumeType>Unknown</d3p1:VolumeType><d3p1:VolumeType>Other</d3p1:VolumeType><d3p1:VolumeType>RAM</d3p1:VolumeType><d3p1:VolumeType>VirtualMemory</d3p1:VolumeType><d3p1:VolumeType>FixedDisk</d3p1:VolumeType><d3p1:VolumeType>RAMDisk</d3p1:VolumeType><d3p1:VolumeType>FlashMemory</d3p1:VolumeType><d3p1:VolumeType>NetworkDisk</d3p1:VolumeType><d3p1:VolumeType>MountPoint</d3p1:VolumeType></d2p1:AutoImportVolumeTypes><d2p1:BulkList xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><d3p1:string>$serverIP</d3p1:string></d2p1:BulkList><d2p1:Credentials i:nil="true" /><d2p1:DiscoverAgentNodes>true</d2p1:DiscoverAgentNodes><d2p1:PreferredPollingMethod>SNMP</d2p1:PreferredPollingMethod><d2p1:SharedCredentials xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><d3p1:KeyValueOfintint><d3p1:Key>0</d3p1:Key><d3p1:Value>8</d3p1:Value><d3p1:Key>1</d3p1:Key><d3p1:Value>12</d3p1:Value></d3p1:KeyValueOfintint></d2p1:SharedCredentials><d2p1:SubnetList /><d2p1:WMICredentials /><d2p1:WmiRetries>1</d2p1:WmiRetries><d2p1:WmiRetryInterval>PT10S</d2p1:WmiRetryInterval></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/Virtualization" i:type="d2p1:VimDiscoveryPluginConfiguration"><d2p1:IsDiscoveryForVimEnabled>false</d2p1:IsDiscoveryForVimEnabled><d2p1:SelectedVmWareCredentials xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2013/Ncm" i:type="d2p1:NcmDiscoveryPluginConfiguration"><d2p1:AccountID>admin</d2p1:AccountID></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/Interfaces" i:type="d2p1:InterfacesDiscoveryPluginConfiguration"><d2p1:AutoImportExpressionFilter xmlns:d3p1="http://schemas.datacontract.org/2004/07/" i:nil="true" /><d2p1:AutoImportStatus xmlns:d3p1="http://schemas.datacontract.org/2004/07/SolarWinds.Interfaces.Common.Enums"><d3p1:IfAutoImportStatus>Up</d3p1:IfAutoImportStatus></d2p1:AutoImportStatus><d2p1:AutoImportVirtualTypes xmlns:d3p1="http://schemas.datacontract.org/2004/07/System"><d3p1:boolean>true</d3p1:boolean><d3p1:boolean>false</d3p1:boolean><d3p1:boolean i:nil="true" /></d2p1:AutoImportVirtualTypes><d2p1:AutoImportVlanPortTypes xmlns:d3p1="http://schemas.datacontract.org/2004/07/SolarWinds.Orion.Core.Models.Enums"><d3p1:VlanPortType>Access</d3p1:VlanPortType><d3p1:VlanPortType>Trunk</d3p1:VlanPortType><d3p1:VlanPortType>Unknown</d3p1:VlanPortType></d2p1:AutoImportVlanPortTypes></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:ApmBlackBoxSqlDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:ExchangeDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:DiscoveryPluginConfiguration"><d2p1:EnableAutoImport>false</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:IISDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:ActiveDirectoryDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/SCM" i:type="d2p1:ScmDiscoveryPluginConfiguration"><d2p1:BuiltInProfiles xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><d3p1:int>1</d3p1:int><d3p1:int>2</d3p1:int><d3p1:int>3</d3p1:int><d3p1:int>4</d3p1:int><d3p1:int>5</d3p1:int><d3p1:int>6</d3p1:int><d3p1:int>7</d3p1:int><d3p1:int>8</d3p1:int><d3p1:int>9</d3p1:int></d2p1:BuiltInProfiles><d2p1:IsAutoImport>true</d2p1:IsAutoImport></DiscoveryPluginConfigurationBase></ArrayOfDiscoveryPluginConfigurationBase></pluginItem></PluginItems>
    "@
    
    $sqlQuery = @"
    UPDATE dp
    SET dp.PluginConfigurations = '$rawXml'
    SolarWindsOrion.dbo.DiscoveryProfiles AS dp
    WHERE dp.ProfileID = $DiscoveryProfileID
    "@
    
    $discoveryName = "Scripted Discovery - $([DateTime]::Now)"
    $discoveryDescription = "Discovery created via API and direct SQL"
    
    $sqlInsertDiscovery = @"
    INSERT INTO SolarWindsOrion.dbo.DiscoveryProfiles
    VALUES ('$discoveryName', '$discoveryDescription',0,NULL,$engineId,4,'00000000-0000-0000-0000-000000000000',0,0,2000,3000,1,1800,1,1,0,0,0,0,60,NULL,NULL,'',0,'$rawXml',1,0,0,161,'<?xml version="1.0"?>  <ReportSchedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:nil="true" />')
    "@
    
    $sqlResult1 = (Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Reporting' -Verb 'ExecuteSQL' -Arguments @($sqlInsertDiscovery)).ChildNodes.DocumentElement.ExecuteSqlResults
    
    $profileIdQuery = @"
    SELECT DISTINCT TOP 1 dp.ProfileID 
    FROM Orion.DiscoveryProfiles AS dp
    WHERE ( dp.Name = '$discoveryName' AND dp.Description = '$discoveryDescription' AND dp.EngineID = $engineId )
    "@
    
    $discoveryProfileID = Get-SwisData -SwisConnection $swis -Query $profileIdQuery
    
    $null = Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Discovery' StartDiscoveryProfile @($discoveryProfileID)
    
    Write-Host -NoNewline "Discovery profile #$discoveryProfileID running..."
    
    $statusQuery = "SELECT dp.Status FROM Orion.DiscoveryProfiles AS dp WHERE dp.ProfileID = $discoveryProfileID"
    
    do {
    
        Write-Host -NoNewline "."
        Start-Sleep -Seconds 1
        $Status = Get-SwisData -SwisConnection $swis -Query $statusQuery
    
    } while ($Status -eq 1)
    
    $resultQuery = "SELECT dl.Result, dl.ResultDescription, dl.ErrorMessage, dl.BatchID FROM Orion.DiscoveryLogs AS dl WHERE dl.ProfileID = $discoveryProfileID"
    
    $Result = Get-SwisData -SwisConnection $swis -Query $resultQuery
    
    switch ($Result.Result) {
        0 {"Unknown"}
        1 {"InProgress"}
        2 {"Finished"}
        3 {"Error"}
        4 {"NotScheduled"}
        5 {"Scheduled"}
        6 {"NotCompleted"}
        7 {"Canceling"}
        8 {"ReadyForImport"}
    }
    $Result.ResultDescription
    $Result.ErrorMessage
    
    $batchQuery = "SELECT dli.EntityType, dli.DisplayName, dli.NetObjectID FROM Orion.DiscoveryLogItems AS dli WHERE dli.BatchID = '" + $Result.BatchID + "'"
    
    $sqlDiscoveryCleanupQuery = @"
        -- I don't want discovery automatically adding appinsight templates
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredExchangeServers]
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredBlackBoxWstmInstances]
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredBlackBoxIISInstances]
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredBlackBoxSqlInstances]
      
        DELETE dv --cleaning up volumes
        --select *
        FROM solarwindsorion.dbo.DiscoveredVolumes AS dv
        INNER JOIN SolarWindsOrion.dbo.discoveredNodes AS dn ON ( dn.nodeid = dv.DiscoveredNodeID AND dn.ProfileID = dv.ProfileID )
        WHERE dv.VolumeType NOT IN ( 4 ) --only want fixed disks
        OR dn.Vendor NOT IN ( 'Windows', 'Linux', 'net-snmp' ) --keep drives for servers, remove them for anything else
        
        DELETE di1
        FROM SolarWindsOrion.dbo.DiscoveredInterfaces AS di1 --all interfaces rule
        WHERE InterfaceTypeDescription in ('Loopback','VMware Virtual Network Interface','Proprietary Multiplexor') --we currently never monitor these types of interfaces and can delete them wherever they show up
        OR OperStatus != 1 --we don't want to add new interfaces in any status beside up
        OR InterfaceName LIKE '%miniport%' -- these are useless sub interfaces that show up on windows snmp nodes
        OR InterfaceName LIKE '%-0000' -- these are useless sub interfaces that show up on windows snmp nodes
    "@
        
        $sqlResult2 = (Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Reporting' -Verb 'ExecuteSQL' -Arguments @($sqlDiscoveryCleanupQuery)).ChildNodes.DocumentElement.ExecuteSqlResults
    
    $discoveredNodeQuery = @"
        SELECT dn.NodeID FROM Orion.DiscoveredNodes AS dn WHERE dn.ProfileID = $discoveryProfileID
    "@
    
    $discoveredNodes = Get-SwisData -SwisConnection $swis -Query $discoveredNodeQuery
    
    $nodesToImportXml = ""
    $dnCount = 0
    $dnTotal = $discoveredNodes.Count
    
    ForEach($dn in $discoveredNodes) {
    
        [string]$NodeID = $dn
    
        $nodesToImportXml += "`t`t<a:int>$NodeID</a:int>"
    
    If ($dnCount -lt $dnTotal-1) {
    
        $nodesToImportXml += "`n"
    
    }
    
    $dnCount ++
    
    }
    
    $importXml = @"
        <DiscoveryImportConfiguration xmlns='http://schemas.solarwinds.com/2008/Core'>
            <DeleteProfileAfterImport>false</DeleteProfileAfterImport>
            <NodeIDs xmlns:a='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>`n
    "@
    $importXml += $nodesToImportXml
    $importXml += @"
    `n        </NodeIDs>
            <ProfileID>$discoveryProfileID</ProfileID>
        </DiscoveryImportConfiguration>
    "@
    
        $importConfiguration = ([xml]$importXml).DocumentElement
    
        $importResult = Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Discovery' -Verb 'ImportDiscoveryResults' -Arguments @($importConfiguration)
    
    if ($Result.Result -eq 2) {
    
        $Discovered = Get-SwisData -SwisConnection $swis -Query $batchQuery 
        "$($Discovered.Count) items imported."
        $Discovered
    
    }

Reply
  • So I have not figured out the appropriate XML mark for including agents within the discovery through the API... that said I went a different route and am hitting this straight at the source in SQL. Since the discovery can be created in the Web UI to target and poll agents I thought why not just try copying the exact XML markup from the database in the PluginConfigurations column in the DiscoveryProfiles table... and insert my own discovery profile with that same (slightly edited) XML markup.

    Low and behold it works if you edit some key areas. Below is an example script that works to discover 1 SNMP node and 1 Agent node. I'll be building on this to make it better and for our needs but thought it'd be worth providing the concept out there.

    <# finding the default values SQL query:
    
    SELECT dp.Name, dp.IsHidden, dp.IsAutoImport, dp.Status, dp.StatusDescription
    , SUBSTRING(dp.PluginConfigurations, CHARINDEX('PreferredPollingMethod', dp.PluginConfigurations) + 23, 4) AS [PreferredPollingMethod]
    , SUBSTRING(dp.PluginConfigurations, CHARINDEX('DiscoverAgentNodes', dp.PluginConfigurations) + 19, 5) AS [DiscoverAgentNodes]
    , dp.PluginConfigurations
    FROM SolarWindsOrion.dbo.DiscoveryProfiles AS dp
    WHERE ( dp.Name IS NOT NULL AND dp.Name != '' )
    ORDER BY dp.Name DESC
    
    #>
    
    Import-Module -Name SwisPowerShell
    
    $hostname = 'x.x.y.y'
    $creds = Get-Credential
    
    $swis = Connect-Swis -Hostname $hostname -credential $creds
    
    $engineId = 'x'
    
    $agentIP = "z.x.c.v"
    $serverIP = "z.x.c.b"
    
    $rawXml = @"
    <?xml version="1.0" encoding="utf-16"?><PluginItems><knownTypes><ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><string>SolarWinds.Orion.Core.Models.Discovery.CoreDiscoveryPluginConfiguration,SolarWinds.Orion.Core.Models.V1</string><string>SolarWinds.VIM.Common.Configuration.VimDiscoveryPluginConfiguration,SolarWinds.VIM.Common</string><string>SolarWinds.NCM.Contracts.Discovery.NcmDiscoveryPluginConfiguration,SolarWinds.NCM.Contracts</string><string>SolarWinds.Interfaces.Common.Models.Discovery.InterfacesDiscoveryPluginConfiguration,SolarWinds.Interfaces.Common</string><string>SolarWinds.APM.BlackBox.Sql.Common.Models.Discovery.ApmBlackBoxSqlDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.Sql.Common</string><string>SolarWinds.APM.BlackBox.Exchg.Common.Models.Discovery.ExchangeDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.Exchg.Common</string><string>SolarWinds.APM.BlackBox.Wstm.Common.Models.Discovery.DiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.Wstm.Common</string><string>SolarWinds.APM.BlackBox.IIS.Common.Models.Discovery.IISDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.IIS.Common</string><string>SolarWinds.APM.BlackBox.ActiveDirectory.Common.Models.Discovery.ActiveDirectoryDiscoveryPluginConfiguration,SolarWinds.APM.BlackBox.ActiveDirectory.Common</string><string>SolarWinds.Orion.SCM.Models.Discovery.ScmDiscoveryPluginConfiguration,SolarWinds.Orion.SCM.Models</string></ArrayOfstring></knownTypes><pluginItem><ArrayOfDiscoveryPluginConfigurationBase xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SolarWinds.Orion.Core.Models.Discovery"><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/Orion" i:type="d2p1:CoreDiscoveryPluginConfiguration"><d2p1:ActiveDirectoryList /><d2p1:AddressRange /><d2p1:AgentsAddresses xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /><d2p1:AgentsFilterDefinition>filter:/Orion.Nodes[IP_Address=''$agentIP'']</d2p1:AgentsFilterDefinition><d2p1:AgentsFilterQuery>Nodes.IP_Address = ''$agentIP''</d2p1:AgentsFilterQuery><d2p1:AutoImportVolumeTypes xmlns:d3p1="http://schemas.datacontract.org/2004/07/SolarWinds.Common.Snmp"><d3p1:VolumeType>Unknown</d3p1:VolumeType><d3p1:VolumeType>Other</d3p1:VolumeType><d3p1:VolumeType>RAM</d3p1:VolumeType><d3p1:VolumeType>VirtualMemory</d3p1:VolumeType><d3p1:VolumeType>FixedDisk</d3p1:VolumeType><d3p1:VolumeType>RAMDisk</d3p1:VolumeType><d3p1:VolumeType>FlashMemory</d3p1:VolumeType><d3p1:VolumeType>NetworkDisk</d3p1:VolumeType><d3p1:VolumeType>MountPoint</d3p1:VolumeType></d2p1:AutoImportVolumeTypes><d2p1:BulkList xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><d3p1:string>$serverIP</d3p1:string></d2p1:BulkList><d2p1:Credentials i:nil="true" /><d2p1:DiscoverAgentNodes>true</d2p1:DiscoverAgentNodes><d2p1:PreferredPollingMethod>SNMP</d2p1:PreferredPollingMethod><d2p1:SharedCredentials xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><d3p1:KeyValueOfintint><d3p1:Key>0</d3p1:Key><d3p1:Value>8</d3p1:Value><d3p1:Key>1</d3p1:Key><d3p1:Value>12</d3p1:Value></d3p1:KeyValueOfintint></d2p1:SharedCredentials><d2p1:SubnetList /><d2p1:WMICredentials /><d2p1:WmiRetries>1</d2p1:WmiRetries><d2p1:WmiRetryInterval>PT10S</d2p1:WmiRetryInterval></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/Virtualization" i:type="d2p1:VimDiscoveryPluginConfiguration"><d2p1:IsDiscoveryForVimEnabled>false</d2p1:IsDiscoveryForVimEnabled><d2p1:SelectedVmWareCredentials xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2013/Ncm" i:type="d2p1:NcmDiscoveryPluginConfiguration"><d2p1:AccountID>admin</d2p1:AccountID></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/Interfaces" i:type="d2p1:InterfacesDiscoveryPluginConfiguration"><d2p1:AutoImportExpressionFilter xmlns:d3p1="http://schemas.datacontract.org/2004/07/" i:nil="true" /><d2p1:AutoImportStatus xmlns:d3p1="http://schemas.datacontract.org/2004/07/SolarWinds.Interfaces.Common.Enums"><d3p1:IfAutoImportStatus>Up</d3p1:IfAutoImportStatus></d2p1:AutoImportStatus><d2p1:AutoImportVirtualTypes xmlns:d3p1="http://schemas.datacontract.org/2004/07/System"><d3p1:boolean>true</d3p1:boolean><d3p1:boolean>false</d3p1:boolean><d3p1:boolean i:nil="true" /></d2p1:AutoImportVirtualTypes><d2p1:AutoImportVlanPortTypes xmlns:d3p1="http://schemas.datacontract.org/2004/07/SolarWinds.Orion.Core.Models.Enums"><d3p1:VlanPortType>Access</d3p1:VlanPortType><d3p1:VlanPortType>Trunk</d3p1:VlanPortType><d3p1:VlanPortType>Unknown</d3p1:VlanPortType></d2p1:AutoImportVlanPortTypes></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:ApmBlackBoxSqlDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:ExchangeDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:DiscoveryPluginConfiguration"><d2p1:EnableAutoImport>false</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:IISDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2007/08/APM" i:type="d2p1:ActiveDirectoryDiscoveryPluginConfiguration"><d2p1:EnableAutoImport>true</d2p1:EnableAutoImport></DiscoveryPluginConfigurationBase><DiscoveryPluginConfigurationBase xmlns:d2p1="http://schemas.solarwinds.com/2008/SCM" i:type="d2p1:ScmDiscoveryPluginConfiguration"><d2p1:BuiltInProfiles xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><d3p1:int>1</d3p1:int><d3p1:int>2</d3p1:int><d3p1:int>3</d3p1:int><d3p1:int>4</d3p1:int><d3p1:int>5</d3p1:int><d3p1:int>6</d3p1:int><d3p1:int>7</d3p1:int><d3p1:int>8</d3p1:int><d3p1:int>9</d3p1:int></d2p1:BuiltInProfiles><d2p1:IsAutoImport>true</d2p1:IsAutoImport></DiscoveryPluginConfigurationBase></ArrayOfDiscoveryPluginConfigurationBase></pluginItem></PluginItems>
    "@
    
    $sqlQuery = @"
    UPDATE dp
    SET dp.PluginConfigurations = '$rawXml'
    SolarWindsOrion.dbo.DiscoveryProfiles AS dp
    WHERE dp.ProfileID = $DiscoveryProfileID
    "@
    
    $discoveryName = "Scripted Discovery - $([DateTime]::Now)"
    $discoveryDescription = "Discovery created via API and direct SQL"
    
    $sqlInsertDiscovery = @"
    INSERT INTO SolarWindsOrion.dbo.DiscoveryProfiles
    VALUES ('$discoveryName', '$discoveryDescription',0,NULL,$engineId,4,'00000000-0000-0000-0000-000000000000',0,0,2000,3000,1,1800,1,1,0,0,0,0,60,NULL,NULL,'',0,'$rawXml',1,0,0,161,'<?xml version="1.0"?>  <ReportSchedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:nil="true" />')
    "@
    
    $sqlResult1 = (Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Reporting' -Verb 'ExecuteSQL' -Arguments @($sqlInsertDiscovery)).ChildNodes.DocumentElement.ExecuteSqlResults
    
    $profileIdQuery = @"
    SELECT DISTINCT TOP 1 dp.ProfileID 
    FROM Orion.DiscoveryProfiles AS dp
    WHERE ( dp.Name = '$discoveryName' AND dp.Description = '$discoveryDescription' AND dp.EngineID = $engineId )
    "@
    
    $discoveryProfileID = Get-SwisData -SwisConnection $swis -Query $profileIdQuery
    
    $null = Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Discovery' StartDiscoveryProfile @($discoveryProfileID)
    
    Write-Host -NoNewline "Discovery profile #$discoveryProfileID running..."
    
    $statusQuery = "SELECT dp.Status FROM Orion.DiscoveryProfiles AS dp WHERE dp.ProfileID = $discoveryProfileID"
    
    do {
    
        Write-Host -NoNewline "."
        Start-Sleep -Seconds 1
        $Status = Get-SwisData -SwisConnection $swis -Query $statusQuery
    
    } while ($Status -eq 1)
    
    $resultQuery = "SELECT dl.Result, dl.ResultDescription, dl.ErrorMessage, dl.BatchID FROM Orion.DiscoveryLogs AS dl WHERE dl.ProfileID = $discoveryProfileID"
    
    $Result = Get-SwisData -SwisConnection $swis -Query $resultQuery
    
    switch ($Result.Result) {
        0 {"Unknown"}
        1 {"InProgress"}
        2 {"Finished"}
        3 {"Error"}
        4 {"NotScheduled"}
        5 {"Scheduled"}
        6 {"NotCompleted"}
        7 {"Canceling"}
        8 {"ReadyForImport"}
    }
    $Result.ResultDescription
    $Result.ErrorMessage
    
    $batchQuery = "SELECT dli.EntityType, dli.DisplayName, dli.NetObjectID FROM Orion.DiscoveryLogItems AS dli WHERE dli.BatchID = '" + $Result.BatchID + "'"
    
    $sqlDiscoveryCleanupQuery = @"
        -- I don't want discovery automatically adding appinsight templates
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredExchangeServers]
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredBlackBoxWstmInstances]
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredBlackBoxIISInstances]
        DELETE FROM [SolarWindsOrion].[dbo].[APM_DiscoveredBlackBoxSqlInstances]
      
        DELETE dv --cleaning up volumes
        --select *
        FROM solarwindsorion.dbo.DiscoveredVolumes AS dv
        INNER JOIN SolarWindsOrion.dbo.discoveredNodes AS dn ON ( dn.nodeid = dv.DiscoveredNodeID AND dn.ProfileID = dv.ProfileID )
        WHERE dv.VolumeType NOT IN ( 4 ) --only want fixed disks
        OR dn.Vendor NOT IN ( 'Windows', 'Linux', 'net-snmp' ) --keep drives for servers, remove them for anything else
        
        DELETE di1
        FROM SolarWindsOrion.dbo.DiscoveredInterfaces AS di1 --all interfaces rule
        WHERE InterfaceTypeDescription in ('Loopback','VMware Virtual Network Interface','Proprietary Multiplexor') --we currently never monitor these types of interfaces and can delete them wherever they show up
        OR OperStatus != 1 --we don't want to add new interfaces in any status beside up
        OR InterfaceName LIKE '%miniport%' -- these are useless sub interfaces that show up on windows snmp nodes
        OR InterfaceName LIKE '%-0000' -- these are useless sub interfaces that show up on windows snmp nodes
    "@
        
        $sqlResult2 = (Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Reporting' -Verb 'ExecuteSQL' -Arguments @($sqlDiscoveryCleanupQuery)).ChildNodes.DocumentElement.ExecuteSqlResults
    
    $discoveredNodeQuery = @"
        SELECT dn.NodeID FROM Orion.DiscoveredNodes AS dn WHERE dn.ProfileID = $discoveryProfileID
    "@
    
    $discoveredNodes = Get-SwisData -SwisConnection $swis -Query $discoveredNodeQuery
    
    $nodesToImportXml = ""
    $dnCount = 0
    $dnTotal = $discoveredNodes.Count
    
    ForEach($dn in $discoveredNodes) {
    
        [string]$NodeID = $dn
    
        $nodesToImportXml += "`t`t<a:int>$NodeID</a:int>"
    
    If ($dnCount -lt $dnTotal-1) {
    
        $nodesToImportXml += "`n"
    
    }
    
    $dnCount ++
    
    }
    
    $importXml = @"
        <DiscoveryImportConfiguration xmlns='http://schemas.solarwinds.com/2008/Core'>
            <DeleteProfileAfterImport>false</DeleteProfileAfterImport>
            <NodeIDs xmlns:a='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>`n
    "@
    $importXml += $nodesToImportXml
    $importXml += @"
    `n        </NodeIDs>
            <ProfileID>$discoveryProfileID</ProfileID>
        </DiscoveryImportConfiguration>
    "@
    
        $importConfiguration = ([xml]$importXml).DocumentElement
    
        $importResult = Invoke-SwisVerb -SwisConnection $swis -EntityName 'Orion.Discovery' -Verb 'ImportDiscoveryResults' -Arguments @($importConfiguration)
    
    if ($Result.Result -eq 2) {
    
        $Discovered = Get-SwisData -SwisConnection $swis -Query $batchQuery 
        "$($Discovered.Count) items imported."
        $Discovered
    
    }

Children
No Data