'Script to Remote Exec files in list of servers from (computers.txt) file 
'Copy NRPE_NT folder to Remotec, cp/execute nagios.bat on remote(install Nagios -i, start Nagios.)
'Author: Felipe Ferreira
'Version: 3.1
'Date: 08/07/2008 updated 28/10/08

'Todo:
'1. Complete quite uninstall(.bat), OK
'2. Get password from Input, - OK
'3. Better Timing, if still running dont stop...? while cmd.exe ? with psexec, wait for it to finish..

'Option explicit

Dim objTSIn,strComputer,strPassword,domain,hostsfile,objPassword
Dim mapcmd,cpcmdf,cpcmdf2
Dim verbose : verbose = 0  
Dim strScriptFile : strScriptFile = WScript.ScriptFullname
Dim sPath : sPath = Left(strScriptFile, Len(strScriptFile) - Len(WScript.Scriptname)) 'PATH OF SCRIPT LOCATION
Dim objFSO : Set objFSO = CreateObject("Scripting.FilesyStemObject")  


'@@@@@@@@@@@@@@@@@@@@@@ EDIT HERE @@@@@@@@@@@@@@@@@@@@@@@@@@
verbose = 1  						       'Display Outputs
hostsfile = spath & "computers.txt"			       'computers.txt should have hostanmes, or IPs (one per line)
'Attention in the domain name, I use a dynamic way to get it mine...

'@@@@@@@@@@@@@@@@@@@@@@ MAIN @@@@@@@@@@@@@@@@@@@@@@@@@@
'Prompt for password and store in encrpyt vbs way

Set objPassword = CreateObject("ScriptPW.Password") 
WScript.StdOut.Write "Please enter your password:" 
strPassword = objPassword.GetPassword() 


'get hosts from file
Set objTSIn = objFSO.OpenTextFile(hostsfile)

Do Until objTSIn.AtEndOfStream
    'Deploy script to remote computer
    strComputer = objTSIn.ReadLine
    pt strComputer & ": " & Now
    execRemote(strComputer)	
    pt strComputer & " Completed at " & Now
    pt ""
    'Set WshShell = Nothing
Loop
objTSIn.Close
pt "Script done."
pt "_______________________________________"

Function execRemote(strcomputerp)
	Dim WshShell,strSCommandPre,strSCommand
	Set WshShell = CreateObject("WScript.Shell")
'Get ComputerName last 3 digits and sets DOMAIN (
	domain = "DIBAJ" & right(strcomputerp,3)	
	WshShell.Run "net use O: /delete /yes" 
	mapcmd = "net use o: \\"& strcomputerp & "\c$ /user:" &domain& "\administrator "& strPassword		
	pt "_________________________________________________"
	pt "Mapping Remote o: Drive in " & strcomputerp
	pt mapcmd	
	wscript.sleep 300	
	WshShell.Run mapcmd
	wscript.sleep 6000
	WshShell.Run "dir o:" 
'NRPE FILES, COPY ALL, AND EXECUTE NRPE_NT.EXE
	pt "_________________________________________________"
    	pt "Creating NRPE folders into " & strcomputerp & " c:\nrpe_nt"
	cpcmdf = "cmd /C xcopy /T /C /Y /Q " & spath & "nrpe_nt\*.*  o:\nrpe_nt\"
	cpcmdf2 = "cmd /C xcopy /E /C /Y " & spath & "nrpe_nt\*.*  o:\nrpe_nt\"  
	pt cpcmdf
	WshShell.Run cpcmdf
wscript.sleep 500	
	pt "_________________________________________________"
	pt "Copying NRPE " & strcomputerp & " c:\nrpe_nt"
        pt cpcmdf2
	WshShell.Run cpcmdf2
'ESPERA 3min
	wscript.sleep 95000

'EJECUTA nagios.bat (Instala Nagios y Quita los Agente NSM)	
	strSCommand = "-u " & domain &"\administrator -p " & strPassword & " cmd.exe /C c:\nrpe_nt\nagios.bat"
	pt "_________________________________________________"
	pt "Running Script... Remotely at " & strComputerp  	
	pt spath &"psexec.exe \\" & strComputerp & " " & strSCommand	
	WshShell.run  spath &"psexec.exe \\" & strComputerp & " " & strSCommand	

'Esperar 5+ minutos
	wscript.sleep 170000
	pt "_________________________________________________"
	pt "Disconnect the Remote Disk of: " & strComputerp  	 	
	pt "net use o: /delete /yes"
	WshShell.run "net use O: /delete /yes"
	'WshShell.Run "kill psexec.exe"	
end Function

Function pt(msg)
'PRINTS OUT TO PROMPT
on error resume next
	if verbose = 1 then
	 wscript.echo msg
	end if
end function
