Reboot remote server with powershell and get output of status


Hi

I am using below script to reboot remote servers which are in CSV file.


============
Import-CSV "D:\script\serverlist.csv" | ForEach-Object -Begin {
"Computer $_ initiated reboot at $(Get-Date)" | Add-Content -Path Rebootlogs.txt
} -Process {
    Restart-Computer $_.servername -Force
} -End {
    Import-CSV "D:\script\serverlist.csv" | ForEach-Object -Begin {
        Start-Sleep -Seconds 60
    } -Process {
        if (Test-Connection $_ -quiet) {"Computer $_ verified to be responding to ping at $(Get-Date)" | Add-Content -Path Rebootlogs.txt } 
else {"Computer $_ unresponsive to ping at $(Get-Date)" | Add-Content -Path Rebootlogs.txt}
    } -End {}
}
==========================

Result:
======================
Computer  initiated reboot at 01/30/2015 05:20:24
Computer @{servername=server12; schedule=ONCE; RebootTime=22:30:00; RebootDate=01/26/2015} unresponsive to ping at 01/30/2015 05:21:28
========================

But when I am using below script wiht "get-content" from TXT file..  

==================
Import-Csv "D:\script\serverlist.txt" |
ForEach-Object -Begin {
    "Computer $_ initiated reboot at $(Get-Date)" | Add-Content -Path RebootLog.txt
} -Process {
    Restart-Computer -Force -ComputerName $_
} -End {
    Get-Content -Path "D:\gops\serverlist.txt" | ForEach-Object -Begin {
        Start-Sleep -Seconds 60
    } -Process {
        if (Test-Connection $_ -quiet) {"Computer $_ verified to be responding to ping at $(Get-Date)" | Add-Content -Path RebootLog.txt} else {"Computer $_ unresponsive to ping at $(Get-Date)" | Add-Content -Path RebootLog.txt}
    } -End {
    }
}

=========================

Result:
==============
Computer  initiated reboot at 01/30/2015 05:26:02
Computer server12 verified to be responding to ping at 01/30/2015 05:27:05

=============================


I am not able to catch, why so difference showing from "import-csv" input file.

I need the output same like "get-content" result script:

Please help or guide me on this..

I want to use CSV input file..





  • Edited by Mr. Raj Thursday, February 05, 2015 7:46 PM
January 30th, 2015 10:33am

Hi Peter,

that can be added to it, if you really want to. Easy method:

Split list of Servers into 5 smaller lists. Run the script 5 times in parallel, once per list.

It's less simple if you want to wait for verification before launching the next bunch (and do it all within one list of servers):

  1. Create an outer loop that will break once you're done with all computers on the list
  2. Restart Computers until you have restarted 5 Computers (keep a counter)
  3. Then wait and keep trying to verify their reboot, using whatever mechanism seems appropriate
  4. Once they've rebooted, hit the end of the outer loop (looping back to the beginning of Step 2 and restart the next 5 computers)

Neither way is all that complicated, whichever is the best depends on your necessities.

Cheers,

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2015 8:04am

Couldn't we have a workflow that would process this in parallel?  Otherwise, you will have to wait for each server to fully reboot before the next one is started.  Depending on what you are looking for. 

workflow test-restart {

 param ([string[]]$computernames)

 foreach -parallel ($computer in $computernames) {

   Restart-Computer -Wait -PSComputerName $computer

 }

}

$servers = Import-CSV serverlist.csv | select-object ExpandProperty ServerName

test-restart ($servers)
Justin


July 20th, 2015 3:22pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics