Solved

Run vbscript to change configs on all nodes in nagios network

Posted on 2009-05-15
5
542 Views
Last Modified: 2012-06-22
I have about 12 windows servers that I monitor w/ nagios.  I manage all the configs and scripts from my laptop.  As of right now I have to go to each server and run my config update script.  What I want to do is to write a script that I can run on my laptop that will update all the windows server configs simultaneously.  I also want the script to be able to add a line of configuration, but when script writes to the file, it clears the whole file and only adds the line I want to add.  I started work on one but it only looks for the files on my computer.  I am including the code that I have already.
Option Explicit 
Dim file, objFile, objFSO
Dim objShell
Dim command, objExecObject
Dim aMachines, strMachines, machine
 
strMachines = "IP addresses separated by comma w/ no spaces"
aMachines = Split(strMachines, ",")
 
For Each machine In aMachines
	command = "cmd /c xcopy ""P:\Departments\Information Technology\Scripts\chk_syslog_ntfs.vbs"" c:\progra~1\NSClient++\"	
	Set objShell = CreateObject("Wscript.Shell")
	Set objExecObject = objShell.Exec(command)
	
	file = "C:\Program Files\NSClient++\NSC.ini"
	Const ForAppending = 2
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(file, ForAppending)
	objFile.WriteLine("check_syslog_ntfs=chk_syslog_ntfs.vbs")
Next
 
If Err.number = vbEmpty Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.run ("Explorer" &" " & file & "\" )
Else WScript.echo "VBScript Error: " & Err.number
End If
 
WScript.Quit

Open in new window

0
Comment
Question by:jleleux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Assisted Solution

by:internetsavant
internetsavant earned 100 total points
ID: 24397144
well to start out.  ForWriting is 2, ForAppending is 8 so the reason your file is overwriting instead of appending is because you're telling it to.  Example below:

http://www.computerperformance.co.uk/vbscript/vbscript_file_opentextfile.htm


Second, you need to specify the UNC path of your hosts in your loop:

file = "\\" & machine & "C$\Program Files\NSClient++\NSC.ini"

http://msdn.microsoft.com/en-us/library/aa711216(VS.71).aspx


Third, 'that will update all the windows server configs simultaneously" is not correct -- via scripting unless you made it thread somehow (don't know if that's possible in VBS), it will happen sequentially in your loop.



0
 
LVL 17

Accepted Solution

by:
Jared Luker earned 400 total points
ID: 24397285
See if this works for you:
Option Explicit 
Dim file, objFile, objFSO
Dim objShell
Dim command, objExecObject
Dim aMachines, strMachines, machine
 
Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
strMachines = "IP addresses separated by comma w/ no spaces"
aMachines = Split(strMachines, ",")
 
For Each machine In aMachines
	objFSO.CopyFile "P:\Departments\Information Technology\Scripts\chk_syslog_ntfs.vbs","\\" & machine & "\c$\progra~1\NSClient++\"	
 
	file = "C:\Program Files\NSClient++\NSC.ini"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(file, ForAppending)
	objFile.WriteLine("check_syslog_ntfs=chk_syslog_ntfs.vbs")
Next
 
If Err.number = vbEmpty Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.run ("Explorer" &" " & file & "\" )
Else WScript.echo "VBScript Error: " & Err.number
End If
 
WScript.Quit

Open in new window

0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24397295
oops... you'll need to dim objFSO since you have Option Explicit turned on
0
 
LVL 1

Author Comment

by:jleleux
ID: 24398045
Ok got a new error, permission denied.  Can set it up to use certain user credentials?
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24398500
It's probably easier just to run it from a command window with elevated credentials

runas /user:domain\username cmd.exe
0

Featured Post

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!

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

739 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