Advertisement

06.26.2007 at 06:31AM PDT, ID: 22658189
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

vbscript to replace text in a file

Asked by bobs6140 in VB Script, Microsoft Windows Operating Systems

Tags: , , ,

I am trying to update a file on a bunch of systems with a vbscript, the script finds the severs but it deletes the contents of the file, I read in the file, close it then write to it but it appears the write is not happening.

'====================
'
' Type:      VBScript
'
' NAME:      TSMFix.vbs
'
' AUTHOR:    Bob Smith
'
' PURPOSE:   This script will parse a list of servers, and modify the TSM
'            config. It requires servers.txt in the same folder as this script.
'            Servers.txt should contain one machine name per line.
'
' COMMENTS:    
'
' *****************************************************
' ~~~Begin Script~~~

Option Explicit
' Declare Everything!
Dim objFSO, objTextFile, objTextFile1, objTextFile2, objWMIService, objItem, objService, objChksrv, i
Dim strComputer, Line, strNextLine, Srvpad, colItems, colListOfServices, oDrives, WshNetwork
Dim drive, objShell

On Error Resume Next

Const ForReading = 1, ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Opens the servers.txt file to find the server name(s)
' -----------------------------------------------------
Set objTextFile = objFSO.OpenTextFile("C:\TsmFix\servers.txt", ForReading)

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set objShell = WScript.CreateObject("WScript.Shell")

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
'Check if the Server is online and has TSM folder
' -----------------------------------------------------
Srvpad = "\\"&strComputer&"\c$\program files\TIVOLI\TSM\baclient"

IF objFSO.FolderExists(Srvpad) = FALSE THEN
WScript.Echo "Server " & strServer & " doesn't have TSM or is offline!"
WScript.Quit
END If

' Backup current TSM config
objFSO.CopyFile Srvpad & "\dsm.opt" , Srvpad &"\dsm.opt.old"

' Wait for copy to complete
WScript.Sleep 10000

' Set TSM Service name
' -----------------------------------------------------
Set objService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" _
& strComputer & "/root/cimv2:Win32_Service.Name='TSM Scheduler'")

' Stop TSM Scheduler
' -----------------------------------------------------
objService.StopService

' Set the file for Reading / Writing
' -----------------------------------------------------
Set objTextFile2 = objFSO.OpenTextFile(Srvpad & "\dsm.opt", ForReading)

       strText = objTextFile2.ReadAll
       objTextFile2.Close
       strNewText = Replace(strText, "HYDRA1", "James")

Set objTextFile2 = objFSO.OpenTextFile(Srvpad & "\dsm.opt", ForWriting)
objTextFile2.WriteLine strNewText
objTextFile2.Close
' -----------------------------------------------------
' Wait 10 seconds for things to settle down
WScript.Sleep 10000

' Start TSM Scheduler
objService.StartService
Wscript.Echo "The TSM Scheduler service on " & strComputer & " has restarted succesfully."

' Go get next computer name and start over...
Loop

objTextFile.Close

' -----------------------------------------------------
' Functions
' -----------------------------------------------------

'Read text file
Function GetFile(FileName)
If FileName<>"" Then
Dim FS, FileStream
Set FS = CreateObject("Scripting.FileSystemObject")
on Error resume Next
Set FileStream = FS.OpenTextFile(FileName)
GetFile = FileStream.ReadAll
End If
End Function

'Write text file.
Function WriteFile(FileName, Contents)
Dim OutStream, FS

on Error Resume Next
Set FS = CreateObject("Scripting.FileSystemObject")
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End FunctionStart Free Trial
[+][-]06.26.2007 at 06:41AM PDT, ID: 19363517

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 06:48AM PDT, ID: 19363605

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 07:00AM PDT, ID: 19363709

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 07:07AM PDT, ID: 19363777

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 07:52AM PDT, ID: 19364244

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 02:37PM PDT, ID: 19368057

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 02:42PM PDT, ID: 19368099

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 08:45PM PDT, ID: 19369578

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 09:04PM PDT, ID: 19369639

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: VB Script, Microsoft Windows Operating Systems
Tags: vbscript, replace, text, file
Sign Up Now!
Solution Provided By: RobSampson
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.26.2007 at 11:04PM PDT, ID: 19370043

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.26.2007 at 11:16PM PDT, ID: 19370074

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44