user1657052
user1657052

Reputation: 21

How to send request using a specific IP address to an website with PowerShell

I need to execute a script to access an website binding to a specific IP address. I found something in C# to do that using BindIpEndPointDelegate class.

var req = (HttpWebRequest)WebRequest.Create("http://google.com/");
req.ServicePoint.BindIPEndPointDelegate = BindTo;
 using (req.GetResponse());

 static IPEndPoint BindTo(ServicePoint servicepoint, IPEndPoint remoteendpoint, int retrycount)
{
IPAddress ip = IPAddress.Any; //This is where you specify the network adapter's address
int port = 0; //This in most cases should stay 0. This when 0 will bind to any port     available.
return new IPEndPoint(ip, port);
 }

I don't know how to pass that class to powershell.

Thanks for all the help!

Upvotes: 2

Views: 2738

Answers (1)

Shay Levy
Shay Levy

Reputation: 126712

Give this a try (not tested), this is the PowerShell equivalent of the C# code:

function Bind-IPEndPointCallback([System.Net.IPAddress]$ip,$port)
{
        New-Object -TypeName System.Net.IPEndPoint $ip, $port
}

$request = [System.Net.HttpWebRequest]::Create("http://somewhere.com")
$request.ServicePoint.BindIPEndPointDelegate={ (Bind-IPEndPointCallback -ip 10.10.10.10 -port 80 ) }
$request.GetResponse()

Upvotes: 1

Related Questions