RestoreCommand.sql doesn't run as is in case-sensitive environments.
The following needs to be changed (in two places)
FETCH NEXT FROM restorecursor
INTO @databasename, @command
to
FETCH NEXT FROM RestoreCursor
INTO @DatabaseName, @Command
and all other references to restorecursor need to be changed to RestoreCursor