Updated

Jordan Schafer drove in three runs and the Houston Astros ended a 12-game losing streak, beating the Pittsburgh Pirates 9-5.

Schafer delivered the go-ahead home run for the Astros, who avoided a four- game sweep with the win.

Lucas Harrell (8-7) tossed another strong outing for Houston, giving up two runs on five hits in 5 2/3 innings. The rookie has a 1.86 earned run average in four starts since the All-Star break.

Meanwhile, James McDonald's (10-5) second-half struggles continued. The Astros tagged him for five runs on six hits and a career-high seven walks in five innings. McDonald has pitched to an 8.91 ERA over his last four starts.

"Last couple [starts] it's been rough," McDonald said. "I think every year I've pitched I've been in the same situation like this, where I've gotta fight my way out of some rough starts."

Houston took advantage of McDonald's control problems to pull ahead in the fourth inning. Chris Snyder drew a one-out walk and Schafer made McDonald pay, lining a home run into the right-field seats to give Houston a 3-2 lead.

The Astros piled on two more runs in the fifth inning. J.D. Martinez sent a flare into right field, which Garrett Jones misplayed, and ended up at third base. Scott Moore punched a single through the right side to score Martinez before advancing to third on Steve Pearce's double. After an intentional walk and a force out at the plate, Schafer scored Pearce with an infield chopper.

After Andrew McCutchen and the Pirates left the bases loaded in the top of the sixth, Moore and the Astros added another run in the bottom half of the frame, giving Houston a 6-2 lead.

Pittsburgh picked up a run in the seventh when Neil Walker crushed a double into the right-field gap to score Josh Harrison, but Houston blew the game open with three runs in the bottom of the seventh.

Snyder and Matt Downs singled and Jose Altuve walked to load the bases for Marwin Gonzalez, who stroked a double into the left-field corner to clear the bases and open up a 9-3 advantage.

"Offensively, [Gonzalez], what a day," Houston manager Brad Mills said. "It was nice to see him do a real good job with the three hits and those RBIs."

Pittsburgh capped the scoring with a pair of runs in the ninth, but Xavier Cedeno and Wilton Lopez combined to record the final three outs and end Houston's losing streak.

Houston opened up the scoring in the third when Martinez lofted a sacrifice fly to score Altuve, who advanced to third with a perfectly played hit and run.

Jones slammed an upper-deck home run in the fourth inning to give Pittsburgh a 2-1 lead.

Game Notes

Starling Marte stole his first career base in the fifth inning...Prior to the game, Houston traded third baseman Chris Johnson to Arizona for minor league outfielders Marc Krauss and Bobby Borchering...Harrell leads National League rookies with 127 1/3 innings...Harrell improved to 6-1 at home this year with a 2.02 ERA (13 earned runs in 58 innings)...Altuve went 1-for-3 and has hit in eight of his last nine games...The loss snapped the Pirates' four-game winning streak...The Pirates have homered in 19 straight road games...Pittsburgh has not swept a four-game series in Houston since 1983...McDonald has a 2.15 ERA at home and a 5.06 ERA on the road...Pittsburgh went 4-for-10 with runners in scoring position and stranded nine...Houston went 3-for-12 with RISP and left 10 on base.