Erik Richardson
Erik Richardson

Reputation: 51

Powershell invoke web request to multiple targets

My first time using powershell and i would like to know how i could invoke multiple targets in the easiest method possible

for example using curl i can do the following

curl "http://root:[email protected].(196,197,198,199,200}/axis-cgi/restart.cgi"

So far i have created a script to send a single web request to a url as below

$username = "root"
$password = "pass" | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object  
System.Management.Automation.PSCredential($username,$password)

$res = Invoke-WebRequest http://10.21.66.21/axis-cgi/restart.cgi -Credential 
$cred

But i would like to send this command to around 100 devices, is there a way of doing this.

Any help welcomed

Upvotes: 0

Views: 2159

Answers (2)

arco444
arco444

Reputation: 22831

Using the range operator might work in your case:

(196..200) | ForEach-Object {
  $res = Invoke-WebRequest http://10.21.66.$_/axis-cgi/restart.cgi -Credential $cred
}

The numbers in the range can be accessed in the pipeline using $_ which you can put into your target ip address

Upvotes: 1

Dave Sexton
Dave Sexton

Reputation: 11188

How about something like this:

$text = @'
username,password,url
user1,pass1,url1
user2,pass2,url2
user3,pass3,url3
'@

$text | ConvertFrom-csv | % {
  $username = $_.username
  $password = $_.password | ConvertTo-SecureString -asPlainText -Force
  $cred = New-Object System.Management.Automation.PSCredential($username,$password)

  $res = Invoke-WebRequest $_.url -Credential $cred
}

The $text variable could also be stored in a CSV file and just use Import-Csv instead on ConvertFrom-Csv

Upvotes: 0

Related Questions