NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Restarting the Server Remotely

Written by Eric Smith, Northstar Computer Systems LLC

Steve Neel sent in a tip on how to create a script to start and stop your web server without having to be on the console. The script will prompt for the server name and whether you want to start or stop the services. This script even works via dial-up connection. Of course, you need administrator privileges on the server for this to work.
'This script will Start or Stop the IIS related services on a remote server
'
Dim ServerName
Dim ServiceSet
Dim Service
Dim StartStop

Sub StopServices()
    Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" _
      & servername).ExecQuery("select * from Win32_Service")
    For each Service in ServiceSet
        if Service.Description = "World Wide Web Publishing Service" then
            If Service.State = "Running" Then
                Service.StopService()
            End If
        ElseIf Service.Description = "Microsoft SMTP Service" then
            If Service.State = "Running" Then
                Service.StopService()
            End If
        ElseIf Service.Description = "FTP Publishing Service" then
            If Service.State = "Running" Then
                Service.StopService()
            End If
        ElseIf Left(Service.Description, 9) = "IIS Admin" then
            If Service.State = "Running" Then
                Service.StopService()
            End If
        ElseIf Service.Description = "MSDTC" then
            If Service.State = "Running" Then
                Service.StopService()
            End If
        End If
    Next
End Sub

Sub StartServices()
    Set ServiceSet =
GetObject("winmgmts:{impersonationLevel=impersonate}!//" &
servername).ExecQuery("select * from Win32_Service")
    For each Service in ServiceSet
        if Left(Service.Description, 9) = "IIS Admin" then
            If Service.State <> "Running" Then
                Service.StartService()
            End If
        ElseIf Service.Description = "Microsoft SMTP Service" then
            If Service.State <> "Running" Then
                Service.StartService()
            End If
        ElseIf Service.Description = "FTP Publishing Service" then
            If Service.State <> "Running" Then
                Service.StartService()
            End If
        ElseIf Service.Description = "World Wide Web Publishing Service" Then
            If Service.State <> "Running" Then
                Service.StartService()
            End If
        ElseIf Service.Description = "MSDTC" then
            If Service.State <> "Running" Then
                Service.StartService()
            End If
        End If
    Next
End Sub

ServerName = InputBox("Enter the NetBIOS Name of the Server you would like to process")
StartStop = InputBox("Would you like to START or STOP the IIS services? " _
   & (Chr(13) & Chr(10)) & "Please Enter 'start' or 'stop'" & (Chr(13) & Chr(10)) _
   & "OR 'quit' to Exit","Start Or Stop Remote IIS Services","quit")
If lcase(StartStop) = "stop" Then
    StopServices
    Wscript.Echo "Services Stopped!"
ElseIf lcase(StartStop) = "start" Then
    StartServices
    Wscript.Echo "Services Started!"
Else
    Wscript.Echo "No Action Taken."
End If

Keywords: [ Uncategorized ASP Tips ]

Publication Date: 4/1/2000