Solved

script to copy and rename file on multiple servers

Posted on 2011-09-12
5
350 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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