Solved

script to copy and rename file on multiple servers

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

18 Experts available now in Live!

Get 1:1 Help Now