Link to home
Start Free TrialLog in
Avatar of jmohan0302
jmohan0302

asked on

Looking for a script

Need a script to find whether IIS is installed for a list of servers. If IIS is installed I need a script to find what are all the websites are hosted? is it possible to get a script for this
Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Do you mean your own servers on your own network?
Avatar of jmohan0302
jmohan0302

ASKER

yes
Avatar of RobSampson
Hi, try this.  Run it from a command prompt using
cscript ShowWebsites.vbs

Regards,

Rob.
strServers = "servers.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile(strServers, ForReading, False)
While Not objFile.AtEndOfStream
	strServer = Trim(objFile.ReadLine)
	If strServer <> "" Then
		If Ping(strServer) = True Then
			WScript.Echo vbCrLf & "Web Sites on " & strServer & ":"
			strIISPath = "IIS://" & ServerName & "/W3SVC"
			On Error Resume Next
			Set objIIS = GetObject(strIISPath)
			If Err.Number = 0 Then
				For Each objItem In objIIS
					If Item.Class = "IIsWebServer" Then
						WScript.Echo Item.name
					End If
				Next
			Else
				WScript.Echo "Error retrieving list of sites from " & strServer
			End If
		Else
			strServer & " is offline."
		End If
	End If
Wend
objFile.Close
WScript.Echo vbCrLf & "Finished."

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Hi Rob,

I am getting sysntax error when I ran this script. I tried this from my laptop. Do we need to run this only from a server
Hi Rob,

The script is not working  I am getting syntax error at line 24 Char 14. I tried running on the servers even getting the same error. Please refer to the attachment. User generated image
Kindly help me in getting this script run. Thanks
My apologies, please change this line:
                  strServer & " is offline."

to this:
                  WScript.Echo strServer & " is offline."

You can run it from any domain computer, as long as the user account has admin rights on the target machines.

Regards,

Rob.
Hi Rob,

Thanks a lot. I have corrected it . Thanks . Is there any script to find whether IIS service is installed or not from a list of servers?
OK, I've made the script check for the existence, and the status, of the IISAdmin service.  Hopefully that's enough.

Regards,

Rob.
strServers = "servers.txt"

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile(strServers, ForReading, False)
While Not objFile.AtEndOfStream
	strServer = Trim(objFile.ReadLine)
	If strServer <> "" Then
		If Ping(strServer) = True Then
			Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
			Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name = 'iisadmin'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
			blnInstalled = False
			blnRunning = False
			For Each objItem In colItems
				blnInstalled = True
				If objItem.Status = "Running" Then blnRunning = True
			Next
			WScript.Echo "IIS Installed: " & blnInstalled
			WScript.Echo "IIS Running: " & blnRunning
			If blnInstalled = True Then
				WScript.Echo vbCrLf & "Web Sites on " & strServer & ":"
				strIISPath = "IIS://" & ServerName & "/W3SVC"
				On Error Resume Next
				Set objIIS = GetObject(strIISPath)
				If Err.Number = 0 Then
					For Each objItem In objIIS
						If Item.Class = "IIsWebServer" Then
							WScript.Echo Item.name
						End If
					Next
				Else
					WScript.Echo "Error retrieving list of sites from " & strServer
				End If
			End If
		Else
			WScript.Echo strServer & " is offline."
		End If
	End If
Wend
objFile.Close
WScript.Echo vbCrLf & "Finished."

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Hi Rob,

Thanks a lot. The scripts are working fine. When I ran the script it says only one site is running. It is not giving the iis instances names. Actually I am looking for the list of websites or URLs configured under Default Website for a list of servers where IIS is configured. Could you please help me in getting the script for my requirement? Really thanks a lot.
Hi Rob,

I am enclosing the output what I get from the script. If you check the screenshot you will find a number of sites configured. I just need a script that list the configured sites for a list of servers configured with IIS. Kindly need your help in getting a script:

 User generated image User generated image
Hi, I haven't tested this too much, but see what this gives you.  I'll work some more on it tomorrow.

Rob.
strServers = "servers.txt"

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile(strServers, ForReading, False)
While Not objFile.AtEndOfStream
	strServer = Trim(objFile.ReadLine)
	If strServer <> "" Then
		If Ping(strServer) = True Then
			WScript.Echo "Checking " & strServer
			Set objWMIService = GetObject("winmgmts:\\" & strServer & "\root\CIMV2")
			Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name = 'iisadmin'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
			blnInstalled = False
			blnRunning = False
			For Each objItem In colItems
				blnInstalled = True
				If objItem.State = "Running" Then blnRunning = True
			Next
			WScript.Echo "IIS Installed: " & blnInstalled
			WScript.Echo "IIS Running: " & blnRunning
			If blnInstalled = True Then
				WScript.Echo vbCrLf & "Web Sites on " & strServer & ":"
				strIISPath = UCase("IIS://" & strServer & "/W3SVC")
				On Error Resume Next
				Err.Clear
				WScript.Echo "Connecting to " & strIISPath
				Set objIIS = GetObject(strIISPath)
				If Err.Number = 0 Then
					For Each objItem In objIIS
						If objItem.Class = "IIsWebServer" Then
							WScript.Echo "Site ID: " & objItem.name
							WScript.Echo "Server Comment: " & objItem.ServerComment
						End If
					Next
				Else
					WScript.Echo "Error retrieving list of sites from " & strServer & ". Error " & Err.Number & ": " & Err.Description
				End If
				On Error GoTo 0
			End If
		Else
			WScript.Echo strServer & " is offline."
		End If
	End If
Wend
objFile.Close
WScript.Echo vbCrLf & "Finished."

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Hi Rob,

The script is running fine. I really appreciate the efforts you have shown to me so far. Thanks a lot. The script says whether IIS is running or not. So one part of my query is resolved. The other part I am looking is that what are all the URLs configured on that server.I mean that what are the websites configured on a server. I kindly request you to give me a script which displays the configured websites on a server. Thanks

 Please find the screen shot of the above script:

 User generated image
Hi, does this give you the correct web site names?

Rob.
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "YourWebServer"

WScript.Echo "Computer: " & strComputer

Set objLocator= CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService = objLocator.ConnectServer(strComputer, "root/MicrosoftIISv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM IIsWebVirtualDir", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
	Set nodeObj = objWMIService.Get("IIsWebVirtualDirSetting='" & objItem.Name & "'") 
	WScript.Echo nodeObj.AppFriendlyName
Next

Open in new window

Hi Rob,

Yes its working fine. Is it possible to combine both the scripts. I mean if we combine both the script, I will get my output. That is the script has to find for a list of servers whether IIS server is running or not. If running it has to give the list of websites configured. My objective is to find out of the entire inventory I have to find what are all the server that are configured with IIS service and what are all the sites configured. Thanks
Sure, I was just making sure that snippet was going to give you the results before I combined them.

Try this.  I have combined the snippets.

Regards,

Rob.
strServers = "servers.txt"

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objLocator= CreateObject("WbemScripting.SWbemLocator") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile(strServers, ForReading, False)
While Not objFile.AtEndOfStream
	strServer = Trim(objFile.ReadLine)
	If strServer <> "" Then
		If Ping(strServer) = True Then
			WScript.Echo "Checking " & strServer
			On Error Resume Next
			Set objWMIService = objLocator.ConnectServer(strServer, "root/MicrosoftIISv2")
			Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name = 'iisadmin'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
			If Err.Number = 0 Then
				blnInstalled = False
				blnRunning = False
				For Each objItem In colItems
					blnInstalled = True
					If objItem.State = "Running" Then blnRunning = True
				Next
				WScript.Echo "IIS Installed: " & blnInstalled
				WScript.Echo "IIS Running: " & blnRunning
				If blnInstalled = True Then
					WScript.Echo vbCrLf & "Web Sites on " & strServer & ":"
					On Error Resume Next
					Set colItems = objWMIService.ExecQuery("SELECT * FROM IIsWebVirtualDir", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
					For Each objItem In colItems
						Set nodeObj = objWMIService.Get("IIsWebVirtualDirSetting='" & objItem.Name & "'") 
						WScript.Echo nodeObj.AppFriendlyName
					Next
					WScript.Echo VbCrLf
					Err.Clear
					On Error GoTo 0
				End If
			Else
				WScript.Echo "Error connecting to the IIS provider on " & strServer & ". Error " & Err.Number & ": " & Err.Description
				Err.Clear
				On Error GoTo 0
			End If
		Else
			WScript.Echo strServer & " is offline."
		End If
	End If
Wend
objFile.Close
WScript.Echo vbCrLf & "Finished."

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Hi Rob,

Thanks a lot. The script is running absolutely fine in the local host.  But getting access denied when it runs for the other servers it takes from the servers.txt. Do you have any idea on it?

Enclosed is the screenshot: User generated image
Try changing this:
                  Set objWMIService = objLocator.ConnectServer(strServer, "root/MicrosoftIISv2")

to this
                  Set objWMIService = objLocator.ConnectServer(strServer, "root/MicrosoftIISv2", strServer & "\Administrator", "LocalAdminPassword")

so that you connect using credentials local to each remote computer.  Hopefully your local admin password is the same across the servers, so it will work against them all.

Regards,

Rob.
Hi Rob,

I am using domain account. so how can I pass my domain credentials into the script. Thanks
I'll have to check the WMI Security on the MicrosoftIISv2 namespace when I get back to work tomorrow, but I have a feeling the default security will be limited to local accounts only, since I already tried it with domain credentials and it didn't appear to work.

I'll get back to you.

Regards,

Rob.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
good