?
Solved

script to copy and rename file on multiple servers

Posted on 2011-09-12
5
Medium Priority
?
358 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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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