Solved

Batch Script to restart IIS remotely

Posted on 2009-04-03
25
6,142 Views
Last Modified: 2012-05-06
I'm looking for a way to write a batch or vbscript to restart iis remotely.  This script is inserted into my backup command script that runs batch or vbscript type commands.  The problem this computer doesn't have iis installed on it so I can't jsut run iisrest/timeout:300 computername.  Is there a way to have a script that connnects to this server and runs the iisreset command?  Currently I runnign a batch script natievly on the server with iis but would like to include this after my backup command since thi sis a requirement after I shutdown the db it runs on for the backup.  When the db comes back online iss needs to be reset.  Including this in the backup script with make managing things easier.
0
Comment
Question by:georgedschneider
  • 10
  • 8
  • 3
  • +2
25 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 250 total points
Comment Utility
First, create a scheduled task on the server that runs your batch file.  Disable the task so that it doesn't run automatically.

Then you can manually run the task from your remote host by doing this:

schtasks.exe  /run  /s yourServerName  /tn nameOfTheTask
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
Hi there.

I have just typed another suggestion, close to the first.

You could schedule a task on the iisserver that runs iisreset.exe.
I have had problems by calling iisreset in a script, so I would recommend
to just call the iisreset.exe directly in the task.
You could call the task "Iisreset" :-)

Then afterwards, you download psexec.exe from

www.sysinternals.com,

put it in the system32 folder on the machine or server you want to
use for initiating the iisreset on your webserver.

You then call this in a script:

start psexec \\ddkcx01 schtasks /run /TN "Iisreset"

and the local scheduled task is being run.

The good thing about this solution is, that you should not worry much about
your rights to do stuff through the network.
You initiate a local job, that is run with an account belonging to the machines "administrators" account,
and you have all the necessary rights.

If you ever is starting the job, and it won't stop or has some problems,
you can stop the job by creating a script with:

start psexec \\ddkcx01 schtasks /end /TN "Iisreset"

But that should not be necessary under normal circumstances.
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
Sorry about 2 of the lines

I by mistake included a local servername from one of my own scripts.

start psexec \\webserver schtasks /run /TN "Iisreset"

start psexec \\webserver schtasks /end /TN "Iisreset"
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
You can simply copy iisreset.exe from the webserver to your backup server; it's a stand-alone utility, it doesn't require IIS on the local machine to restart IIS remotely on another machine.
0
 

Author Comment

by:georgedschneider
Comment Utility
I tried that but I get an interface not registered.  How do I register the interface or what do I need to do to get it work?  I copied it to the sytsem32 directory in the exact same location as it is on a working server with iis installed.
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
What OS are you using for the remote iis reset?
0
 
LVL 3

Expert Comment

by:qualchoice-it
Comment Utility
Use the SC.exe command, built into XP, W2k3, or installed from the W2k reskit.

C:\>sc
DESCRIPTION:
        SC is a command line program used for communicating with the
        NT Service Controller and services.
USAGE:
        sc <server> [command] [service name] <option1> <option2>...

        The option <server> has the form "\\ServerName"
        Further help on commands can be obtained by typing: "sc [command]"
        Commands:
          query-----------Queries the status for a service, or
                          enumerates the status for types of services.
          queryex---------Queries the extended status for a service, or
                          enumerates the status for types of services.
          start-----------Starts a service.
          pause-----------Sends a PAUSE control request to a service.
          interrogate-----Sends an INTERROGATE control request to a service.
          continue--------Sends a CONTINUE control request to a service.
          stop------------Sends a STOP request to a service.
          config----------Changes the configuration of a service (persistant).
          description-----Changes the description of a service.
          failure---------Changes the actions taken by a service upon failure.
          qc--------------Queries the configuration information for a service.
          qdescription----Queries the description for a service.
          qfailure--------Queries the actions taken by a service upon failure.
          delete----------Deletes a service (from the registry).
          create----------Creates a service. (adds it to the registry).
          control---------Sends a control to a service.
          sdshow----------Displays a service's security descriptor.
          sdset-----------Sets a service's security descriptor.
          GetDisplayName--Gets the DisplayName for a service.
          GetKeyName------Gets the ServiceKeyName for a service.
          EnumDepend------Enumerates Service Dependencies.

        The following commands don't require a service name:
        sc <server> <command> <option>
          boot------------(ok | bad) Indicates whether the last boot should
                          be saved as the last-known-good boot configuration
          Lock------------Locks the Service Database
          QueryLock-------Queries the LockStatus for the SCManager Database
0
 

Author Comment

by:georgedschneider
Comment Utility
windows 2003
0
 
LVL 3

Expert Comment

by:qualchoice-it
Comment Utility
Remeber to include the stop of the dependencies as well

1 WorldWide Web Publishing Service
2 FTP Publishing Services (If you have ftp installed)

So if I wanted to scipt this out to stop IIS it would look like this, make sure you do this under an admin account

@echo on
sc \\"servername" stop W3SVC
sc \\"servername" stop MSFTpsvc
sc \\"servername" stop IISadmin

0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
What OS is running on the webserver, and what is the *exact* error message you're getting on the machine you're running iisreset.exe from? Do you have a firewall running on the webserver?

qualchoice-it,
it's not that easy with sc.exe. sc sends the command and returns *immediately*; it does NOT wait for a successful finish.
0
 

Author Comment

by:georgedschneider
Comment Utility
the web server is running windows 2003.  Te machine I'm runnign iisreset form is also a windows 2003.  All i did is copy iisreset from a web server running iss that I can run iisreset form to this server.  Is there something else i need to do such as copy a dll or register a dll to get it ot work?  

The error message is interface not registered.
0
 
LVL 3

Expert Comment

by:qualchoice-it
Comment Utility
then add a SLEEP 60 between each one, and put some logging in it

@echo on
sc \\"servername" stop W3SVC
SLEEP 60
echo stop service >> \\"servername"\"folder"\%computername%\logfile.txt
sc \\"servername" stop MSFTpsvc
SLEEP 60
echo stop service >> \\"servername"\"folder"\%computername%\logfile.txt
sc \\"servername" stop IISadmin
SLEEP 60
echo stop service >> \\"servername"\"folder"\%computername%\logfile.txt
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
What happens if you install asp.net on the server, you want to run iisreset from.
It looks like they are close connected.
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
What happens if you just run
%systemroot%\system32\iisreset.exe /?
*exactly* like this, do not leave out the ".exe"?
Is "interface not registered" the *exact*, literal, and only information you get?
Do you get this error as popup or in the console?
Again: is there a firewall running on the webserver blocking traffic from the "backup" machine?
What is the file version of iisreset.exe?
0
 

Author Comment

by:georgedschneider
Comment Utility
When  run the following coammnd %systemroot%\system32\iisreset.exe /? I get the help mneu as expected.

I tried running the whole command like this and still get the interface not registered which is the only message I get.  

I have no firewalls running between machines either.
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
You need to install iis on the Windows 2003 server, that you want to use for remote restart.
Then you can just stop the www service and disable it.

I tried that on a Windows XP - I think it is the only way.

Then the iisreset is available to you.
0
 

Author Comment

by:georgedschneider
Comment Utility
What about just installing the management tools for iis6 would this work as well?
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
I think it is worth a try to install that. It wouldn't do any harm,
and the chances are pretty good, that this would work.
If it doesn't you could try my suggestion about installing IIS.
0
 

Author Comment

by:georgedschneider
Comment Utility
Are there management tools for IIS for Windows 2003 outside of installing full fledge IIS?  The management tools I can find seem to be only for XP based computers?
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
Or you could try the new version.
I don't know if it supports iis 6 too,
but I think the chances are pretty fair.

http://www.microsoft.com/downloads/details.aspx?familyid=32C54C37-7530-4FC0-BD20-177A3E5330B7&displaylang=en
0
 

Author Comment

by:georgedschneider
Comment Utility
I don't think the IIS manager is included with the toolpack which is what i think is needed for iisreset to work.
0
 
LVL 6

Assisted Solution

by:Kentrix70
Kentrix70 earned 250 total points
Comment Utility
I have found a solution.

On the 2003 server you want to use for remote restart.

Click Add or Remove programs

click add/remove windows components
doubleclick Application Server
doubleclick Internet Infromation Services (IIS)
Choose Internet Information Services Manager (common files will also be selected).

click ok a couple of times.

The iisreset command is available to you,
but if you look under Internet Information Services (IIS) Manager,
there is no websites there.
0
 
LVL 6

Expert Comment

by:Kentrix70
Comment Utility
I just checked, and there is no WWW service, on the server, I tried this test on.

So this setup should work for you.
0
 

Author Comment

by:georgedschneider
Comment Utility
That worked perfectly.  I did however remember that windows taks scheduleder can have multiple schedules set up so I was able to create a batch script to run iisrest and scheduled it to run weekly at 7:00 AM after the backup and on the first of the month which is when the backup runs.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now