[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Looking for a script

Posted on 2011-10-20
21
Medium Priority
?
249 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:jmohan0302
  • 11
  • 9
21 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 37002719
Do you mean your own servers on your own network?
0
 

Author Comment

by:jmohan0302
ID: 37002731
yes
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37003525
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jmohan0302
ID: 37004239
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
0
 

Author Comment

by:jmohan0302
ID: 37006536
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. Error while running the script
Kindly help me in getting this script run. Thanks
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37013427
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.
0
 

Author Comment

by:jmohan0302
ID: 37014519
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?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37015194
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

0
 

Author Comment

by:jmohan0302
ID: 37022472
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.
0
 

Author Comment

by:jmohan0302
ID: 37022479
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:

 Output Output
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37022542
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

0
 

Author Comment

by:jmohan0302
ID: 37039529
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:

 Output got from above script
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37042446
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

0
 

Author Comment

by:jmohan0302
ID: 37042499
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37042678
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

0
 

Author Comment

by:jmohan0302
ID: 37048425
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: Output
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37049377
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.
0
 

Author Comment

by:jmohan0302
ID: 37064309
Hi Rob,

I am using domain account. so how can I pass my domain credentials into the script. Thanks
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37069130
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.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 37080748
I had an error in the script, rather than there being permissions problems.  Try this, and change the "domain\username" and "password" strings in both places where it connects to the WMI classes.

Regards,

Rob.
strServers = "servers.txt"

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const WbemAuthenticationLevelPktPrivacy = 6
Const wbemImpersonationLevelImpersonate = 3
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 objWMIService1 = objLocator.ConnectServer(strServer, "root/cimv2", "domain\username", "password")
			objWMIService1.Security_.AuthenticationLevel = wbemAuthenticationLevelPkt
			objWMIService1.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
			Set objWMIService2 = objLocator.ConnectServer(strServer, "root/MicrosoftIISv2", "domain\username", "password")
			objWMIService2.Security_.AuthenticationLevel = wbemAuthenticationLevelPkt
			objWMIService2.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
			If Err.Number = 0 Then
				Set colItems = objWMIService1.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 = objWMIService2.ExecQuery("SELECT * FROM IIsWebVirtualDir", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
						For Each objItem In colItems
							Set nodeObj = objWMIService2.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 "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

0
 

Author Closing Comment

by:jmohan0302
ID: 37244935
good
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question