This paper is on beamforming in wireless relay networks with perfect channel information at relays, the receiver, and the transmitter if there is a direct link between the transmitter and receiver. It is assumed that every node in the network has its own power constraint. A two-step amplify-and-forward protocol is used, in which the transmitter and relays not only use match filters to form a beam at the receiver but also adaptively adjust their transmit powers according to the channel strength information. For a network with any number of relays and no direct link, the optimal power control is solved analytically. The complexity of finding the exact solution is linear in the number of relays. Our results show that the transmitter should always use its maximal power and the optimal power used at a relay is not a binary function. It can take any value between zero and its maximum transmit power. Also, this value depends on the quality of all other channels in addition to the relay's own channels. Despite this coupling fact, distributive strategies are proposed in which, with the aid of a low-rate broadcast from the receiver, a relay needs only its own channel information to implement the optimal power control. Simulated performance shows that network beamforming achieves the maximal diversity and outperforms other existing schemes. Then, beamforming in networks with a direct link are considered. We show that when the direct link exists during the first step only, the optimal power control is the same as that of networks with no direct link. For networks with a direct link during the second step, recursive numerical algorithms are proposed to solve the power control problem. Simulation shows that by adjusting the transmitter and relays' powers adaptively, network performance is significantly improved.