Friday, December 7, 2012

Scheduling continued

This is an update to my previous post.

I noticed that while my computer would turn on automatically, it would go back to sleep about two minutes after the initial wake up. I searched around a bit and ran into a couple of posts similar to the link below.
http://www.sevenforums.com/tutorials/35767-sleep-return-timeout-unattended-wake-up.html

What is happening is that the computer would wake up from sleep. But because no one was touching the keyboard, mouse, or anything else, it would wait a certain amount of time(2 minutes) and then go back to sleep again.  This would happen regardless of what the computer was actually doing, it was only looking for user input. Since I am hopefully in deep sleep by this point I am not going to be touching it.

This setting doesn't appear in Advanced Power options, or at least no where that I could find it. But there is a registry value for it found here
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\7bc4a2f9-d8fc-4469-b07b-33eb785aaca0\DefaultPowerSchemeValues


There are a couple of different registry values that you can see in my above link. One for each power plan. For mine I changed the value from 120 seconds to 3600 seconds. That would give my computer enough time to do everything I want it to do and then go back to sleep.

Everything seems to be working much better this time. When I check the logs in the morning everything ran at the correct times. 

Sunday, November 18, 2012

Scheduling MSE and Malwarebytes Anti-Malware to run automatically

I have not been doing as good of a job as I should have with making sure my computer is up to date with virus definitions and scans. I usually use Microsoft Security Essentials for real time, but then I round it off with Malwarebytes Anti-Malware on the side. There are a few problems though.

1. MSE is set to run once a week at 2:00 am. The computer is not on at this time and I don't want to leave it on all night long. This means it usually gets run while I am using it.
2. Even if I do have the computer wake up for this, how do I get it to go back to sleep?
3. What about Malwarebytes? What about definition updates?

I decided that I would set up a scheduled time early in the morning where everything will run. I will first have to turn on the computer, then run MSE, then check for definition updates for Malwarebytes, then run Malwarebytes, and then put the computer back to sleep. Lots to do.

Recently I discovered two pieces of technology that may help with this. One is Task Scheduler, and the other are windows batch files (.bat extension). I used the command line briefly before, but had never used it much until a few months ago.

This is the solution that I came up with.

1. I noticed that there already was a scheduled task labeled "Microsoft Antimalware Scheduled Scan" This was set to run once a week, but since the computer was put to sleep it was not running.
There are some settings though that can fix that. I discovered a setting called "Wake the computer to run this task"
Once that was checked it successfully woke up my computer and started running MSE. 

2. Next up is Malwarebytes. Malwarebytes does support a schedule, however that is only in the Pro version. No since I don't really want to pay for it, and I like learning things I set out to find a way to do it all from a command line. 

I created a new .bat file that will start this process. After looking around I discovered two things. One is that you can update and run Malwarebytes from the command line, but only the paid Pro version supports silent mode. That will make it slightly more difficult  but still doable.

The first line of the file will then be:
start mbam.exe /update
I discovered an issue where after it updates its definitions, a window pops up asking that you press ok. The batch file will not proceed forward until the user clicks ok. Now since I am asleep at that time that is not going to work. So by saying "start mbam.exe /update" instead of just "mbam.exe /update" it will cause the rest of the script to go on and not wait for it to finish. 
This means that we need to have it wait for it to finish though. There is no easy way that I could think of, especially since the window just opens up after the update is finished, so I decided to just hard code in a set time. I haven't found a good way to cause it to wait so I just used:
ping -n 30 127.0.0.1
This will force it to ping itself 30 times before going on to the next step. (around 30 seconds or so) This should give it enough time to update itself.
Now we still have that issue where the window popped up saying that it had updated successfully. I cannot start the scan until this is killed. So I use the useful taskkill function.
taskkill /F /IM "mbam.exe"
/IM tells it to look for mbam.exe and /F means it will forcefully kill it. 
 Then our final step:
start mbam.exe /scan -quick -terminate
This will do a quick scan. The -terminate parameter tells it to close the program when finished if nothing is detected. If something is it will just remain open until I get to it the next day.

The final result is:
cd "\Program Files (x86)\Malwarebytes' Anti-Malware"
start mbam.exe /update
ping -n 30 127.0.0.1
taskkill /F /IM "mbam.exe"
start mbam.exe /scan -quick -terminate


3. Now I need to schedule everything. MSE usually takes less than 10 minutes to do a scan, so I will give it a 20 minute window. That means that Malwarebytes needs to start running 20 minutes MSE starts.
So I created a scheduled task that calls the .bat file that I just created. I had to check the box for "Run with highest privileges" so that it will run the .bat file as Administrator.


4. Last is to put the computer back to sleep. Not sure of an easy way so created another scheduled task. I am having this one run about 20 minutes after Malwarebytes starts.
For this one you want it to call rundll32.exe with "Powrprof.dll,SetSuspendState Sleep" as the arguments.

 And that is that! I think this will work pretty well, but give it a week or two and I will be able to see if I missed anything. If anyone knows any hints or ideas to make it a little cleaner please let me know

UPDATE:
See my next post for an update on getting this to work.



Intro

I figured that I probably need more of an online presence, so I figured why not share some of the random things I learn from work and messing around at home. Most of it will probably by related to SQL and Windows, but you never know.