0

I want to reboot specific servers, I am using findstr to find specific servers in the list of 1000's of servers, however, is there any way I can hardcode servernames in a script so the script only run on a particular set of remote servers? Also, how to use for each against array variable. For e.g is below method correct?

$serverlist = "server1,server2,server3"

for each ($server in $serverlist){  $serverboot= gwmi win32_operatingsystem -comp $server
$serverboot.Reboot
}
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
Mickey
  • 25
  • 9

1 Answers1

0

First define a list of your servers for this the coma (,) is the array operator in PowerShell :

$serverlist = "server1","server2","server3"

Now $serverlist is an array or a collection you can use.

Then you pipe this list in the Foreach-Object Cmdlet that allow you to execute a scriptblock for each element in the list. $_ represent the current element. ; is the instruction seprator :

$serverlist | ForEach-Object {$serverboot= gwmi win32_operatingsystem -comp $_; $serverboot.Reboot()}
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • 1
    I tried the above script, it did not work and kept on asking me: cmdlet Foreach-object at command pipeline position 1 Please supply values for the following parameters: Process[0] .... I have defined $serverlist = "server1","server2" – Mickey Apr 14 '16 at 23:14
  • Sorry,, I gorgot the `()` because I don't reboot my servers, but for me it works till there. – JPBlanc Apr 15 '16 at 04:33
  • the actual issue what I have found is not declaring -Process and mentioning what to do on each object...i.e we have to reboot so the working command is: $serverlist | ForEach-Object -Process {.... – Mickey Apr 15 '16 at 17:58