none
Reboot remote server with powershell and get output of status RRS feed

  • Question

  • 

    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 5, 2015 7:46 PM
    Friday, January 30, 2015 10:33 AM

Answers

  • Hi Raj,

    ok ... did a little rewrite of your script, does this work for you?

    # Get list of Servers
    $Servers = Import-CSV "D:\script\serverlist.csv" | Select-Object -ExpandProperty servername
    
    # Reboot each server
    foreach ($Server in $Servers)
    {
    	"Computer $Server initiated reboot at $(Get-Date)" | Add-Content -Path Rebootlogs.txt
    	Restart-Computer $Server -Force
    }
    
    # Give them a minute to start
    Start-Sleep -Seconds 60
    
    # Check each Server
    foreach ($Server in $Servers)
    {
    	if (Test-Connection $Server -quiet) { "Computer $Server verified to be responding to ping at $(Get-Date)" | Add-Content -Path Rebootlogs.txt }
    	else { "Computer $Server unresponsive to ping at $(Get-Date)" | Add-Content -Path Rebootlogs.txt }
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Mr. Raj Friday, January 30, 2015 1:59 PM
    Friday, January 30, 2015 1:47 PM

All replies

  • Hi Raj,

    in the csv version, replace all instances of "$_" with "$_.servername" (Except for the one time where ".servername" is already appended.

    Cheers,
    Fred

    Note: Importing from Csv will return a list of objects (one per line) with properties named exactly the same way the columns are named in the csv file. Since you only want to use the Servername, that's the property you need to use, not the whole object.


    There's no place like 127.0.0.1



    • Edited by FWN Friday, January 30, 2015 10:59 AM
    Friday, January 30, 2015 10:57 AM
  • I can see the above script is take whole object from my CSV . I have my csv in below format:

    servername,schedule,RebootTime,RebootDate
    server12,ONCE,22:30:00,01/26/2015

    That's why .. the result is showing like below:

    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

    How can it be possible to query only for servername instead of who csv objects.

    I tried replace all instances of "$_" with "$_.servername but no luck..

    still getting same output..

    Friday, January 30, 2015 1:10 PM
  • Hi Raj,

    ok ... did a little rewrite of your script, does this work for you?

    # Get list of Servers
    $Servers = Import-CSV "D:\script\serverlist.csv" | Select-Object -ExpandProperty servername
    
    # Reboot each server
    foreach ($Server in $Servers)
    {
    	"Computer $Server initiated reboot at $(Get-Date)" | Add-Content -Path Rebootlogs.txt
    	Restart-Computer $Server -Force
    }
    
    # Give them a minute to start
    Start-Sleep -Seconds 60
    
    # Check each Server
    foreach ($Server in $Servers)
    {
    	if (Test-Connection $Server -quiet) { "Computer $Server verified to be responding to ping at $(Get-Date)" | Add-Content -Path Rebootlogs.txt }
    	else { "Computer $Server unresponsive to ping at $(Get-Date)" | Add-Content -Path Rebootlogs.txt }
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Mr. Raj Friday, January 30, 2015 1:59 PM
    Friday, January 30, 2015 1:47 PM
  • You are Champ FWN..

    You made my life easy and you are time saver for me..

    I was struggling on this from last 4 hours but couldn't get success.

    You have done just 30 mins..

    You are rocking..

    Thanks and have great day ahead..

    Friday, January 30, 2015 2:00 PM
  • It would be nice if this script automated a rolling reboot of, say 5 servers at a time, then move to next 5 after they return.
    Friday, July 17, 2015 7:29 PM
  • 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,
    Fred


    There's no place like 127.0.0.1


    • Edited by FWN Monday, July 20, 2015 12:21 PM
    Monday, July 20, 2015 12:01 PM
  • 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


    Monday, July 20, 2015 7:20 PM
  • Hi Fred,

    I have one requirement of adding a validation check after each server reboot i.e. it ask Yes or no after each server reboot. Could you please assist in modifying above script to add a validation task. Thanks in advance :)

    Monday, February 6, 2017 6:13 PM
  • Hi Fred,

    I have one requirement of adding a validation check after each server reboot i.e. it ask Yes or no after each server reboot. Could you please assist in modifying above script to add a validation task. Thanks in advance :)


    Please don't piggyback on old and closed threads.  You need to open your own question.

    \_(ツ)_/

    Tuesday, February 7, 2017 12:39 AM