Link to home
Start Free TrialLog in
Avatar of jfgray
jfgray

asked on

Copy file across entire network

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

Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

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?
Avatar of jfgray
jfgray

ASKER

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  

Avatar of jfgray

ASKER

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
Avatar of jfgray

ASKER


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
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

Avatar of jfgray

ASKER

It keeps stopping at

MsgBox "Unable to determine IP Address."

Avatar of jfgray

ASKER

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
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

Avatar of jfgray

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jfgray

ASKER

You are THE MAN !
Please accept my comment ID:34931127 as the answer, as it appears to have provided the solution.
Avatar of jfgray

ASKER

oops Thought I did that !
Great, thanks for the grade.

Regards,

Rob.
Avatar of jfgray

ASKER

Thank you for all your help !