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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to Scripting need help closing games 2

Status
Not open for further replies.

RITec

MIS
Joined
May 15, 2002
Messages
98
Location
US
Hi Everyone,

I am new to scripting so any help much appreciated.

I ran across a script to close an application modified it to close Sol.exe,

Worked great

Created separate scripts to close winmine, and freecell, etc

I tried my luck at creating an array (never done that before)to just have one script but only the first instance close can someone let me know what I am doing wrong.

This works

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & 

"\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancecreationevent " _
        & " within 1 where TargetInstance isa 'Win32_Process'")
i = 0
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    if objLatestProcess.TargetInstance.Name = "sol.exe" then
    objLatestProcess.TargetInstance.Terminate
    End if
Loop

This closes sol.exe, and freecell.exe, but not winmine.exe

Code:
'Attemp to stop or shut down local games
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & 

"\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancecreationevent " _
        & " within 1 where TargetInstance isa 'Win32_Process'")
i = 0
Tapps = Array("freecell.exe","sol.exe","winmine.exe")
For each tapp in tapps
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    if objLatestProcess.TargetInstance.Name = tapps then
    objLatestProcess.TargetInstance.Terminate
    End if
Loop
next
Thank You

RITec
 
Try changing this:
Code:
Tapps = Array("freecell.exe","sol.exe","winmine.exe")
For each tapp in tapps
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    if objLatestProcess.TargetInstance.Name = tapps then
    objLatestProcess.TargetInstance.Terminate
    End if
Loop
with this:
Code:
Tapps = Array("freecell.exe","sol.exe","winmine.exe")
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    For j=0 to UBound(Tapps)
        if objLatestProcess.TargetInstance.Name = Tapps(j) then
            objLatestProcess.TargetInstance.Terminate
        End if
    Next
Loop

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Thank you TomThumbKP

Worked !!

If you have time could you explain the ubound(Tapps)

Thank you
RITec
 
UBound(Tapps) returns the index of the Tapps array that is currently in use. Just to try something (if you want), try replacing that same block of code with:
Code:
strTapps = "freecell.exe sol.exe winmine.exe"
Tapps = Array("freecell.exe","sol.exe","winmine.exe")
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
        if InStr(strTapps, LatestProcess.TargetInstance.Name) > 0 Then
        objLatestProcess.TargetInstance.Terminate
    End if
Loop
I'm curious to see if it would work as well.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Hmmm...there should be a better way to do this than by looping an array. That could get quite expensive if you have many programs that you want to look for. If you want I could show you how to do it using a dictionary. The dictionary .Exists method should be faster than looping an array.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I know this doesn't pertain to scripting, but I turned off my users games through Group Policy. Unfortuneatly, this only works with Server 2003, and XP. I might use this for the 2000 workstations....
 
Without array:
strTapps = ",freecell.exe,sol.exe,winmine.exe,"
Do While i = 0
Set objLatestProcess = colMonitoredProcesses.NextEvent
If InStr(strTapps, "," & LatestProcess.TargetInstance.Name & ",") > 0 Then
objLatestProcess.TargetInstance.Terminate
End if
Loop

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sure That would be great.

They are now adding to the list aim, msn etc

 
Well, PHV's technique above would allow you to do it without having to loop an array. I'm a little peeved as to why his would work and my InStr method wouldn't. I need to spend some time looking into that.

Anyway. If I were being tasked with this, then I would put all of the exe names into a text file, one per line. Then I could read them into a dictionary. Assume that you make a file called exclusion.txt, then:
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("exclusions.txt")
Set oExcludeDict = CreateObject("Scripting.Dictionary")

While oFile.AtEndOfStream <> True
    strName = oFile.ReadLine
    On Error Resume Next
    oExcludeDict.Add strFile, 1
    On Error Goto 0
Wend
oFIle.close
Set oFso = Nothing
Set oFile = Nothing
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    For j=0 to UBound(Tapps)
        if oExcludeDict.Exists(objLatestProcess.TargetInstance.Name) = True then
            objLatestProcess.TargetInstance.Terminate
        End if
    Next
Loop

Then whenever they want to add another exe to the list, you can just append it to the end of the text file.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
TomThumbKP, I think you don't need to browse an inexistant array when you have the dictionary [wink]
 
Oops...beaten by the evil cut-n-paste again. Corrected:
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("exclusions.txt")
Set oExcludeDict = CreateObject("Scripting.Dictionary")

While oFile.AtEndOfStream <> True
    strName = oFile.ReadLine
    On Error Resume Next
    oExcludeDict.Add strFile, 1
    On Error Goto 0
Wend
oFIle.close
Set oFso = Nothing
Set oFile = Nothing
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    if oExcludeDict.Exists(objLatestProcess.TargetInstance.Name) = True then
            objLatestProcess.TargetInstance.Terminate
        End if
Loop

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
tgf - we are looking into 2003 server but, currently we only have 2000.

we do currently have a group policy that maps user and shared drives. I tried to add cscript \\Path\temp.vbs to it but it did not work.

PHV - that is not working for me.

I am going to try and restart the computer because I am getting an error

 
And this ?
strTapps = ",freecell.exe,sol.exe,winmine.exe,"
Do While i = 0
Set objLatestProcess = colMonitoredProcesses.NextEvent
If InStr(1, strTapps, "," & LatestProcess.TargetInstance.Name & ",", 1) > 0 Then
objLatestProcess.TargetInstance.Terminate
End if
Loop

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV
Error reads

Script: Path to vbs file
Line: 13
Char: 3
Error: Object required: 'LatestProcess'
code: 800A01A8
Source: Microsoft VBScript runtime error
 
PHV
No Error but still does not work for me.

RITec
 
I don't know if it matters but I am saving all of these as filename.vbs

PHV

I did get the same error

Tom
I copied your example and added text file in same folder
ran script
Error
script: path to script
line: 15
Char: 5
Error: Object required:'colMonitored Processess'
Code: 800A01A8
Source: Microsoft VBScript runtime error
 
RITec, I guess you omit the beginning of the script:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
ExecNotificationQuery("select * from __instancecreationevent " _
& " within 1 where TargetInstance isa 'Win32_Process'")
i = 0

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks you for your patience
PHV

This is what I have
Code:
'Attemp to stop or shut down local games
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" _
  & strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
  ExecNotificationQuery("select * from __instancecreationevent " _
  & " within 1 where TargetInstance isa 'Win32_Process'")
i = 0 
strTapps = ",freecell.exe,sol.exe,winmine.exe,"
Do While i = 0
  Set objLatestProcess = colMonitoredProcesses.NextEvent
  If InStr(1, strTapps, "," & LatestProcess.TargetInstance.Name & ",", 

1) > 0 Then
    objLatestProcess.TargetInstance.Terminate
  End if
Loop

I get error
script: path to script
line: 13
Char: 3
Error: Object required:'LatestProcess'
Code: 800A01A8
Source: Microsoft VBScript runtime error
 
Change this bit:
If InStr(1, strTapps, "," & LatestProcess
to
If InStr(1, strTapps, "," & objLatestProcess

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top