Script discovery only returns one instance

Hi, my brain doesen’t work today and I need a pair of fresh eyes. Im creating a powershell script based discovery to create an instance for all of our customers in the microsoft CSP program.

Out of 200+ customers, only one is created in SCOM. I have created similar discoveries with foreach a few other times without issues, but i cannot see my error here.

#GET ALL CUSTOMERS FROM OUR CSP PORTAL
#Only properties needed are selected.
$customerUri = $PartnerCenterPreFix + "/customers"
try {
    $Customers = (Invoke-RestMethod -Uri $customerUri -Method Get -Headers $CSPheaders).items | Select-Object id, @{N = 'domain'; E = {$_.companyProfile.domain}}, @{N = 'companyName'; E = {$_.companyProfile.companyName}}
}

catch {
    $api.LogScriptEvent("GetCSPCustomers.ps1", 7005, 0, "Failed to retrieve CSP Customers from API $_")
}

#Loop through the customers and add them as instances.
if ($Customers.count -gt "0") {
	$Count = $Customers.count
	$api.LogScriptEvent("GetCSPCustomers.ps1", 7002, 0, "Found $count Customers")
    foreach ($customer in $customers) {
			$Instance = $null
			$CompanyName = $Customer.companyName
            #Create a class instace
            $instance = $discoveryData.CreateClassInstance("$MPElement[Name='Intility.Office365.CSPCustomer.Class']$")
            #add class properties
            $instance.AddProperty("$MPElement[Name='Office365.CSPCustomer.Class']/CustomerName$", $CompanyName)
            $instance.AddProperty("$MPElement[Name='Office365.CSPCustomer.Class']/URL$", $customer.domain)
            $instance.AddProperty("$MPElement[Name='Office365.CSPCustomer.Class']/CSPID$", $customer.id)
            #add entity
            $instance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $CompanyName + "(CSP Customer)")
            $instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $computerName) 

            #write data
			$api.LogScriptEvent("GetCSPCustomers.ps1", 7003, 0, "Adding $CompanyName")
            $discoveryData.AddInstance($instance) 
        }
    }

#Log script execution time
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds

$api.LogScriptEvent("GetCSPCustomers.ps1", 7004, 0, "Discovery Ended - execution time $scripttime")
#Return data
$discoveryData

As you can see each customer is logged in the event log, and this works perfectly, but only one object is to find in SCOM – and it’s not the last one…

Hi

Which property is the key in the class definition?

you pointed me in the right direction. I forgot to define a key property :slight_smile: