Script to reboot multiple Mac computers on a network in Apple Script or PowerShell?

I am trying to write a script to reboot a Mac in a range of IP addresses. Doing this with PowerShell works, but the process waits until each machine reboots before it moves on to the next machine. I find ssh'in every machine, and rebooting with sudo shutdown -S shutdown -r nowis faster ... it's just an instruction. Here is what I still have in PowerShell:

$serverRoot = "xxx.xxx.xxx."
$startVal = 100
$stopVal = 150

for ($i=$startVal; $i -le $stopVal; $i++)
{
    $User="username"
    $Password="password"

    $SecurePass=ConvertTo-SecureString -string $Password -AsPlainText -Force

    $Credential = New-Object System.Management.Automation.PSCredential $User, $SecurePass

    $session = New-SSHSession -ComputerName ($serverRoot + $i) -Credential $Credential  -AcceptKey
    Invoke-SSHCommand -SSHSession $session -Command "echo $Password | sudo -S shutdown -r now"   
    Remove-SSHSession -SSHSession $session -Verbose

}

Is there something I can add that just shuts down the reboot process on all computers at once? Should I use AppleScript?

+4
source share
3 answers

, -ComputerName New-SSHSession ,

$serverRoot = "xxx.xxx.xxx."
$startVal = 100
$stopVal = 150

# First start with creating a collection
$servers = @()
for ($i = $startVal; $i -le $stopVal; $i++)
{
    $servers += ($serverRoot + $i)
}

# Then, pass the $servers variable directly when creating the session
$session = New-SSHSession -ComputerName $servers -Credential $credential -AcceptKey

!

+3

, :

workflow Kill-All {
  param([string[]]$computers)

  foreach -parallel ($computer in $computers) {
      InlineScript {
         # Your powershell stuff.
      }
  }
}

Kill-All -Computers "132.134.123.1", "123.4.53.12"

bat powershell bat. , bat, , .

, rae1, - New-SSHSession , .

:

$sessions  = New-SSHSession -ComputerName (1..254 | %{ "xxx.xxx.xxx.$_"})

: http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/20/use-powershell-workflow-to-ping-computers-in-parallel.aspx

http://community.spiceworks.com/topic/341776-call-a-ps-script-from-another-and-don-t-wait-for-it-to-finish

+3

You can use plink.exealong these lines (untested):

plink root@192.168.0.1-pw secret "shutdown -S shutdown -r now" &
plink root@192.168.0.2-pw secret "shutdown -S shutdown -r now" &
plink root@192.168.0.3-pw secret "shutdown -S shutdown -r now" &
plink root@192.168.0.4-pw secret "shutdown -S shutdown -r now" &

Or like that

for %%a in (...) do plink root@%%a -pw secret "shutdown -S shutdown -r now" &
0
source

Source: https://habr.com/ru/post/1584319/


All Articles