Don't use Ping
since it's external, use the PowerShell cmdlet Test-Connection
to see if the server is down or not, and you can use Write-Progress
to track what state it's in.
param (
[string]$compname = $( Read-Host "Input computer name, please" )
)
Write-Output "Update-MpSignature"
Update-MpSignature -CimSession $compname
Write-Output "Start-MpWDOScan"
Start-MpWDOScan -CimSession $compname
Write-Output "Ping "$compname" for two minutes"
While(Test-Connection $compname -Quiet -Count 1){
Write-Progress -Activity "Rebooting $compname" -Status "Waiting for $compname to shut down."
Start-Sleep -sec 1
}
While(!(Test-Connection $compname -Quiet -Count 1)){
Write-Progress -Activity "Rebooting $compname" -Status "Waiting for $compname to come back up."
Start-Sleep -sec 1
}
Edit: Reading servers from a file is easy, you just use Get-Content
to read the file, and then use a ForEach
loop to loop through the list. You would want a simple text file with one server name per line. In my examples I will use a file named servers.txt
located on the user's desktop. You would want to structure the file like this:
SQLServerA
FileServerA
WebServerA
SQLServerB
FileServerB
WebServerB
You can either hard code the path to the file, or change your parameter to point to the file, using a default location. Then you can read that file and store the contents in a variable like this:
$complist = Get-Content $listpath
That will set $complist
to be an array of strings, where each item in the array is one line from the text file. Next you would loop through it using a ForEach
loop as such:
ForEach($compname in $complist){
<code to do stuff>
}
So in the end the whole thing would look like this:
param (
$listpath = "$home\desktop\servers.txt"
)
#Import server list
$complist = Get-Content $listpath
#Loop through the list of servers
ForEach($compname in $complist){
Write-Progress -Activity "Processing $compname" -CurrentOperation "Updating Signature on $compname." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Update-MpSignature -CimSession $compname
Write-Progress -Activity "Processing $compname" -CurrentOperation "Initializing offline scan of $compname." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Start-MpWDOScan -CimSession $compname
While(Test-Connection $compname -Quiet -Count 1){
Write-Progress -Activity "Processing $compname" -CurrentOperation "Waiting for $compname to go offline." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Start-Sleep -sec 1
}
While(!(Test-Connection $compname -Quiet -Count 1)){
Write-Progress -Activity "Processing $compname" -CurrentOperation "Waiting for $compname to come back up." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Start-Sleep -sec 1
}
}
Then, as long as you have servers.txt
on your desktop, you can just run the script and it will give you a status bar saying what it's doing, what server it's working on, and give a 'server X of Y' status so you know how far into things you are.