This answer is just an extension / special case of the answer from @Mohamed El-Qassas MVP♦
In our case the script
Get-SPContentDatabase | ?{$_.NeedsUpgrade -eq $true} | Upgrade-SPContentDatabase
did not work first at all, because Get-SPContentDatabase (without specifying the ID of the problematic DB explicitly) did not returned the content DB suffering from the problem. As it turned out shortly, the reason was, that the content DB was not assigned to any web application. It was removed from its web application previously in an attempt to fix an issue with a CU installation (see https://sharepointumar.wordpress.com/2019/07/06/psconfig-failed-with-attempt-to-register-null-pointer-at-sharepoint-2019/), like:
$wa.ContentDatabases.Delete('7f29fee9-4827-403e-ba23-11e0e34e16c2')
The database was returned however by the cmdlet Get-SPDatabase, so a possible workaround could have been:
Get-SPDatabase | ? { $_.Type -eq 'Content Database' } | % { Get-SPContentDatabase $_.ID } | ? { $_.NeedsUpgrade } | % {
Write-Host Upgrading content DB $_.Name with ID $_.ID
Upgrade-SPContentDatabase $_.ID -Confirm:$false
}
Unfortunately, it did not help a lot. Although it resulted first:
100.00% : SPContentDatabase Name=YourContentDB
Finalizing the upgrade...
Finaly we received an error:
Upgrade-SPContentDatabase : Fail to find the parent SPWebApplication when upgrading Content Database SPContentDatabase
Name=YourContentDB
Lessons learned:
Get-SPContentDatabase returns only content DBs bound to a web
application, if the ID of the DB is not explicitly specified. It seems to iterate through the web applications and displays the content DBs for each. It consider it to be a bug.
- You can only upgrade content DBs by
Upgrade-SPContentDatabase that are
attached to a web application. This one might be a result of the first point, however.
How we solved the issue?
First attach the content DB back to the web application:
$db = Get-SPContentDatabase 7f29fee9-4827-403e-ba23-11e0e34e16c2
$wa.ContentDatabases.Add($db)
And upgrade the DB in the next (and last) step:
Upgrade-SPContentDatabase $db.Id -Confirm:$false
Just a last comment yet. We've also tried to upgrade the DB like
$db.Upgrade($true)
but it resulted in a failure either:
Exception calling "Upgrade" with "1" argument(s): "The global session cannot be used to perform an upgrade operation."