Link to home
Start Free TrialLog in
Avatar of mmiller3442
mmiller3442Flag for United States of America

asked on

VBS script to restart IIS on remote server

Hello

I wanted to get a vbs script or a batch file created that would restart IIS on a remote server. I was hoping the batch or VBS script when run it will pop up with a box asking for the server name and after entering the server name it will restart IIS on that remote server

I have the following syntax below to get the pop up box but don't know how to get the rest of the vbs script to execute iisreset


'Get the servername
strServer = InputBox("Please submit the server name","IIS Restart script")
IF strServer = "" THEN
  WScript.Echo "No servername was given or you clicked Cancel"
  WScript.Quit
END IF

'check if the server is online
Srvpad = "\\"&strServer&"\c$\WINDOWS\system32"
SET oChksrv = CreateObject("Scripting.FileSystemObject")
IF oChksrv.FolderExists(Srvpad) = FALSE THEN  
  WScript.Echo "Server " & strServer & " doesn't exist or isn't online!"
  WScript.Quit
END IF


Any help would be appreciated
ASKER CERTIFIED SOLUTION
Avatar of plusone3055
plusone3055
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 Justin Ellenbecker
Replace ServiceName on lines 16,21,26 and 31 this will stop the service and all dependants.  Then it will sleep for 10 seconds and start them back up.  Always use the main service name.  I think in your case it IIS Management, but I would have to double check.
'Get the servername
strServer = InputBox("Please submit the server name","IIS Restart script")
IF strServer = "" Then
	WScript.Echo "No servername was given or you clicked Cancel"
	WScript.Quit
END If
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")

'check if the server is online
Srvpad = "\\"&strServer&"\c$\WINDOWS\system32"
SET oChksrv = CreateObject("Scripting.FileSystemObject")
IF oChksrv.FolderExists(Srvpad) = FALSE THEN  
	WScript.Echo "Server " & strServer & " doesn't exist or isn't online!"
	WScript.Quit
Else
	Set colServiceList = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='ServiceName'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
	For each objService in colServiceList
    	objService.StopService()
	Next
	Wscript.Sleep 20000
	Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='ServiceName'")
	For each objService in colServiceList
    	errReturn = objService.StopService()
	Next
	WScript.Sleep 10000
	Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='ServiceName'")
	For each objService in colServiceList
    	errReturn = objService.StartService()
	Next
	Wscript.Sleep 20000
	Set colServiceList = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='ServiceName'} Where " & "AssocClass=Win32_DependentService " & "Role=Dependent" )
	For each objService in colServiceList
    	objService.StartService()
	Next
	WScript.Echo "All operations completed"
End If

Open in new window

Here's a code for a batchfile

WMIC /node:"servername" service where caption="DNS Client" Call stopservice && WMIC /node:"servername" service where caption="DNS Client" Call startservice

With /user and /password you can define credentials to authenticate


Note: To execute IISReset is a better way to reset the IIS Server than restaring the service
Avatar of mmiller3442

ASKER

Thanks Guys

I'm going to use the VBS script route

I have modified the code below with the service names but the only service that restart is the WWW service, any thoughts?

Else
        Set colServiceList = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='IISADMIN'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
        For each objService in colServiceList
        objService.StopService()
        Next
        Wscript.Sleep 20000
        Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='W3SVC'")
        For each objService in colServiceList
        errReturn = objService.StopService()
        Next
        WScript.Sleep 10000
        Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='W3SVC'")
        For each objService in colServiceList
        errReturn = objService.StartService()
        Next
        Wscript.Sleep 20000
        Set colServiceList = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='IISADMIN'} Where " & "AssocClass=Win32_DependentService " & "Role=Dependent" )
        For each objService in colServiceList
        objService.StartService()
        Next
        WScript.Echo "All operations completed"
End If
They need to all be the same service make them all IISADMIN and that should do it WWW is the only dependant of W3SVC i think is why it is happening.
Made that change below and the only task that initiates is a stop command to WWW. No command is sent to start WWW or any other services



     WScript.Quit
Else
        Set colServiceList = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='IISADMIN'} Where " & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
        For each objService in colServiceList
        objService.StopService()
        Next
        Wscript.Sleep 20000
        Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='IISADMIN'")
        For each objService in colServiceList
        errReturn = objService.StopService()
        Next
        WScript.Sleep 10000
        Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='IISADMIN'")
        For each objService in colServiceList
        errReturn = objService.StartService()
        Next
        Wscript.Sleep 20000
        Set colServiceList = objWMIService.ExecQuery("Associators of " & "{Win32_Service.Name='IISADMIN'} Where " & "AssocClass=Win32_DependentService " & "Role=Dependent" )
        For each objService in colServiceList
        objService.StartService()
        Next
        WScript.Echo "All operations completed"
It possible we are not waiting long enough for the services to stop.  Please adjust the sleep numbers to all be 45000 that is 45 seconds between steps.  The other thing you can do is to do it manually and time them out and see what works.