Solved

Copy file across entire network

Posted on 2011-02-16
15
378 Views
Last Modified: 2012-05-11
I would like a script that will get the curent gateway and attempt to copy a file to every machine on that gateway

I will have full admin permissions on the gateway

So if the script determines my gateway was 10.0.10.40

It will try to copy the file to 10.0.10.1 -10.0.10.254

0
Comment
Question by:jfgray
  • 9
  • 5
15 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 34908580
What if it's not a class 'C' network?  

Does having "full admin permissions on the gateway" mean you'll have permissions on each machine on the network?
0
 

Author Comment

by:jfgray
ID: 34908608
I meant to say determin my IP address. We are going to move a big file  over our wan to one pc. after the file reaches the PC we would like it to check the C$ share on all computer on the local lan. if it finds a pc the copy the file  

0
 

Author Comment

by:jfgray
ID: 34908943
ok so file is coppied over the way to 1 pc.


The pc's ip address is 192.168.1.40

I would like it to attempt to copy to

192.168.1.1\c$\location
192.168.1.2\c$\location
192.168.1.3\c$\location
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jfgray
ID: 34910383

Curent IP=IP$


I would need to

 \\IP$+1\c$\temp    = \\192.168.1.1\c$\Temp
 \\IP$+2\c$\temp    = \\192.168.1.2\c$\Temp
 \\IP$+3\c$\temp    = \\192.168.1.3\c$\Temp
 \\IP$+4\c$\temp    = \\192.168.1.4\c$\Temp

I would like to do this for the entire  254 IP if no pc is found then move to the next with out erroring
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34913096
Hi, try something like this.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Location\"

strMyIP = ResolveIP(objNetwork.ComputerName)
If InStr(strMyIP, ".") > 0 Then
	If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
	strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
	For i = 1 To 254
		strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
		If Ping(strSubnet & i) = True Then
			If objFSO.FolderExists(strDestination) = True Then
				objFSO.CopyFile strSourceFile, strDestination, True
			End If
		End If
	Next
Else
	MsgBox "Unable to determine IP Address."
End If

Function ResolveIP(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("ping " & computerName & " -n 1")
	strOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
	RegEx.Global = True
	If RegEx.Test(strOutput) Then
		ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
	Else
		ResolveIP = "IP Address could not be resolved"
	End If
End Function

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:jfgray
ID: 34917460
It keeps stopping at

MsgBox "Unable to determine IP Address."

0
 

Author Comment

by:jfgray
ID: 34917717
Here is a script that gets my ip could we some how use this to determin the IP. In this script ?

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            WScript.Echo IPConfig.IPAddress(i)
        Next
    End If
Next
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34921805
Ok, try this.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Location\"

strMyIP = GetMyIP
If InStr(strMyIP, ".") > 0 Then
	If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
	strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
	For i = 1 To 254
		strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
		If Ping(strSubnet & i) = True Then
			If objFSO.FolderExists(strDestination) = True Then
				objFSO.CopyFile strSourceFile, strDestination, True
			End If
		End If
	Next
Else
	MsgBox "Unable to determine IP Address."
End If

Function GetMyIP
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	
	Set IPConfigSet = objWMIService.ExecQuery _
	    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
	 
	For Each IPConfig in IPConfigSet
	    If Not IsNull(IPConfig.IPAddress) Then 
	        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
	            If IPConfig.IPAddress(i) <> "0.0.0.0" Then strIP = IPConfig.IPAddress(i)
	        Next
	    End If
	Next
	GetMyIP = strIP
End Function

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:jfgray
ID: 34929638
ok turns out I do have to authenticate

How does this look password is stored in a file called input.txt in the same directory

I am not sure if I am passing admin rights to the entire process any thoughts ?

On Error Resume next
Set oShell = CreateObject("Wscript.Shell")
Authenticate = "runas /user:Domain\User cmd < input.txt"
oshell.Run Authenticate, 0




Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Temp\"

strMyIP = ResolveIP(objNetwork.ComputerName)
If InStr(strMyIP, ".") > 0 Then
      If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
      strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
      For i = 1 To 254
            strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
            If Ping(strSubnet & i) = True Then
                  If objFSO.FolderExists(strDestination) = True Then
                        objFSO.CopyFile strSourceFile, strDestination, True
                  End If
            End If
      Next
Else
      MsgBox "Unable to determine IP Address."
End If

Function ResolveIP(computerName)
      Set objShell = CreateObject("WScript.Shell")
      Set objExec = objShell.Exec("ping " & computerName & " -n 1")
      strOutput = objExec.StdOut.ReadAll
      Set RegEx = New RegExp
      RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
      RegEx.Global = True
      If RegEx.Test(strOutput) Then
            ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
      Else
            ResolveIP = "IP Address could not be resolved"
      End If
End Function

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
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34931127
Hi, try this.  It uses the specified username and password to map a connection the IPC$ share of each computer, to gain an access tunnel with those credentials.  Then the file is copied, and the connection is removed.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = "C:\Temp\MyFile.txt"
strTargetFolder = "C:\Location\"
strUser = "domain/admin"
strPassword = "password"

strMyIP = ResolveIP(objNetwork.ComputerName)
If InStr(strMyIP, ".") > 0 Then
	If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"
	strSubnet = Left(strMyIP, InStrRev(strMyIP, "."))
	For i = 1 To 254
		strDestination = "\\" & strSubnet & i & "\" & Replace(strTargetFolder, ":", "$")
		If Ping(strSubnet & i) = True Then
			objNetwork.MapNetworkDrive "", "\\" & strSubnet & i & "\IPC$", False, strUser, strPassword
			If objFSO.FolderExists(strDestination) = True Then
				objFSO.CopyFile strSourceFile, strDestination, True
			End If
			objNetwork.RemoveNetworkDrive "\\" & strSubnet & i & "\IPC$", True, False
		End If
	Next
Else
	MsgBox "Unable to determine IP Address."
End If

Function ResolveIP(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("ping " & computerName & " -n 1")
	strOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
	RegEx.Global = True
	If RegEx.Test(strOutput) Then
		ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
	Else
		ResolveIP = "IP Address could not be resolved"
	End If
End Function

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:jfgray
ID: 34945825
You are THE MAN !
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34946636
Please accept my comment ID:34931127 as the answer, as it appears to have provided the solution.
0
 

Author Comment

by:jfgray
ID: 34946814
oops Thought I did that !
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34947005
Great, thanks for the grade.

Regards,

Rob.
0
 

Author Comment

by:jfgray
ID: 34947010
Thank you for all your help !
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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