HOUSTON — The Houston Astros added Carlos Pena to bring a veteran presence to their young team.

They also thought he could help move the offense.

On Thursday the 35-year-old showed his value in both areas, encouraging his teammates after a tough start and coming through in the 10th inning with a three-run homer to give the Astros a 7-4 win over the Milwaukee Brewers.

"He's been tremendous on and off the field," manager Bo Porter said. "He is a first-class guy that really has helped mentor a lot of these guys we have here. You look at some of the big hits he's been able to get for our ball club (and) it speaks to the reason why we brought him aboard."

Pena takes pride in getting the team back on track when things go wrong.

"I do try to bring a good attitude every single day and throughout the ball game regardless of what's going on motivating my teammates and encouraging," he said.

Carlos Corporan struck out but reached on a wild pitch with one out in the 10th and J.D. Martinez followed with a double. Pena then launched the first-pitch homer off Michael Gonzalez (0-3) to the second deck in right field.

It was his sixth career last at-bat homer and his first since Aug. 2008 when he was with Tampa Bay.

"It never gets old," he said, still recovering from a post-game shower of Gatorade, water and shaving cream. "What a ball game. What a game. What a great homestand. I thought we were in every single game. This is a lot of momentum moving forward."

Houston went 5-2 in this homestand and is 18-16 since May 15.

Things didn't look good early on Thursday though, when the Astros fell behind 3-0. It was a sloppy game that featured six unearned runs and four errors.

Hector Ambriz (2-4) pitched a scoreless 10th to help Houston take two of three in the series.

Corporan tied it with a solo home run in the eighth. Matt Dominguez tied a career-high with three hits.

The Brewers led by a run before Jonathan Lucroy's homer in the sixth, which bounced on top of the low wall in right field, made it 4-2 and chased Lucas Harrell.

Houston got back within one when Martinez reached on an error by second baseman Scooter Gennett leading off the bottom half. Dominguez hit an RBI double with two outs that made it 4-3.

Milwaukee manager Ron Roenicke was troubled by his team's performance on Thursday.

"That's as bad as it gets," Roenicke said. "Sometimes we might lose big, or we may not pitch well, but all the things we did wrong in that game, that's a bad game."

Milwaukee starter Yovani Gallardo allowed five hits and three runs — none earned — in seven innings. He has not permitted an earned run in 21 straight innings spanning three starts.

Gallardo didn't exactly let his defense off the hook for struggling in this one.

When asked if the mistakes on defense bothered him he said: "Not really; maybe a little bit."

Then he backtracked a bit saying that it's part of the game.

"I know for a fact the guys behind me don't want to make errors," he said. "They want to get every out they can. That is just how it is. As a pitcher, you have to move past it, whether it's an error or a misplayed ball. You have to focus on the next guy."

Jim Henderson replaced Gallardo for the eighth and immediately gave up the homer to Corporan.

Gennett was thrown out at home on a grounder for the second out in the ninth. Dominguez doubled to lead off the ninth and reached third with one out, but John Axford retired the next two batters to send it to the 10th.

Carlos Gomez doubled to start the Milwaukee eighth before a peculiar error on second baseman Jose Altuve. He missed catching the ball on a pickoff attempt, allowing Gomez to take third.

Altuve immediately looked down at his glove and ran to the Astros dugout. Television shots showed that his mitt had come apart, leaving a big hole in it that allowed the ball to get through. An Astros official tied it back together and he returned to the field.

Astros center fielder Justin Maxwell dropped a fly ball hit by Caleb Gindl that sent two runs home in the second for a 3-0 lead.

Houston right fielder Trevor Crowe injured his right shoulder crashing into the wall attempting to catch the home run by Lucroy and left the game. He was placed on the 15-day disabled list with a sprained right shoulder after the game. Marc Krauss was promoted from Triple-A Oklahoma City to take his place on the roster.

NOTES: Houston plays another one of its former NL Central rivals when it opens a three-game series with the Cubs on Friday. Dallas Keuchel is scheduled to pitch for the Astros against Matt Garza. ... The Brewers return home to begin a three-game series with Atlanta on Friday. Wily Peralta opposes Atlanta's Julio Teheran.