Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Using an Array to assign to a Variable

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2351
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Using an Array to assign to a Variable
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Using an Array to assign to a Variable - 3/15/2005 3:45:01 AM   
  TommyTalula

 

Posts: 2
Score: 0
Joined: 3/15/2005
From: United Kingdom
Status: offline
Hi all,

I am new today to VBScript!!
I am trying to stop a number of Processes on the PC, then start up a couple of processes.

I have located code to stop a single process. So I though you could just create an array and use For Each to loop through the list of processes provided. But it just shuts down the 1st process and freezes.

Any Ideas??

dim KillList(2)
killList(0)="Notepad.exe"
KillList(1)="taskmgr.exe"
KillList(2)="desktopmgr.exe"

For Each x in KillList
On Error Resume Next
ProNum = 0
InProcess = False

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


Do While InProcess = False
pickle = x
wbscript.echo "Killing Process " & pickle
If pickle = "" Then WScript.Quit
Forever = 1

Do Until Forever = 1000
Set colProcessList2 = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & pickle & "' OR Handle = '" & pickle & "'")
For Each objProcess in colProcessList2
If obj.Process.Name = pickle OR obj.Process.Handle = pickle Then InProcess = True
objProcess.Terminate()
Next
If KillForever = vbNo Then Forever = 1000
Loop
Loop
next
For Each objProcess in colProcessList2
If objProcess.Name = pickle Then Success = False

If Success = False Then
WScript.Echo "Successfully killed the " & pickle & " process"
Else
WScript.Echo "Unable to kill the " & pickle & " process"
End If
Next
 
 
Post #: 1
 
 Re: Using an Array to assign to a Variable - 3/15/2005 4:37:43 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
First, when troubleshooting a script, make sure you comment out any On error resume next's. Once you do that, you will see that you get an error on the line wbscript.echo "Killing Process " & pickle. Also, make sure that you are indenting correctly as it will help you verify that you are looping correctly. I also got an error on line 20 ( If obj.Process.Name = pickle OR obj.Process.Handle = pickle Then InProcess = True). Once I changed it to obkprocess.--- it worked. But only once. For some reason it was not checking for the next process. Try the code below.

This code works for me.

      

(in reply to TommyTalula)
 
 
Post #: 2
 
 Re: Using an Array to assign to a Variable - 3/15/2005 4:52:04 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Two syntax/logic problems that I can see.

1. If obj.Process.Name = pickle OR obj.Process.Handle = pickle Then InProcess = True

It should be:

If objProcess.Name = pickle OR objProcess.Handle = pickle Then InProcess = True

2. If KillForever = vbNo Then Forever = 1000

KillForever was not initialized with a proper value (defualt is null); thus "Forever" is never 1000. The result is an endless loop.

Your code looks more complicated than it should, I would sugget just use mbouchard's code. It is simpler, clearer, and it works =)

(in reply to TommyTalula)
 
 
Post #: 3
 
 Re: Using an Array to assign to a Variable - 3/15/2005 5:35:47 AM   
  giantlunarmoth

 

Posts: 47
Score: 0
Joined: 3/9/2005
From:
Status: offline
I have no exp w/WMI, but to demo an actual Array, I've used XP's taskkill:


      Works great on XP, and only 5 lines.

(in reply to TommyTalula)
 
 
Post #: 4
 
 Re: Using an Array to assign to a Variable - 3/15/2005 6:38:15 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
If you have a list of process name to be killed in a text file, you can also do this with the help of killtask.

for /f %f in (tasks.txt) do taskkill /f /t /im %f

One line =)

(in reply to TommyTalula)
 
 
Post #: 5
 
 Re: Using an Array to assign to a Variable - 3/15/2005 7:43:37 AM   
  giantlunarmoth

 

Posts: 47
Score: 0
Joined: 3/9/2005
From:
Status: offline
You're right...the fewer the merrier :)

(in reply to TommyTalula)
 
 
Post #: 6
 
 Re: Using an Array to assign to a Variable - 3/15/2005 9:41:27 PM   
  TommyTalula

 

Posts: 2
Score: 0
Joined: 3/15/2005
From: United Kingdom
Status: offline
Excellent...I found PSKILL & TaskKill this morning!

Now is the decider. I want to start up 2 programs after the kill process.


PSKILL seems to start up Excel & Notes easily, but I cannot get it to start another executable in the Program Files directory. Not sure why it can find 1 & not the other.

Locations are
C:\lotus\notes\notes.exe
C:\Program Files\Research in Motion\Blackberry\DesktopMgr.exe
(over came errors by PROGRA~1\RESEAR~1\BLACKB~1\DesktoipMgr.exe but just opens a DOS box with this as the title)


VBScript probably has some options, I am currently scouring the internet for some code.

Taskkill probably does not have an option to Start programs. Shame as cannot believe you managed to nail it to 1 line of code.

Any ideas for this function?

(in reply to TommyTalula)
 
 
Post #: 7
 
 Re: Using an Array to assign to a Variable - 3/16/2005 5:55:45 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
hmm... Is there a reason why you can not just execute the program directly instead of using pskill ?

(in reply to TommyTalula)
 
 
Post #: 8
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Using an Array to assign to a Variable Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts