Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help making a script to run tasks 1

Status
Not open for further replies.

spool

Technical User
Jan 20, 2003
262
US
hello,

I know a little about scripts but I think this is over my head.

I need to run three scheduled programs on 20 computers in our office without intervention in succession. I like to schedule it when no one is working around 3:00 AM. I was able to this with our 98 boxes but now all are XP's.

Disk Cleanup>cleanmgr.exe
Error-Checking>chkdisk.exe
Disk Defragmenter>defrag.exe

I used Scheduled Tasks to start Disk Cleanup but it needs someone to choose what drive to scan and then click OK and then click OK to delete the files. I need it to run all the way through on it's own.

I have XP machines so Error Checking can only be run after restart.

Defrag is the same as Disk Cleanup it needs someone to choose what's next to be done.

This is what I'm trying to do. First schedule Disk Cleanup to run then after it is finished restart the computer and run Error Checking after Error Checking I think it brings me back to the logon screen. So I think I will have to bypass logon so the final program Defrag can run then restart or log off to bring the system back to the logon/Welcome screen.

Now this is what I have done/tried. The script I did can get Disk Cleanup to start but I can't get past the OK button so the other OK button can be reached so the program can delete the files.

I did a script that will restart the computer. But I need checkdisk to know that it is next to run.

I got from this forum a vbscript that will run defrag completely.

I need to connect all these tasks to work in one fluid motion.

Any help would be appreciated.

spool


Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
Thanks tsuji,

I have two of the three tasks scheduled to run.

I'm having trouble with defrag. I got the script from this forum and a couple elsewhere. The program starts and does the defrag but when it is done it will not close. It hangs at the "Send a tab key to move the focus from View Report button to the Close Button" I tried to tweak it but it still does not close.

Here is the whole script as I copied it. Can someone look at it and see why it does not close the program.

set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second
WshShell.Run "dfrg.msc"
WScript.Sleep 1000

'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 2000

'Send a D to start the defrag
WshShell.SendKeys "D"

'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5000
Wend

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"

Thanks, spool



Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
Update:

Need help.

as stated in my last post the Defrag program starts and finishes but does not close. The script stops at.

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

The WshShell.Sendkeys commands are not working. I tried this script on four different XP boxes and it still hangs.

Are the Sendkeys done internally or from the the key board?

Thanks, spool

Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
Hello,

Thank you for all the info.

As I stated above. I wan't to learn how to write/understand scripts/batch files and how they work so I can run them using Scheduled Tasks.

I know I can run these tasks from the command prompt. I wan't to use scripts/batch files for these tasks.

I got Disk Cleanup Scheduled using cleanmgr /sageset, cleanmgr /sagerun.

I wrote a batch file for check disk that works for XP and 2000. And it runs through all five stages.
As we know Check Disk does not run until the system frees up the hard drive. So the computer has to be restarted.
This batch file does just that, unattended using Scheduled Tasks.


-------------------------------------------
echo y|chkdsk C: /F /R

Shutdown.exe /r /t 05
-------------------------------------------

F = Fixes errors on the disk.
R = Locates bad sectors and recovers readable information.

Shutdown:
r = restart
t = time in seconds
05 = launches a warning window saying how many seconds before restart.

Use notepad, save as, .bat.

Note: Win 2000 does not have a copy of Shutdown.exe you can get a copy from the Win 2000 resource kit or XP. C:\Windows\System32\. Drop it in C:\Winnt\System32

Now I'm having problems with defrag. I got a few vbScripts to run defrag unattended.

(I did not write these scripts but I did try to change some lines but it still does not work. They do work for everyone else using them.)

It stops/hangs at this line.

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200,

It needs to do this next.

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500

It doesn't.


The "WshShell.Sendkeys" commands are not working. I tried this script on four different XP boxes and it still hangs.

I tried it on a Win 2000 box it works but not with Scheduled
Tasks. How do the "WshShell.Sendkeys" work. Is there a setting that is stopping/blocking, "WshShell.Sendkeys" from working?

Note: noting is running in the foreground that would keep
"WshShell.Sendkeys" from working.

If anyone can help, to save space, you can read the whole Script in my past post above.

Unrelated: When I Preview my Post and then need to go back to edit after editing I go back to Preview again then Submit Post it has posted two times. So this is why you may see this two times posted again.

Thanks, spool









Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
I think you missed my point.

Description of the New Command Line Defrag.exe Included with Windows XP
This article describes the Defrag.exe command line utility that is included with Windows XP. The Defrag.exe command line utility is specifically designed for scripting and for use by system administrators.
Note that the first part of the article cited above proceeds to tell you how to do a GUI defrag.

The article gets around to the command-line defrag.exe after that. This version pops up no windows, but does return text to its console window you can intercept in WSH via a WshScriptExec object's StdOut property.
 
Thanks again dilettante,

I did get your point. I know about the GUI mmc and the defrag.exe tool. But they have it's limitations.

Like I said I am new to scripting so I'm not used to all the terms yet. The script mentioned above needs no input by someone. I like the mmc version because if someone accidentally goes to the computer they will see the programs GUI and knows not to work from that computer until the screen is clear. The tasks are scheduled for early morning but you never know, if someone comes in early and forgets.

I don't actually work at these companies they out source using my firm. I maintain the computers from our office remotely. Once a month we go there physically to check things out if need be. So if I can use Scheduled Tasks this cuts down man hours out in the field.

So I have two down, defrag is left. I tried the ones you mentioned and they both need some kind of user input. When the task is done something is left on the screen for someone to click to close it. I need a clear screen when the user comes in the morning and start the day.

Now the script I have mentioned is known to do it all. It starts, does the defrag and closes the app. I have one that defrags more then one volume/partitions, or multiple hard drives.

I can't get it to go past that hang point.
I need to find out why The "WshShell.Sendkeys" commands are not working after that point. I have about four of these scripts and none of them work for me. They do work for others I have read. On XP's and 2000. Like I said I got it to run on Win 2000. This is what I'm looking for.

How can I see how "WshShell.Sendkeys" works in an app. and where do I find it in the windows built in vb Scripting program? That's if I can.

If not, I then set something on these computers thats stopping the script from finishing it's routine.

Can someone try this script and see if it works. The one here was taken from this forum. The person that posted it said we could freely use it. So I take no credit.

Thanks, again for any help.

spool





Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
Well you know what you need, but keep in mind that using .SendKeys is notoriously unreliable. If the user tries any interaction while your script "has control" it can be thrown pretty seriously out of whack. All they need to do is anything that changes which window has focus between .AppActivate and any of your "sent" keys.

You're right, if you have to do this on Win2K you may be out of luck. It doesn't seem to have a scriptable defrag.exe included, that's a new feature on XP. All you get with Win2K is one that expects GUI user interaction.
 
dilettante,

The link you furnished was where I got my defrag scripts from and one from this forum.

I've gone all over the Web hunting for information.

This script looked like a good solution to my Scheduled Tasks dilemma.

Well, this is still a work in progress.

Thanks, spool





Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
Spool, Have you found the answer to why it will not close? Please share. If you dont mind share the other scripts as well. I am trying to automate my three machines(all xp) and this is cool. I am also very new to this as well. Thanks.
 
brodie1227,

I'm still testing should have more info soon.

spool

Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
brodie1227,

I'm still testing. All three scheduled tasks will run on time if the admin. is logged on. If the system is in the log off mode defrag will not run.

If admin. is logged on and the screen saver is running and set to "On resume, display Welcome screen" all tasks run. This keeps users from getting into the admin. account.

I really wan't the tasks to run with a full log off. I set up the tasks to run as admin with a password still no go.

I got the defrag script to run through closing the defrag utility. Can't have any windows open in the foreground.
The "WshShell.Sendkeys" doesn't know what window to send the key strokes to.

If you wan't to try these .bat's and .vbs. here they are.

Disk Cleanup: go to this link.


Check Disk: use this one. Copy/Paste in Notepad and save as .bat file. This Bat tells the system that you want a full check disk scan and it will restart and run the utility.

-------------------------------------------
echo y|chkdsk C: /F /R

Shutdown.exe /r /t 05
-------------------------------------------

Defrag: Copy/Paste the defrag script into Notepad and save as .vbs file.

set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second
WshShell.Run "dfrg.msc"
WScript.Sleep 1000

'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 2000

'Send a D to start the defrag
WshShell.SendKeys "D"

'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5000
Wend

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"

Note: I saved all to my desk top for easy access. After I tested them I moved them to the C:\Windows folder or you might have C:\Winnt folder if using Win 2000.

Let me know how you do.

spool






Don't argue with an idiot, he will bring you down to his level and beat you with his experience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top