Link to home
Start Free TrialLog in
Avatar of misha1
misha1Flag for United States of America

asked on

To stop and start IIS in VB script


 Hello guys and gals.
 I have a little simple script from MSDN
 to stop and start IIS (Internet Info Service).
 ------------------------------------------
 Net stop iisadmin /y
 Net start iisadmin
 -------------------------------------------

 Now I need to wrap it in VB script code, so
 to put on HTML page.
 
 Thanks.
 
Avatar of procyn00
procyn00

Ok.... heres a dumb question.

How are you going to execute a VB script code on a page if the page can't be served cause the servers stopped.
Avatar of Anthony Perkins
procyn00,

I think the questioner is asking about vbs not ASP.  You can run vbs files without IIS.

Anthony
This is just a command line call, yes?  You should be able to execute it using the WScript.Shell class' Run method:

http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/wsh/htm/wsMthRun.asp
Avatar of misha1

ASKER


 So, as I understand,
 I would not embbed this in VB Script but
 would run an notepad with this code.
 
 Is there a way to embed this in code itself?
 
 
If you mean you want to run this from a regular VB program, then you should look into just using the Shell function to run that command line call.
Avatar of misha1

ASKER


 As I understand Shell runs only executables
 and can't run command lines,
 from MSDN:
 
 "Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero"
 
 I have few line commands that I have to execute.

ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of misha1

ASKER


 Thanks,
 That's more in line with what I want.
 Now, do you know what command prompt (cmd.exe)
 I would use for this IIS? This cmd.exe or the like
 has to exist there.
 
 Can you give me the exact code to run in this instance?
 
 
 
 
 
I'm not on my IIS machine right now, but what I posted above should work (at least it looks right to me).  I know for Win2k you will use cmd.exe and then the command line string to pass to it.  If you get an error like "File Not Found", it is probably not finding cmd.exe...it may be command.exe for that machine.  Or, you may need to put the fully qualified path (e.g., C:\WINNT\System32\cmd.exe) but I dont think you should have to.
Avatar of misha1

ASKER


 Thanks,
 Will try it.
Avatar of misha1

ASKER


 Tried it out.
 The code runs good, finds cmd.exe, but
 IIS was not stoped, tried just the "stop" command.
 
 Can you try this code on your IIS when you have a chance.
 Will increase points to 75.
 
 Thanks.
Try this:

Shell "cmd.exe /c Net stop iisadmin /y"
There are better ways of skinning this cat, but just to show you one way:

Create a text file in the root of C:\ with the following line:
MsgBox "You got it!"

and name the file: virus.vbs

Then add the following line:

Shell "cmd /c c:\virus.vbs"

Of course you will have to use Command instead oc Cmd if this is Windows 9.x, but you get the picture.

Anthony




Oops, I think Azra just beat me to it.

Anthony
Avatar of misha1

ASKER


 Thanks guys,
 Stopping IIS worked fine,
 but when I added 'start' line, it didn't work.
 
 I've added:
 Shell "cmd.exe /c Net start iisadmin"

 Looks good, but didn't start IIS, it remained stopped.
 Also noticed that IIS stops for about 5 to 10 seconds.
 So maybe code is too fast for it?
 
 Misha1.
 
 
That is possible...you can use some more complex code to determine when a Shelled application finishes, so then you could call your next line.  Here are a couple of samples:


"Determining the End of a Shelled Application Using GetExitCodeProcess"
http://www.mvps.org/vbnet/index.html?http://www.mvps.org/vbnet/faq/main/getexitcprocess.htm

"Determining the End of a Shelled Application Using WaitForSingleObject"
http://www.mvps.org/vbnet/index.html?http://www.mvps.org/vbnet/faq/main/waitforsingleobject2.htm
Avatar of misha1

ASKER


 
 We are making lots of progress.
 Tried event handling code from the links, works fine.
 Now Start command line waits for Stop command line to finish.
 But when Start command line starts, it opens Command
 Progress Window, displays "Starting IIS", but then
 closes everything and IIS remains stopped.
 So, in other words, it starts "Start" process and then
 aborts it.
 
 Can you try it on your IIS.
 
 Thanks.