Utah Web Site Designers


Jan 23 2008

Fun with WSH - How to write a vbs file to check a directory, count the files, and restart a service. Then email your cell.

Tag: Tech StuffMatthew Moeller @ 12:50 pm

You might wonder how a windows script host app like this could possibly be of use.  Well it has many uses like checking a mail server queue and restarting a service that may have hung.  Or a web development application that allows clients to upload files for print projects but no body ever goes out and cleans off the files.  With a little modification to this you could pull off many scheduled tasks to fit your needs.  I wrote this because google failed me in the search for a quick fix, it’s not often but it does happen. Anyway if this saves you some time then great, mission accomplished. The code below checks a specified directory and counts the total files in it;  If the count is over our threshold then take action by restarting a service that is hung.  Finally send an email with all the fun details to my cell phone.

This has a bunch of unnecessary code in it for debugging, feel free to clean it up.
1. Set your SMTP  address for the email function
2. Set nMax - the threshold before we run an event
3. Uncomment out the echos if you want to see it run manually
4. Setup task scheduler to run the .vbs file at some interval
5. This is tested and runs spiffy on win2003 server but should run on anything win

Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep
Dim n, SYSDATA, SYSEXPLANATION, nMax, strPath, bIncludeDirsInCount, objMessage
'On Error Resume Next 'optional '--------------------------------------------------------------
'COUNT FILES in specified DIR
'--------------------------------------------------------------
'setup variables
strPath = "\\localhost\d$\exchange\mail\_incoming" '_incoming dir path?
bIncludeDirsInCount = false 'count dir as a file?
nMax = 100 'how many files needed before we decide to restart?
SYSDATA = ""
SYSEXPLANATION = "Unable to count files"
n = cntFiles( strPath, bIncludeDirsInCount )
If( n < 0 ) Then
WScript.Quit
End If

SYSDATA = n
SYSEXPLANATION = "Number of files=[" & n & "], maximum allowed=[" & nMax & "]”
‘wscript.echo SYSEXPLANATION

If( n > nMax ) Then

‘SEND EMAIL ON REMOTE SERVER AND RESTART SERVICE
‘———————————————————-
‘SEND ALERT EMAIL TO ADMIN THAT THERE HAS BEEN A RESTART
‘———————————————————-

Set objMessage = CreateObject(”CDO.Message”)
objMessage.Subject = “ServiceX Was restarted on ” & NOW()
objMessage.From = “screwed@server.com”
objMessage.To = “you@domain.com”
objMessage.Cc = “mynumber@mobile.mycingular.net”
‘objMessage.Bcc = “myphone@mobile.mycingular.net”
objMessage.TextBody = “My Service HAD TO BE RESTARTED. Summary: ” & SYSEXPLANATION

objMessage.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
objMessage.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “mail.myserver.com
objMessage.Configuration.Fields.Item _
(”http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25

objMessage.Configuration.Fields.Update

objMessage.Send

‘WScript.Echo “Admin alert email sent”

‘———————————————————-
‘RESTART SERVICE CODE
‘———————————————————-
‘RESTART SERVICE CODE
Dim objShell, intShortSleep, intLongSleep

Set objShell = CreateObject(”WScript.Shell”)
‘Notes must have double quotes if it has spaces in the service name, also leave the single empty space before the service

‘get the service name from the ctrl alt del not the service name in services snapin

strService = ” “”SERVICE TITLE”"”
intShortSleep = 1500
intLongSleep = 5500

‘ Cmd prompt opened
objShell.Run “cmd”
Wscript.Sleep intShortSleep

‘ Service stopped with ‘Net’ command
objShell.SendKeys “NET STOP” & strService
Wscript.Sleep intShortSleep
objShell.SendKeys “{Enter}”
Wscript.Sleep intLongSleep

‘ Service started with ‘Net’ command
objShell.SendKeys “NET START” & strService
Wscript.Sleep intShortSleep
objShell.SendKeys “{Enter}”
Wscript.Sleep intLongSleep

‘ Cmd prompt exited
objShell.SendKeys “Exit”
Wscript.Sleep intShortSleep
objShell.SendKeys “{Enter}”

‘Wscript.Echo strService & ” service stopped and restarted”
WScript.Quit

Else
‘wscript.echo “Good to go no restart needed”
End If

Function cntFiles( strFolder, bIncludeDirInCount )
Dim objFolder, objSubFolders, objFso, o, n

On Error Resume Next

cntFiles = -1

Set objFso = Createobject( “Scripting.FileSystemObject” )
Set objFolder = objFso.GetFolder(strFolder)
If( Err.Number <> 0 ) Then
Exit Function
End If
n = objFolder.files.count
Set objSubFolders = objFolder.SubFolders
For Each o In objSubFolders
n = n + cntFiles( o, bIncludeDirInCount )
‘If( bIncludeDirInCount ) Then
‘n = n + 1
‘End If
Next

Set objSubFolders = Nothing
Set objFolder = Nothing

cntFiles = n
End Function


Nov 17 2007

How to run a php or asp file on a schedule with windows XMLHTTP object and scheduled tasks

Tag: Tech StuffMatthew Moeller @ 12:36 am

If you do alot of web developmentyou may have come across a project that required reports or emails that need to run on a schedule.   You could do it all with web code by tracking the intervals your routine should run but that has one major flaw, it requires a visitor to trigger the event. If no one visits the web site the routine gets skipped and so much for your schedule. If your scheduled task is at all important you’re in trouble.

XMLHTTP and Windows task scheduler to the rescue.  Windows scheduler is extremely accurate and requires no action from anyone to be triggered.  I found everything from scripts that lunched IE resulting in hundreds of open windows on your server to buggy scripts that don’t do anything all over the web, so I gave up looking and wrote one myself and it works just dandy. Works with IIS and Apache and works with php, asp, .net, jsp, heck any web language for that matter.  Three easy steps to scheduling bliss.

STEP 1: CREATE YOUR VBS SCRIPT
Open notepad and paste this code then change the file extension to .vbs

Call RunProcess()
Sub RunProcess()
  'if an error occurs keep on truckin | write an error handler if you like
  On Error Resume Next
  Dim URL, objRequest
  Set objRequest = CreateObject("Microsoft.XMLHTTP")
  'url to page needing a scheduled run
  URL = "http://www.yourdomain.com/filename.php
  objRequest.open “POST”, URL , false
  objRequest.Send
  Set objRequest = Nothing ‘clean up memory and thanks for playing
End Sub

Step 2:  SETUP YOUR SCHEDULED TASK
Open Windows scheduler and add a task, browse to your newly created vbs file and set your schedule up. 

Step 3: TEST IT
right click on the task and say run if the result is 0×0 you’re golden otherwise backup and start over.

 (requirements: windows OS, any web server, any web page, direct access to scheduled tasks)

Note: If your web page is running an intensive routine remember to set the script.timeout on the page itself so it has plenty of time to complete the chore. The task scheduler is oblivious to your timeout requirements as it just calls the page and closes.

Don’t have direct access to the server you host on?  You can always use a third party web based service to setup schedules and run them.  I have never tested them as I don’t trust mission critical tasks to other vendors, but I am sure they work fine.


Oct 05 2007

Lots of Apples, do they call it a bushel?

Tag: Tech Stuff, Utah Web DesignVince Stinson @ 9:54 pm

So in a earlier post I mentioned that I bought a Macintosh 7100 in college. Do you know all the Apple products you have owned or used in the past years? The other night I found this image of Apple Form Factor Evolution 1976 through 2007. Lets see how many I have owed and used over the years. How many have you had…

Heres mine going down the list.

1) I had a friend that got a Macintosh Plus for Christmas, boy was I jealous.

2) I used a Macintosh Classic in school.

3) I went to the Colorado Institute of Art in 1993 and started designed on a Power Mac 7100. With my work schedule It was hard to use the computer lab during the open hours so I went out and bought one with the help of my dad ( more then I paid for my first truck ) so I could start my ongoing pattern of using the computer late at night instead of sleeping.

4) My first job out of college as a Art Director. 2 Macs, a Quadra 800 and a Powerbook 180c, I even had a Wacom tablet, scanner and a BW laser printer. Life was good.

5) A few years later I decided to get a new laptop so I bought a color Powerbook 190 - but no CD drive yet.

6) My office bought me a Power Mac G3 when they first came out, I was in love.

7) a year later I upgraded my laptop to a Powerbook G3 and used this as my main computer for awhile.

8 ) Yes I had a Jellybean G3 - I hated the mouse and don’t know anyone that liked the hockey puck of a mouse. I tell people that after using it one night I took it and tossed it against the brisk wall in my house, did you do the same?

9) I owned the next G4 later to glad they changed the mouse design.

10) I bought a Cube a few years ago to run OS 9 on and run a Roland vinyl plotter on, still have it. I remember when it came out and everyone wanted one cause the clear plastic case looked so cool. I bought mine on Ebay for $500.

11) I worked on a Powerbook Titanium G4 for a little while.

12) Used a iBook to.

13) Had a Power Macintosh Quicksilver when I worked for Dunlop.

14) Bought a 2nd generation Ipod ( a brick with a firewire port )

15) Bought a 15″ Powerbook Aluminum. Still use this workhorse and love it.

16) When I bought the last laptop I got a Ipod click wheel and gave it to my wife.

17) Bought a 17″ Powerbook Aluminum. Used it for a couple months then sold it because for a laptop the screen was to big for me.

18) I have a Power Macintosh G5 at my office now.

19) ipod shuffle, looks like a pack of gum. Love this for Mt Biking because its light and small.

20) Mac Mini, I have this hooked up to my LCD TV at home and Bose theater to listen to Itunes and Pandora internet radio. Nice and small.

21) Ipod Nano, I love this Ipod and use it all the time. Its small, holds my playlists and works great. Best Ipod I ever have bought. Actually have 2 of these, one for me and one for my wife.

22) So I bought a video Ipod on Ebay because I knew a guy that swore by his, I never really used it except for a hard drive so I ended up just selling it not to long ago. How many Ipods do I really need.

23) IPhone, this is the greatest phone / planner I have ever owned. I love how it snycs with my outlook address book and calendar with no hick ups. Mail is so east to use, I can have a client email me in the evening when I’m away from the office and send them a reply instantly. Its really helped to make then feel we are always looking after them. If you don’t have one go out and buy one.

24) Apple Monitors I have had all three listed - Studio CRT Display

25) Cinema Display

26) Aluminum Cinema Display - I have this one in my home office and use with my Powerbook. At the office I’m running dual Dell 24″ displays and love them.

27) I just replaced this Airport because lightning touched not to far from my house and burned it out even with a power strip.

28) I like the new Airport Extreme at home. The slimmer design is more appealing the to spaceship looking one they had before and I can use a backup hard drive hooked up to to and connect to it wireless. Pretty cool.

29) I guess I have used all the mice listed but still prefer the Logitech click wheel mouse better then any of them.

So looks like I’ve contributed to the time line a little. In our office we run both platforms for testing, design and applications but I will be a Mac geek forever.


Sep 10 2007

How to get MSN Messenger on your iPhone and more…

Tag: Tech StuffMatthew Moeller @ 8:47 am

It’s not really a shocker that Microsoft doesn’t want you using your iphone to get MSN Messenger, they want to keep some exclusivity for their mobile OS.  Unfortunately the Apple iPhone is leaps and bounds ahead of the mobile tech of the much larger Microsoft has been able to develop. I personally have owned and used every version to date of the windows mobile OS on a myriad of the latest phones, and they all leave you asking the same thing, I paid how mouch for this?  Each phone had similar issues, the phone functionality was horrible, you were lucky if you could get the blue-tooth headset to sync in time to answer a call if you were out and about. Or better yet you try and place calls and find that you have to reboot in order to get it dial successfully.  The OS wasn’t fast enough to play a simple low bit rate video without hiccups and snags, if you had more than one app open it crawled, web browsing was worthless and displayed all funky cause webmasters didn’t care about it, and to top it off was a bugger to manipulate something as simple as email.

I can honestly say the iPhone fixes all those issues, but lacks some fairly obvious and unfortunately highly useful features such as Voice dialing, copy and paste, and multiple email deletion. It also limits your email to only holding 200 messages which is pretty lame for 8GB of storage.  Looking forward to the next firmware release, let’s hope they have a solution to at-least one of those issues in it. 

We use MSN Messenger in our office for everything and were hoping that the web based version located at http://webmessenger.msn.com would work for us on the iPhones, want to see some cool error messages try visiting that page on your iPhone.  There are a couple solutions out there but the best we’ve found is http://iphone.mundu.com , which coincidentally you can use it with the big four im tools not just MSN Messenger. We have been using it without issue for a while now and it gets the job done. Give it a shot, it’s free.


Aug 05 2007

Wordpress 2.x Spell checker Error “could not execute AJAXcall, server didn’t return valid a xml” How to fix

Tag: PHP, Tech StuffMatthew Moeller @ 11:09 pm

I was having a hell of a time getting the spellchecker in Wordpress 2.2.2 to work properly and could not find a solution online anywhere so I decided to post what worked for me.  Two hours of searching online resulted in nothing so when all else fails you are left to your own devices.  To preface this post I am using IIS6 and PHP5.  Everytime you click the spell check icon you get the error “could not execute AJAXcall, server didn’t return valid a xml” .

How to fix this error on a win32 box:

  1. Crack open your php.ini file and uncomment extension = php_pspell.dll, and make sure it actually exists in your ext/ dir.
  2. Download the aspell installer from aspell.net (win32 version) and the dictionary installer (en)
  3. Now the funny thing is the win32 installer files appear to have inccorrect linefeeds so when you run the spell checker after install it still blows up but with a new error like “iso8859-1 is not in the proper format”. I downloaded the source files via ftp and manually replaced them in the aspell directory.  FTP link , (version aspell-w32-0.50.3)
  4. If your php path is in system32/ you need to copy the .dll files there from the aspell/bin/ dir.
  5. Finally you need to actually configure your spellchecker within WordPress 2.2.x, open the file “wordpress\wp-includes\js\tinymce\plugins\spellchecker\config.php” and comment out the Google spell checker that appears not to work at all, and those who have it working have a sweet delayed response time. Next uncomment this line: “require_once(”classes/TinyPspell.class.php”); // Internal PHP version”
  6. Don’t forget to turn on the spell check in the first place in config.php “$spellCheckerConfig['enabled'] = true;”
  7. Restart IIS
  8. Enjoy tested and it is now working in IE7 and Firefox 2.X

Between the incorrect format of the aspell install files and the weak online support for this Ajax error, it was a solution found through trial and error.  Not bagging WordPress, free is free, and it is a killer app which is worth the debug time.  Hope this helps other frustrated hosting providers, not that it isn’t a good use of 3hours on a sunday.  For more web site design and hosting tips visit Red Olive Creative.