Play on their terms
First you should either know what qualities some of those students have. Are they competitive? are they bookworms or maybe enthusiastic about other subjects (for example, physics or biology)?
Once you know these things (which can be discovered with a simple mandatory questionnaire of some sort), then a few options branch out:
1. Class Competition
there are many ways to go about this, but my personal favorite is to divide them into groups (the entire class, with the under motivated ones fairly distributed).
Ask each team to create a program that would complete some given problem that is related to the most recent subject taught. The team that writes the least complex or the fastest or one that's best by some other criteria is the winner (and gets some prize which isn't related to grades). Make sure to take note of the participation of the under motivated students in each team. Emphasize that team members should try to encourage one another to participate.
This would work quite well for the competitive students, including the under motivated ones.
2. Reading assignment
You can casually mention at the end of some lesson that the material for this lesson is freely available in <some book name>, and you highly recommend reading it, even if one isn't so interested in the topic. Of course, make sure that the book you tell them about actually is recommended even for people who are not furvant fanatic for the topic (online book reviews can say so). This gets those who are bookworms interested in the topic, or at least it gets them up to scratch in the subject.
3. Inter-subject project
For the students who are interested in other subjects, you can suggest they try to make a project in computer science, that has to do with the other subject. A physics simulation, a biology-related program (I have near to nothing knowledge in biology, so this is the best example I have, but don't worry; your students who are interested in a subject usually come up with a good idea for a project that combines both subjects) or something else.
This option depends entirely on the students' enthusiasm in other subjects. But students are rarely uninthusiastic in all subjects.
The idea is to encourage them to do something that they want to do, but that also involves Computer Science. This would both teach them much, and show them reasons for being motivated to learn Computer Science.
This solution obviously won't work for every single situation, but with some tweaking it does cover a wide variety of students.