Solved

script to copy and rename file on multiple servers

Posted on 2011-09-12
5
344 Views
Last Modified: 2012-05-12
Hi All

I want to copy and rename machine.config on approx 100+ servers with date suffixed to the file name at same location. I want to do this on all servers using one script. Please suggest.

Please let me know if my question is not clear.

0
Comment
Question by:ghelaniabhishek
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 200 total points
ID: 36527306
Hi, place the computer names in computers.txt and then run this script from a DOS prompt with
cscript CopyMachineConfig.vbs

Regards,

Rob.
strConfig = "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config"
strComputer = "Computers.txt"
strDate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile(strComputers, ForReading, False)
While Not objFile.AtEndOfStream
	strComputer = Trim(objFile.ReadLine)
	If strComputer <> "" Then
		If Ping(strComputer) = True Then
			strOldConfig = "\\" & strComputer & "\" & Replace(strConfig, ":", "$")
			strNewConfig = strOldConfig & "." & strDate
			If objFSO.FileExists(strOldConfig) = True Then
				On Error Resume Next
				objFSO.CopyFile strOldConfig, strNewConfig, True
				If Err.Number = 0 Then
					WScript.Echo "Copied " & strOldConfig & " to " & strNewConfig
				Else
					WScript.Echo "Error copying " & strOldConfig & " to " & strNewConfig & ". Error " & Err.Number & ": " & Err.Description
				End If
				Err.Clear
				On Error GoTo 0
			Else
				WScript.Echo "Could not find " & strOldConfig
			End If
		Else
			WScript.Echo strComputer & " is offline"
		End If
	End If
Wend
objFile.Close
WScript.Echo VbCrLf & "Finished."

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 

Author Comment

by:ghelaniabhishek
ID: 36528057
it gives me error "Invalid procedure call or argument" on line 7
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36528156
Ooops, please change
strComputer = "Computers.txt"

to
strComputers = "Computers.txt"

Rob.
0
 

Author Comment

by:ghelaniabhishek
ID: 36528167
works now. Thank you.
Btw, can I have the backup file name of machine.config as "machine.Backup.13-09-2011.conf"
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36533504
Sure, this should be the required backup file format.

Regards,

Rob.
strConfig = "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config"
strComputers = "Computers.txt"
'strDate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)
strDate = Right("0" & Day(Now), 2) & "-" & Right("0" & Month(Now), 2) & "-" & Year(Now)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile(strComputers, ForReading, False)
While Not objFile.AtEndOfStream
	strComputer = Trim(objFile.ReadLine)
	If strComputer <> "" Then
		If Ping(strComputer) = True Then
			strOldConfig = "\\" & strComputer & "\" & Replace(strConfig, ":", "$")
			'strNewConfig = strOldConfig & "." & strDate
			strNewConfig = Left(strOldConfig, InStrRev(strConfig, "\") + 1) & "machine.backup." & strDate & ".conf"
			If objFSO.FileExists(strOldConfig) = True Then
				On Error Resume Next
				objFSO.CopyFile strOldConfig, strNewConfig, True
				If Err.Number = 0 Then
					WScript.Echo "Copied " & strOldConfig & " to " & strNewConfig
				Else
					WScript.Echo "Error copying " & strOldConfig & " to " & strNewConfig & ". Error " & Err.Number & ": " & Err.Description
				End If
				Err.Clear
				On Error GoTo 0
			Else
				WScript.Echo "Could not find " & strOldConfig
			End If
		Else
			WScript.Echo strComputer & " is offline"
		End If
	End If
Wend
objFile.Close
WScript.Echo VbCrLf & "Finished."

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now