What happens if you modify line 41 in the script of Mahdi Tehrani with this:
$ComputerName = (Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' -Server (Get-ADDomain).DNsroot -SearchBase $Path)
I've not tested this, maybe there is a syntax issue. But maybe this will help.
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten