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

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.
Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 3:33pm

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

Other recent topics Other recent topics