Solved

script to copy and rename file on multiple servers

Posted on 2011-09-12
5
356 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
[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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …

737 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