0

Good afternoon!

I have the following script to automate the process of running Update-MpSignature and then running the defender offline scan with Start-MpWDOScan. I use ping 120 to run a short term ping to verify that the computer goes down but have to either wait it out, or break and re run the script to get the prompt to open back up. Is there a way to use a IF function upon receiving a request timed out to restart the script so i can input the next machine?

Thanks

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"
    ping $compname -n 120
  • 1
    Yes. Yes there is a way to do that. First I would suggest using Test-Connection instead of ping. Use the -Quiet switch when you Test-Connection and you will just get a $true or $false retuned. Use this as the condition for your if. – EBGreen Mar 09 '18 at 19:54

1 Answers1

0

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.

TheMadTechnician
  • 30,190
  • 2
  • 30
  • 42
  • If you are on a relatively stable network, -Count 1 will speed things up over a large list of machines as well. – EBGreen Mar 09 '18 at 20:02
  • Doh, yeah, forgot about that. I use that practice myself, just forgot it for the answer. Thanks EBGreen! – TheMadTechnician Mar 09 '18 at 20:17
  • Will this restart the script to input the next computer? – Andrew Crowell Mar 09 '18 at 20:51
  • No. You could put the whole thing into a while($true) loop though. – EBGreen Mar 09 '18 at 20:53
  • Or, the way that I would do it...put the machine names into a text file and have the script read them out of there...then go have a snack. I suggest bacon. – EBGreen Mar 09 '18 at 20:54
  • I apologize I do not know enough about powershell, or scripting, to know how to accomplish that. Ideally i would like to verify each computer individually, or if there was a way i could tell which ones worked and ones did not work. – Andrew Crowell Mar 09 '18 at 22:07
  • Do you at least have access to bacon? Hopefully @TheMadTechnician will come along and edit this. If he doesn't sometime in the not too distant future I will edit it then he can fix it the way he wants it the next time he comes by. I'll give him some time first though. – EBGreen Mar 09 '18 at 22:15
  • I really appreciate your help on this! I do have access to bacon, you are welcome to have some ~~~~ – Andrew Crowell Mar 09 '18 at 22:28
  • Thanks @TheMadTechnician thought it would be rude to edit your answer and just silly for me to essentially write the same answer over again. You did it better than I would have anyway. :) – EBGreen Mar 10 '18 at 03:41
  • Thank you for all your help. Both of you were able to help me understand which lines of code did what.I removed the Bit for waiting for the computers to come back up and have that in a separate script that I will run to make sure they all come back up in the same manner. I was able to do a whole lab of computers with this and check for them coming back up. – Andrew Crowell Mar 12 '18 at 14:59