::=============================Begin Code=================================
::=======================================================================
::
:: NAME: VS2005_query.bat
::
:: AUTHOR: Harley Stagner		 
:: DATE  : 9/3/2006
::
:: COMMENT: This script will run psservice.exe with the find switch to query
::          the network for the "virtual server" service and output the 
::          results to a file called "psservice_results.txt.
:: ========================================================================

psservice find "virtual server" > psservice_results.txt

::=======================================================================
::==============================End Code=================================

Below is a listing of the vs_query.vbs file.
'===============================Begin Code================================
‘========================================================================
'
' NAME: VS2005_query.vbs
'
' AUTHOR: Harley Stagner		 
' DATE  : 9/3/2006
'
' COMMENT: This script will query for Virtual Server Hosts on the network
'          and email the results to one or several chosen administrators. 
'
'========================================================================
'************************************************************************************************************
'***************************Header Section***************************************************************
'************************************************************************************************************
Option Explicit
Dim objFSO, objQueryFile, objShell, objServFile, objResultFile, objRegExp
Dim objMatches, objMatch, objEmail
Dim strService 'Used for reading in the output of the psservice command
Dim strMatch 'Used to match "Found" within the psservice "query.txt" file
Dim strQueryResults 'Used for reading in the contents of "query.txt"
Dim strFinalResults 'Used for reading in the contents of "final_results.txt
Dim queryresults
Dim psservice_results
Dim finalresults

'************************************************************************************************************
'****************************Reference Section***********************************************************
'************************************************************************************************************
'* In this section I define the text files that are used in the script.  I                                               *
'* also define the File System, Text File, Shell, and Regular Expression                                      *
'* objects.  The Regular Expression object is used to extract some text                                       *
'* from the "query.txt" file later.                                                                                                      *
'************************************************************************************************************

queryresults = "query.txt"
psservice_results = "psservice_results.txt"
finalresults = "final_results.txt"

Const forReading = 1
Const forWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objQueryFile = objFSO.OpenTextFile (queryresults, forWriting, True)
	Set objResultFile = objFSO.OpenTextFile (finalresults, forWriting, True)
	
Set objShell = CreateObject("wscript.Shell")

Set objRegExp = New RegExp
	objRegExp.Pattern = "g(.*)F"
	
'************************************************************************************************************
'*****************************Worker Section*************************************************************
'************************************************************************************************************
'* In this section I call the pssservice.bat file and tell the script host                                             *
'* to wait until psservice.bat is finished processing to continue.  Then, I                                       *
'* iterate through the output file,"psservice_results.txt" searching for                                            *
'* the string "Found".  If there is a match, then I write that line to the                                            *
'* "query.txt" file.  The next loop reads the "query.txt" file and takes                                             *
'* computer names from each line (using a Regular Expression search                                       *
'* combined with the Mid and Len functions)and writes them to the                                              *
'* "final_results.txt" file.  Finally, the text files that are no longer                                                    *
'* necessary are closed and deleted and the "final_results.txt" file is                                            *
'* closed.                                                                                                                                        *   
'************************************************************************************************************
	
objShell.Run "VS2005_query.bat", 0, True

Set objServFile = objFSO.OpenTextFile(psservice_results, forReading)

Do Until objServFile.AtEndOfStream

	strService = objServFile.ReadLine

	strMatch = InStr(strService, "Found")

	If strMatch > 0 Then
		objQueryFile.Writeline (strService)
	End If

Loop

Set objQueryFile = objFSO.OpenTextFile (queryresults, forReading)
	objResultFile.Writeline "The following machines are runnning Virtual Server:"_
		& vbCRLF

Do Until objQueryFile.AtEndOfStream 
	
	strQueryResults = objQueryFile.ReadLine

	Set objMatches = objRegExp.Execute(strQueryResults)

	For Each objMatch in objMatches
		objResultFile.Writeline Mid(objMatch.Value, 3, Len(objMatch.Value) -7)
	Next

Loop

objQueryFile.Close
Set objQueryFile = objFSO.GetFile (queryresults)
objQueryFile.Delete()

objServFile.close
Set objServFile = objFSO.GetFile (psservice_results)
objServFile.Delete()

objResultFile.Close

Set objResultFile = objFSO.OpenTextFile (finalresults, forReading)

strFinalResults = objResultFile.ReadAll

objResultFile.Close

'************************************************************************************************************
'*****************************Output Section**************************************************************
'************************************************************************************************************
'* In this section, I use CDO to email the contents of the final results to                                       *
'* the appropriate administrator.  The "final_results.txt" file is still                                                 *
'* available in the same directory that the script ran in as well.                                                     * 
'************************************************************************************************************

Set objEmail = CreateObject("CDO.Message")
		objEmail.From = "vs2005query@somecompany.com"
		objEmail.To = "administrator@somecompany.com"
		objEmail.Subject = "List of Virtual Server Hosts On Your Network!" 
		objEmail.Textbody = strFinalResults
		objEmail.Configuration.Fields.Item _
    		("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		objEmail.Configuration.Fields.Item _
    		("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        		"yoursmtpserver" 
		objEmail.Configuration.Fields.Item _
    		("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		objEmail.Configuration.Fields.Update
		objEmail.Send

‘========================================================================
‘==============================End Code==================================