我第一次使用powershell,我想知道如何在最简单的方法中调用多个目标。
例如,使用curl,我可以执行以下操作
curl "http://root:pass@10.21.1.(196,197,198,199,200}/axis-cgi/restart.cgi"到目前为止,我已经创建了一个脚本来向url发送一个web请求,如下所示
$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但是我想把这个命令发送到大约100个设备,有办法做到这一点吗?
欢迎任何帮助
发布于 2015-07-15 11:46:06
在您的情况下,使用range操作符可能有效:
(196..200) | ForEach-Object {
  $res = Invoke-WebRequest http://10.21.66.$_/axis-cgi/restart.cgi -Credential $cred
}可以使用$_在管道中访问范围内的数字,您可以将其放入目标ip地址中。
发布于 2015-07-15 10:36:39
像这样的事怎么样:
$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
}$text变量也可以存储在CSV文件中,只需在转换器上使用Import。
https://stackoverflow.com/questions/31427476
复制相似问题