vivek
vivek

Reputation: 47

How do I reset IIS programmatically?

Is there any alternative to the IIS Administrative UI that can be used to reset IIS from a program?..

now we have created a batch file if iis reset and scheduled it every hour......

i just wanted something so that we should not be able to reset iis....

Upvotes: 2

Views: 5935

Answers (4)

anupad
anupad

Reputation: 1

there is file called iisreset.exe in system32 folder. You can create a batch to execute it.

Upvotes: 0

Christopher G. Lewis
Christopher G. Lewis

Reputation: 4835

Using the WMI interface, you can programatically recycle the AppPool

Use MgmtClassGen from the SDK to generate your WMI class:

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\mgmtclassgen" IIsApplicationPool        /n root\MicrosoftIISv2 /l VB /p IIsApplicationPool.vb

-or-

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\mgmtclassgen" IIsApplicationPool        /n root\MicrosoftIISv2 /l CS /p IIsApplicationPool.cs

Then use this in your code:

Dim msScope As New ManagementScope("root\MicrosoftIISv2")
Dim wmiIISAppPool As IIsApplicationPool
Try
    wmiIISAppPool = New IIsApplicationPool(msScope, String.Format("W3SVC/AppPools/{0}", _AppPoolID))
    'Recycle it
    wmiIISAppPool.Recycle()

Catch ex As Exception
    LogEvent("    *** ERROR - AppPoolRecycle failed: AppPoolID: {0} {1}", _AppPoolID, ex.Message)
Finally
    If Not wmiIISAppPool Is Nothing Then wmiIISAppPool.Dispose()
End Try

Upvotes: 1

Klathzazt
Klathzazt

Reputation: 2454

You can also use iisreset from command prompt to restart all of IIS.

I believe tvanfossan answer is ok but the interface he recommends I think can only reset all of IIS once. You may want to programmatically identify which process running in your application pool you want to kill if you have a hung process.

I suggest looking at the scripting interfaces as well: http://msdn.microsoft.com/en-us/library/ms526050.aspx

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532755

Look at the IIS Admin API, in particular the IIisServiceControl interface.

Upvotes: 2

Related Questions