Reputation: 4555
I am looking a way to make web request from remote computer. There a 10 servers shared in one network and I need make web request from each server for example to 'http://google.com'. So, I am going to using PowerShell and started writing the script. But I don't know how make request on behalf of server1, server2.. server10
$hosts = @("server1Ip", "server2Ip", ..,"server10Ip");
$url = "http://google.com"
$hLen = $hosts.Length;
for ($i=0; $i -lt $hLen; $i++)
{
try
{
Write-Host "Pinging web address for server: $url ..."
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
Write-Host "Web Request Succeeded."
} catch
{
Write-Host ("Web Request FAILED!!! The error was '{0}'." -f $_)
} finally
{
if ($response)
{
$response.Close()
Remove-Variable response
}
}
}
Upvotes: 3
Views: 1635
Reputation: 26120
use invoke-command and wrap your code into the sciptblock like :
icm -computername $hosts -scriptBlock{
try
{
Write-Host "Pinging web address for server: $url ..."
$request = [System.Net.WebRequest]::Create("http://google.com")
$response = $request.GetResponse()
Write-Host "Web Request Succeeded."
} catch
{
Write-Host ("Web Request FAILED!!! The error was '{0}'." -f $_)
} finally
{
if ($response)
{
$response.Close()
Remove-Variable response
}
}
}
Upvotes: 3