WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Network

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Find a Computer in a Specified Domain Running a Service

Keywords:   service

This script will find any computer in the specified domain which is running the service a user specifies as input.

For example, you can select domain a.c.com, and specify services Spooler, or any combination of leading/ trailing splats, like *ooler or spool* or *ool* to find anyone running services which contain those characters.

More detailed instructions for running the script are in the comments section at the beginning of the script.

;I would be interested in feedback from
;anyone who takes this and runs it...
;vzp20@amdahl.com 

; 
;  Service Running On - Report what system(s) a specified
;    service is running on, and the state of that service.
;    These systems in the domain reports will be scanned:

;                          The pdc, wntServerList, type 8
;         NT workstation or server, wntServerList, type 4096   
;     BDCs and non-pdc/bdc servers, wntServerList type  32768
;    
;   Before running this, change the pop server (look for string
;    pop.amdahl.com) to your local mailserver. if you don't do 
;    this, you can still mail reports, but will be queried for
;    a local mail server at the time you mail a report.
;
;   To run this program:
;    *  Bring up the menu and specify the system or domain
;    *  Enter the first service you want to check, hit OK
;    *  Enter the next service you want checked, hit OK
;         Services can have wildcards as first or last character,
;         or both first and last character. So, entering strings
;         like: *ooler   spoole*   *ool*     would all cause the
;         spooler to be listed.
;    *  To start the run, leave the services box blank and hit OK
;    *  You will be asked to specify an output report location then
;         the run will start.
;    *  At completion, click 'edit last output report created' to
;         view the report. You can edit remarks onto the top of
;         the output file.
;    *  Exit the edit session
;    *  If you want to mail the report to someone, click on
;        'mail current report to userid' and fill in a userid.
;        (see note above about the pop server)
;
;    caveats/warnings:
;      * This will run on Windows NT systems only
;      * Depending on how many systems are being checked, it
;         may run awhile, so an interrupt is coded in.
;      * This works perfectly for fully-qualified services,
;         However, for 'wilcard' service names, it has to go
;         through the registry services area to check for
;         services that match the partial string you entered.
;
;         Many services seem to be in the services applet but
;         not in the services area of the registry, and these
;         will not be found. Conversely, some services are in
;         the registry but not in the services applet, and 
;         these are noted as such in the output report.
;
;
AddExtender("wwwnt32i.dll")
AddExtender("wwsop32i.dll")
AddExtender("wwwsk32i.dll")

gosub startup

:editdisp
; Define the dialog format
EditFormat=`WWWDLGED,5.0`
EditCaption=`Script ID: System Information`
EditX=220
EditY=05
EditWidth=180
EditHeight=190
EditNumControls=27
Edit01=`070,002,090,DEFAULT,STATICTEXT,DEFAULT,"Services Report"`
Edit02=`045,009,130,DEFAULT,STATICTEXT,DEFAULT,"Produce a report listing which system(s) "`
Edit03=`045,016,130,DEFAULT,STATICTEXT,DEFAULT,"the specified service(s) are running on"`
Edit04=`020,028,165,DEFAULT,STATICTEXT,DEFAULT,"========== Select a system or domain to scan =========="`
Edit05=`015,034,050,DEFAULT,RADIOBUTTON,reqtype,"Current System:",1`
Edit06=`067,036,090,DEFAULT,STATICTEXT,DEFAULT,"%sysname%"`
Edit07=`015,044,090,DEFAULT,RADIOBUTTON,reqtype,"Specify one system by name:",2`
Edit08=`105,044,065,DEFAULT,EDITBOX,onesys,""`
Edit09=`015,056,090,DEFAULT,RADIOBUTTON,reqtype,"Specify one system by ip address:",3`
Edit10=`105,056,065,DEFAULT,EDITBOX,oneipaddr,""`
Edit11=`015,069,050,DEFAULT,RADIOBUTTON,reqtype,"Current Domain:",4`
Edit12=`067,070,090,DEFAULT,STATICTEXT,DEFAULT,"%domname1%"`
Edit13=`015,078,090,DEFAULT,RADIOBUTTON,reqtype,"Specify one domain by name:",5`
Edit14=`105,078,065,DEFAULT,EDITBOX,onedom,""`
Edit15=`020,090,165,DEFAULT,STATICTEXT,DEFAULT,"======== Specify a service, hit ok to start run ========"`
Edit16=`040,099,075,DEFAULT,EDITBOX,srvname,""`
Edit17=`020,112,165,DEFAULT,STATICTEXT,DEFAULT,"========= Status of request and output report ========="`
Edit18=`015,119,210,DEFAULT,STATICTEXT,DEFAULT,"%reportstatus%"`
Edit19=`015,127,210,DEFAULT,STATICTEXT,DEFAULT,"%filestatus%"`
Edit20=`015,133,120,DEFAULT,RADIOBUTTON,rptaction,"Append to last output report created (default)",1`
Edit21=`015,142,100,DEFAULT,RADIOBUTTON,rptaction,"Edit last output report created",2`
Edit22=`015,152,085,DEFAULT,RADIOBUTTON,rptaction,"Delete last output report created",3`
Edit23=`015,161,090,DEFAULT,RADIOBUTTON,rptaction,"Mail current report to userid:",4`
Edit24=`105,161,065,DEFAULT,EDITBOX,mailid,""`
Edit25=`010,173,052,DEFAULT,PUSHBUTTON,DEFAULT,"&Cancel",0`
Edit26=`071,173,025,DEFAULT,PUSHBUTTON,DEFAULT,"OK",5`
Edit27=`105,173,052,DEFAULT,PUSHBUTTON,DEFAULT,"Reset",100`

while @TRUE
    retval = Dialog("Edit")
    if retval == "100" 
      gosub resetbuttons
    endif

    if rptaction == "3" then goto  filedelete
    if rptaction == "2" then goto  fileedit  
    if rptaction == "1" && reqtype != "99" then gosub fileappend
    if rptaction == "4" && reqtype == "99" then gosub filemail


    rptdate  = TimeDate()          ; so date is as accurate as possible on each rpt

    if reqtype == "99" && StrLen(servicelist) == "0" 
      reportstatus = "No system or domain specified"
      goto editdisp
    endif

    if srvname == "" && StrLen(servicelist) == "0"
      reportstatus = "No service specified, enter a service or cancel"
      goto editdisp
    endif


    if  StrLen(srvname) != "0"
      servicelist = StrCat(servicelist,srvname,@tab)
      reportstatus = "Service %srvname% added to list of service(s) to check"
      srvname = ""
      goto editdisp
    endif

    numservices = ItemCount(servicelist,@tab)

    if reqtype   == "1"  then goto currsys1
    if reqtype   == "2"  then goto onesysbyname
    if reqtype   == "3"  then goto onesysbyip
    if reqtype   == "4" || reqtype == "5" then goto rpt1dom || tsonly == "1"
    reportstatus = "No report type selected. Make a selection then hit enter"   
    gosub resetbuttons

endwhile    

:cancel
    if IsDefined(output) 
      FileClose(output)
    endif
    exit

 ; ===========================================================================
:currsys1

   rptsys = sysname
   if !IsDefined(output) then gosub filenew    
   currsys = sysname
   gosub writeheader

:rpt1     
   for zz = 1 to numservices
     currsrv = ItemExtract(zz,servicelist,@tab)

     servicelen = StrLen(currsrv)
     firstchar  = StrSub(currsrv,1,1)            ; first character
     lastchar   = StrSub(currsrv,servicelen,1)   ; last  character
     if firstchar != "*" && lastchar != "*" then goto postsplat
     if firstchar == "*" 
       currsrv1 = StrSub(currsrv,2,(servicelen - 1) )                   
       currsrv  = currsrv1
       servicelen = StrLen(currsrv)
     endif
     if lastchar == "*" 
       currsrv1 = StrSub(currsrv,1,(servicelen - 1) )
       currsrv  = currsrv1
       servicelen = StrLen(currsrv)  
     endif   

     regtouse = RegConnect("%sysname%", @REGMACHINE)
     errormode(@off)
     srv1 = RegOpenkey(%regtouse%, "SYSTEM\CurrentControlSet\Services\")  
     errormode(@on)
     if srv1 == "0"
       FileWrite(output,"Services for system %sysname% could not be obtained")  
       goto postservices
     endif
     regsrvlist = RegQueryKeys(srv1)
     RegClosekey(srv1)
     numregsvcs = ItemCount(regsrvlist,@tab)
     for xy = 1 to numregsvcs
:trynextone
       currsrv2 = ItemExtract(xy,regsrvlist,@tab)
       match = StrIndexNc(currsrv2,currsrv,1,@FWDSCAN)   
       if match != "0" then goto srvmatch   ; if get a match at this
                                            ; level don't bother
                                            ; looking for display name
;
;   add code for extra level to DisplayName
;
      dispkey   = RegOpenKey(%regtouse%,"SYSTEM\CurrentControlSet\Services\%currsrv2%")
      errormode(@off)
      dispname  = RegQueryEx(dispkey,"[DisplayName]",@tab,1)
      errormode(@on)
      if dispname == "0" then goto postsvcchk
;
       match = StrIndexNc(dispname,currsrv,1,@FWDSCAN)    
       if match == "0" then goto postsvcchk

:srvmatch
       currsrvt = currsrv            ;  save original wildcards string
       currsrv = currsrv2            ;  set service to full name from reg
       currsys2 = StrFix("%currsys%"," ",15)    
       line1 = " %currsys2%   *****             %currsrv2%"       
       gosub chkservice              ;  and go check it
       currsrv = currsrvt            ;  set back to original name to
                                     ;  use for subsequent compares
       drop(line1)
     next   ; next to go with:  for xy = 1 to numregsvcs
     ;

:postsplat
     gosub chkservice
:postsvcchk
   next  ; next to go with zz =  1 to numservices
:postservices
   if fromdom == "1"  then goto nextsys

   reportstatus = "Report complete"
   gosub resetbuttons


; ============================================================================
; check to make sure the system name is valid, then go to one-system reporting
;
:onesysbyname
   if StrLen(onesys) == "0"
     reportstatus = "System name must be specified
     goto editdisp
   endif
   rptsys = onesys
   if !IsDefined(output) then gosub filenew    

   errormode(@off)
   ipaddr = wxHost2Addr(onesys)
   errormode(@on)

   if StrLen(ipaddr) == "0"
     reportstatus = "IP address for: %onesys% could not be obtained."
     goto editdisp
   endif

   currsys = onesys
   gosub writeheader  

   goto rpt1


; ============================================================================
; check to make sure the ip address is valid, then go to one-system reporting
;
:onesysbyip
   if StrLen(oneipaddr) == "0"
     reportstatus = "IP address must be specified"
     goto editdisp
   endif
   
   errormode(@off)
   host = wxAddr2Host(oneipaddr)
   errormode(@on)
  
:trybyip

   if StrLen(host) == "0"
     rptsys = oneipaddr
   else
     rptsys = host
   endif

   if !IsDefined(output) then gosub filenew  
   currsys = host
   gosub writeheader  
   goto rpt1



; ===========================================================================
; for each pdc, bdc, and server server, set up for 
;  going to detail report
;
:pdcbdc  
      q = 0 
:nexttype2
      q = q + 1
      if q > pnumtypes   then goto postpdc
     currtype = ItemExtract(q,psystypes,@TAB)
     currtext = ItemExtract(q,psystext,@TAB)
     servers1 = wntServerList("%dompdc%","%domname%",%currtype%) 
      servers = ItemSort(servers1,@TAB)
     srvrcount = ItemCount(servers,@TAB) 

      m = 0 
:nextsys2
         m = m + 1
         if m > srvrcount then goto nexttype2	 
         currsys = ItemExtract(m,servers,@tab)        
         currsyslen = StrLen(currsys)
         ipquerysys = StrSub(currsys,3,(currsyslen - 2))
         ipaddr = wxHost2Addr(ipquerysys)
         astatusbar(1,"Domain %domname%" ,"System %currsys%, %crlf%%m% of %srvrcount%, Hold down shift key to interrupt",%srvrcount%,%m%)

        if IsKeyDown(@SHIFT)
           Message("%SN%","Listing interrupted by user")
           FileWrite(output," %crlf%     Listing not complete - interrupted by user")
           astatusbar(2,"Domain %domname%" ,"Processing interrupted",%srvrcount%,%m%)          
           goto endrtn
         endif
         errormode(@off)
         regtouse = RegConnect("%currsys%", @REGMACHINE)
         errormode(@on)

         if regtouse == ""
           FileWrite(output,equals)
           FileWrite(output,"Could not connect to registry on system %currsys%")
           FileWrite(output,equals)
           goto nextsys2
         endif
         currsys2 = StrSub(currsys,3,StrLen(currsys))
         rptsys = currsys2
         goto rpt1
:backtopdc
         goto nextsys2

:postpdc
         astatusbar(2,"Domain %domname%" ,"System %currsys%, %crlf%%m% of %srvrcount%, Hold down shift key to interrupt",%srvrcount%,%m%)
         reportstatus = "Report complete"
         gosub resetbuttons

; ===========================================================================
; for each terminal server, create a detail report 
;
:tslistonly  

   tslist = ""
   for wd2 = 1 to numwkstns     
       astatusbar(1,"Domain %domname%" ,"Processing Terminal Servers, %wd2% of %numwkstns%, Shift key to interrupt",%numwkstns%,%wd2%)       
       wdsys = ItemExtract(wd2,wkstns,@tab)
       errormode(@off)
       isitts   = wntSvcStatus("%wdsys%","Terminal Server", 0, 2)	       
       errormode(@on) 
       if isitts   != "0" then tslist = StrCat(tslist,wdsys,@tab)
   next  
   numts = ItemCount(tslist,@tab)

     astatusbar(1,"Domain %domname%" ,"Now processing Terminal Servers",100,10)
     m = 0 
:nextsys3
         m = m + 1
         if m > numts then goto postts1	 
         currsys = ItemExtract(m,tslist,@tab)        
         currsyslen = StrLen(currsys)
         ipquerysys = StrSub(currsys,3,(currsyslen - 2))
         ipaddr = wxHost2Addr(ipquerysys)
         astatusbar(1,"Domain %domname%" ,"System %currsys%, %crlf%%m% of %numts%, Hold down shift key to interrupt",%numts%,%m%)

        if IsKeyDown(@SHIFT)
           Message("%SN%","Listing interrupted by user")
           FileWrite(output," %crlf%     Listing not complete - interrupted by user")
           astatusbar(2,"Domain %domname%" ,"Processing interrupted",%numts%,%m%)          
           goto endrtn
         endif
         errormode(@off)
         regtouse = RegConnect("%currsys%", @REGMACHINE)
         errormode(@on)

         if regtouse == ""
           FileWrite(output,equals)
           FileWrite(output,"Could not connect to registry on system %currsys%")
           FileWrite(output,equals)
           goto nextsys3
         endif
         currsys2 = StrSub(currsys,3,StrLen(currsys))
         rptsys = currsys2
         goto rpt1
:backtots
         goto nextsys3

:postts1
         astatusbar(2,"Domain %domname%" ,"System %currsys%, %crlf%%m% of %numts%, Hold down shift key to interrupt",%numts%,%m%)
         reportstatus = "Report complete"
         gosub resetbuttons

; ===========================================================================
; check to make sure domain is valid, then pull information
:rpt1dom
systypes = ""
systext  = ""

   if reqtype == "4" then domname = Environment("USERDOMAIN")
   if reqtype == "5"  
     if StrLen(onedom) == "0"
       reportstatus = "Domain name must be specified"
       goto editdisp
     endif
     domname = onedom
   endif  

   rptsys = domname
   if !IsDefined(output) then gosub filenew  
   currsys = domname
   gosub writeheader  

   dompdc  = wntGetDc("","%domname%",1)

   astatusbar(0,"Processing..","Starting services check list..","10",0)
  
    systypes = StrCat(systypes,"8",@TAB)       ;  pdc 
    systext  = StrCat(systext," Primary Domain Controller",@TAB)
        pdcs = wntServerList("%dompdc%","%domname%",8) 
 
    systypes = StrCat(systypes,"4096",@TAB)    ; nt wkstn or server 


    systypes = StrCat(systypes,"32768",@TAB)   ;  bdc and server/servers  
    systext  = StrCat(systext," Non-pdc/bdc Server(s)",@TAB) 
   srvrsrvrs = wntServerList("%dompdc%","%domname%",32768) 
numsrvrsrvrs = ItemCount(srvrsrvrs,@tab) 

   errormode(@off)
   dompdc  = wntGetDc("","%domname%",1)
   errormode(@on)
   if dompdc == "0"
      reportstatus = "PDC for domain %domname% could not be located"
      Message("SystemInfo","Domain pdc could not be located, run terminating. %crlf% 'SystemInfo' cannot be run while logged on locally.")
      astatusbar(2,"Domain %domname%" ,"%currtext%%crlf%%x% of %srvrcount%, Hold down shift key to interrupt",%srvrcount%,%x%)
      gosub resetbuttons
   endif

     fromdom = "1"
     numtypes = ItemCount(systypes,@tab)
:ctlrinfo
     For i = 1 to numtypes  
       currtype = ItemExtract(i,systypes,@TAB)
       servers = wntServerList("%dompdc%","%domname%",%currtype%) 
       srvrcount = ItemCount(servers,@TAB) 
       for x = 1 to srvrcount	 
         currsys = ItemExtract(x,servers,@tab)

         astatusbar(1,"Domain %domname%" ,"%currtext%%crlf%%x% of %srvrcount%, Hold down shift key to interrupt",%srvrcount%,%x%)
         if IsKeyDown(@SHIFT)
           Message("%SN%","Listing interrupted by user")
           FileWrite(output," %crlf%     Listing not complete - interrupted by user")
           astatusbar(2,"Domain %domname%" ,"Processing interrupted",10,5)     
           goto endrtn
         endif
         goto rpt1

:nextsys
      next                   ; srvrcount
   next                      ; numtypes

:endrtn

:endrtn2
   astatusbar(2,"Domain %domname%" ,"Processing complete",10,5)          
   reportstatus = "Report complete"
   gosub resetbuttons

; ===========================================================================
;  setup work
;  find this script's place in the universe
; 
:startup
mailsrvr     = "pop.amdahl.com"              ; mail server for mailing reports
pdc          = wntGetDc("","",1)             ; obtain primary domain controller
sysinfo      = WinSysInfo()                  ; pull current system info                    
sysname      = ItemExtract(1,sysinfo,@tab)   ; get current system name  
ipaddr       = wxHost2Addr(sysname)          ; get current system ip address 
domname1     = Environment("USERDOMAIN")     ; get current domain 
cr           = Num2Char(13)
lf           = Num2Char(10)
crlf         = StrCat(cr,lf)
equals       = StrFill("=",80)
equalsshort  = StrFill("=",70)
SN           = "Summary Information Report"
servicelist  = ""

        
; set initial variables
reportstatus = ""
filestatus   = ""

;
; reset the dialog buttons and variables
;
:resetbuttons
fromdom       = ""
pdcbdconly    = "99"                  ; pdc/bdc/server detail rpt
reqtype       = "99"                  ; report request type
rptaction     = "99"                  ; report action 
sortby        = "2"

tsonly        = "99"                  ; terminal servers only
onedom        = ""
onesys        = ""
oneipaddr     = ""
mailid        = ""
servicelist   = ""
srvname       = ""
goto editdisp


; ============================================================================
;  subroutines 
;
:filedelete
  ;
  ;  if user wants to delete 
  ;    
  if !IsDefined(output) 
    reportstatus = "No output report to delete"
    gosub resetbuttons
  else
    FileClose(output)
    FileDelete("%fn%") 
    filestatus = "File %fn% deleted"  
    reportstatus = "No current output report" 
    drop(output,fn)  
    gosub resetbuttons   
  endif            

  ;
  ;  if user wants to edit 
  ; 
:fileedit  
  if !IsDefined(output)    
    reportstatus = "No output report to edit"
    gosub resetbuttons
  else
    FileClose(output)
    Runwait("notepad.exe","%fn%")
    output = FileOpen("%fn%","APPEND")
    gosub resetbuttons
  endif

  ;
  ;  if user wants to append
  ;  
:fileappend 
  if  !IsDefined(output)
    filestatus = "No file available to append to"
    gosub resetbuttons
  endif
  return

  ;
  ;  if user must define an output file
  ;  
:filenew    
  fn = AskFileName("ServicesInfo.%rptsys%.txt","c:\temp","AllFiles|*.txt","ServicesInfo.%rptsys%.txt",0)    
  output = FileOpen("%fn%","WRITE")
  filestatus = "Output file: %fn%"
  return

  ;
  ;  if user wants to mail the file
  ;
:filemail
  ;
  ; if there is no current output report, 
  ;  put out appropriate message and return
  ;
  if !IsDefined(output)
    filestatus = "No file available to mail"
    gogo postsendmail
  endif
  ;
  ; see if the mail server is available. if not, ask 
  ;  if user wants to specify an alternate server of
  ;  abandon the mailing.
  ;
  chkmailsrvr = wxPing("%mailsrvr%")
  if chkmailsrvr == @FALSE
    tryagain = AskYesNo("%SN%","Mail server %mailsrvr% not accessible.%crlf% Specify another mail server or abandon mailing?")
    if tryagain == @yes
      mailsrvr = AskLine("%SN%","Enter name of alternate mail server","")
      goto filemail
    else
      goto postsendmail
    endif
  endif
  ;
  ; check to make sure a mailid was specified. 
  ;  if not, message and return
  if mailid == ""
    filestatus = "Id to mail file to must be specified"
    goto postsendmail
  endif
   
  FileClose(output)
  rtn = SmtpSendFile("%mailsrvr%","ntadmin@amdahl.com","%mailid%","Services Report: %rptsys%",fn)
  if rtn == @TRUE  then filestatus = "File was sent successfully to %mailid%"  
  if rtn == @FALSE then filestatus = "File was not sent successfully to %mailid%"
  output = FileOpen("%fn%","APPEND")

:postsendmail
  gosub resetbuttons
  return

:writeheader
   FileWrite(output,"%crlf%================================================================")
   FileWrite(output,"  Check for specified services running on: %currsys%")
   FileWrite(output,"                      Time\Date of report: %rptdate%")        
   prtsrv = ItemExtract(1,servicelist,@tab)
   FileWrite(output,"                 Services checked will be: %prtsrv%")
   for x = 2 to numservices
     prtsrv = ItemExtract(x,servicelist,@tab)
     FileWrite(output,"                                           %prtsrv%")
   next
   FileWrite(output,"%crlf%             If status is listed as '*****' the service is")
   FileWrite(output,"         in the registry but may not appear in services applet")
   FileWrite(output,"%crlf%   If a service does not exist on a system it will not be listed")
   FileWrite(output,"%crlf% System            Status            Service")
   FileWrite(output," --------------    ----------------- -----------------------------")
 return   

:chkservice
  errormode(@off)
     chksrv = wntSvcStatus("%currsys%","%currsrv%", 0, 2)
     errormode(@on)
     if chksrv == "0" && IsDefined(line1)
       FileWrite(output,line1)
     endif	 
     if chksrv != "0"  
       if chksrv == "1" then status = "stopped"
       if chksrv == "2" then status = "starting"
       if chksrv == "3" then status = "stopping"
       if chksrv == "4" then status = "running"
       if chksrv == "5" then status = "pending continue"
       if chksrv == "6" then status = "pending pause"
       if chksrv == "7" then status = "paused"
       currsys2 = StrFix("%currsys%"," ",15)
       currsrv2 = StrFix("%currsrv%"," ",40) 
       status2  = StrFix("%status%"," ",17)   
       FileWrite(output," %currsys2%   %status2% %currsrv2%") 
     endif
  return


Article ID:   W14497
Filename:   Find Computer Running a Particular Service.txt
File Created: 2000:12:26:14:11:50
Last Updated: 2000:12:26:14:11:50