![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
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 code
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
For Each objItem in colItems
if objItem.Caption = "notepad.exe" then
msgbox ("It it's working great!")
appvar = 0
elseif appvar <> 0
appvar = 1
end if
Next
if appvar >= 1 then
strComputer = "."
Hidden_Window=5
Set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
bjConfig.ShowWindow = Hidden_Window
Set objProcess = GetObject ("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("notepad.exe", null, objConfig, intProcessID)
WScript.echo "Notepad was not running, but it is now!"
else
end ifAdded 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.
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
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
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
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 code
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
'Going to cycle through all process running and incrementing.
'If notepad is found in running process, it'll updated "NoteRun" var w/ a numeric
'Put a 0 in the NoteRun var
NoteRun = 0
For Each objItem in colItems
'Checks to see if notepad is in the list of running processes.
if objItem.Caption = "notepad.exe" then
NoteRun = 1
else
AppVar = AppVar + 1
end if
Next
AppVar = AppVar + NoteRun
'Wscript.echo "You have " & appvar & " applications running! You have " & NoteRun & " notepad instances running!"
'Checks NoteRun var to see if it's running.
if NoteRun = 0 then
strComputer = "."
Hidden_Window=5
Set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
bjConfig.ShowWindow = Hidden_Window
Set objProcess = GetObject ("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("notepad.exe", null, objConfig, intProcessID)
WScript.echo "Notepad was not running, but it is now!"
end ifAlso, do i have to have a certian amount of posts before I can modify my own posts / headers? -marshall
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
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")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
'Going to cycle through all process running and incrementing.
'If notepad is found in running process, it'll updated "NoteRun" var w/ a numeric
'Put a 0 in the NoteRun var
NoteRun = 0
For Each objItem in colItems
'Checks to see if notepad is in the list of running processes.
if objItem.Caption = "notepad.exe" then
NoteRun = 1
else
AppVar = AppVar + 1
end if
NextWell, 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")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
If colItems.Count = 0 Then
' Start notepad here
End IfHope this helps!
__________________
"I'm not a genius. Why do I have to suffer?" |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
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.
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. ![]()
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|