Currently I have a script for backup and restore jobs on SQL, using a combination of the the SQLPS commands and the SMO objects. Most of the time this works fine, however occasionally I get an error that a database has existing connections and the restore fails.
Restore-SqlDatabase -ServerInstance $SQLrestoreconnection -Database $targetDB -BackupFile $sourcefile -RelocateFile $rfl -ReplaceDatabase -ErrorAction Stop
What would be the best procedure for correcting the exclusive access? It seems like setting the database or instance to single user is one option, is there a way to add this to the SQLPS command?