![]() |
My first real shot at some VBScripting
Hey, i was posting before about making a VBScript to do a simple process check and if it wasn't running it would fire the process up. Well, i went ahead and took a shot at it. I replaced the app name w/ notepad so it would be easier to read out. Bassically, if notepad is not running the script runs perfect. If notepad is running, i get a couple, "It's working great!" but then it runs notepad again. I am sure it's something simple here I am just not experienced enough and kinda blind at the moement. I've been hunkering over line drive machines all day and i am cross eyed. lol
-Marshall :
' VBScript source codeAdded note:I just realize it had a script error. I think it was after I added the IF appvar >=1 statement on the last part. Arg! I will keep hackin at it. |
Ok, i keep getting a "Then" expected error. Lemme see if i can straighten that out first. It was working a couple min ago. lol
|
Finished
I spent some time on the code today and got it working exactly like i wanted. Bassically, it checks to see what processes are running, counts them, and if Notepad isn't running, it fires it up. Lemme know what you think. I can handel honestly regardless of how brutal.
:
' VBScript source codeAlso, do i have to have a certian amount of posts before I can modify my own posts / headers? -marshall |
I wish I'd seen this post sooner; I've just written something similar at work! I have a small suggestion which would improve the efficiency of this script a little; see the section that reads:
:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Well, the ExecQuery, as you'll know, takes a WQL query and runs it against the CIMV2 repository. The results are then sent back. You can trim down the size of the result very easily by adding a WHERE clause to your query: :
SELECT Caption FROM Win32_Processs WHERE Caption = 'notepad.exe'Notice I'm also no longer selecting *, but just Caption. If notepad is running, you get one result back for each instance of it, but if it's not, you get an empty collection. All that will be sent back is the Caption property, which is really all you're interested in here anyway. This doesn't allow you to count other processes in AppVar as you do, so if that's an important feature of your script you'll have to leave the WHERE clause off your WQL query, but you can still SELECT Caption rather than SELECT * which will help a little. (I'm not sure if this is possible with WQL because I haven't tried it, but it looks like this script wouldn't care if there were 2 or 5 notepad.exe instances running; it only takes action if there are none so 1 or 4 or 25,000 are all equivalent from its point of view so you could use SELECT DISTINCT Caption instead of just SELECT Caption. Like I say, I don't know if this works with WQL but if it's like SQL this will mean you'll get a maximum result collection of one record, no matter how many notepad.exes are running, which again trims down the result size...I know I'm getting a bit obsessive compulsive about this; I mean, what are we talking about, ten bytes?; but still, it's the principle of the thing isn't it? :) ) Reducing the result set like this helps a lot of you're doing this over a WAN, especially if the remote machine is running a relatively large number of processes. In this case, if you are only interested in starting notepad if there are no instances of it running, you could actually avoid iterating over the result collection with For Each; all you care about is whether the returned collection is empty. I can't recall off the top of my head whether you use the Count or the Length property (Count, I think) to find this out, but you could just do something like: :
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Hope this helps! |
Mackenga, thank you for pointing out the possible upgrades. I didn't notice that before. Now that you've made not of it, i can't beleive i didn't see that before. :D I will be testing that out this week at the house to see if i can write it to work right. I'm still gettting full understanding of the GetObject ( blah blah bla) stuff but the rest of it makes sense. Thank you. :)
|
| All times are GMT -5. The time now is 8:04 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC