Determine if computer is a server.

This script can be used to determine if a computer is a server. The script pings the computter to ensure there is a network connection possible. If the ping returns OK then a WMI query runs to determine if the computer is a server. If the computer is a server, the ip-address is written to the screen.

This script uses a component from http://www.netal.com/ssr.htm to ping servers on the network and do a dnslookup to get the IP address.


On Error Resume next
'check if computer is a Server 
Dim objIP
  
'create IPnetwork object
Set objIP = CreateObject("SScripting.IPNetwork")
Errorhandling 
'***************************************************************************
'** Get names from the specified domain
arrComputers = GetPcNames("DomainName")                                                                                               
'***************************************************************************
'** Loop through the computernames                                                                                                                                  
For each strComputerName in arrComputers 
'***************************************************************************
'** If the computer responds to a ping the determine if it is a server                                                                                                
 If objIP.Ping(strComputerName) = 0  Then 
    If IsServer(strComputername) Then
'***************************************************************************
'** Computer is a server, output the IP address
       Wscript.echo strComputername & " is Server!"
       Wscript.echo "IP Address = " & objIP.DNSLookup( strcomputername )
      Errorhandling
    End If  
 End If  
Next     
wscript.quit(0)
'***************************************************************************
'** Function:			GetPcNames
'** Version:		1.0
'** Created:		09-04-2003 11:44
'** Author:			Adriaan Westra
'** E-mail:			
'**
'** Purpose / Comments:
'**
'**		Get all netbiosnames from the domain.
'**
'** Change Log :
'**
'** 09-04-2003 11:49 : Initial Version
'**
'** Arguments :  
'**
'**	strDomain		Name of your nt or windows 2000 domain
'**
'** Returns	:
'**
'**	Array with netbiosnames
'**			
'***************************************************************************
Function GetPcNames(strDomain)
On Error Resume Next
	
	Set objDomain = GetObject("WinNT://" + strDomain)
	objDomain.Filter = Array("Computer")
	
   Dim PcNames()
   I = 1
   For each objcomputer in objDomain
   	 If I = 1 Then	
   	    ReDim PRESERVE PcNames(1)
   	 Else
          ReDim PRESERVE PcNames(UBound(PcNames) + 1)
       End if
       PcNames(UBound(PcNames) - 1) = objComputer.Name
       i = i + 1
   Next
   ReDim PRESERVE PcNames(UBound(PcNames) - 1)
	GetPcNames = pcnames
End Function

Sub ErrorHandling()
	Select Case err.number
		Case 0
			Exit Sub
		Case 429
		   Wscript.Echo "SScrRun.dll is not registered, script is aborting!"
	   	err.clear
	   	Wscript.Quit(1)
		Case 	-2147467259
			wscript.echo "Database is gelocked"
			wscript.quit(1)
		Case else
	   	wscript.echo err.number & " " & err.description
	   	err.clear
	  End select
End Sub
'***************************************************************************
'** Function:		IsServer
'** Version:		1.0
'** Created:		1/18/2009 1:57PM
'** Author:			Adriaan Westra
'** E-mail:			
'**
'** Purpose / Comments:
'**
'**		Determine if a computer is a server
'**
'** Change Log :
'**
'** 1/18/2009 1:58PM : Initial Version
'**
'** Arguments :  
'**
'**	strComputername	Name of the pc to check
'**
'** Returns	:
'**
'**	True or False
'**			
'***************************************************************************
Function IsServer(strComputername)
   On Error Resume Next
   Dim objWMI, objSSystem, objRole
   Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputername)
   'get an instance of the Win32_ComputerSystem for the  computer
   Set objSystem = objWMI.Get("Win32_ComputerSystem='" & strComputerName & "'")
   
   bServer = False
   
   'check if Roles property is Empty array
   If Not IsNull(objSystem.roles) Then
      For Each objRole In objSystem.Roles
      	 If objRole = "Server_NT" Then
       		 bServer = True
       	 End If
      Next
   End If
   IsServer = bServer
End Function