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
mmiller3442Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Justin EllenbeckerIT DirectorCommented:
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

0
merowingerCommented:
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
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

mmiller3442Author Commented:
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
0
Justin EllenbeckerIT DirectorCommented:
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.
0
mmiller3442Author Commented:
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"
0
Justin EllenbeckerIT DirectorCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.