Nice script -thanks for sharing.
I get multple errors when I run it though:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'Service_1d756ce176df40ff88162ccaa'.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '.'.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'Search_Service_Application_CrawlStoreDB_50d2dd819c.dbo.sysfiles'.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'Search_Service_Application_DB_78718af6ae114220b5fb.dbo.sysfiles'.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'Search_Service_Application_PropertyStoreDB_6c687a8.dbo.sysfiles'.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'Secure_Store_Service_DB_9ea28dcf81de4bfb857b8121a0.dbo.sysfiles'.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '.'.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '.'.
Regards,
Snorri