Link to home
Start Free TrialLog in
Avatar of karkou12
karkou12

asked on

copy files to remote computers using powershell

Hi Experts,

I am trying to copy a file to multiple servers using powershell.

$FullPath = $MyInvocation.MyCommand.Path
$Scriptpath = Split-Path -Path $FullPath -Parent
cd $Scriptpath

$servers= '.\servers.txt'
$Source = '.\TraceConsolidator.exe.config'
$outfile = ".\ViewResult.log"
$Destination = 'Program Files\ndwill mynde\Sym\SyM_configMgmt'

#$computers = Get-Content $servers

[string[]]$Computers = Get-Content "$servers"


If(Test-Path $outfile) {Remove-Item -Path $outfile -Force}






ForEach($computer in $Computers){
"Trying to  connect to $computer"
"Trying to  connect to $computer " >> $outfile

if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{

"Problem in connecting to $computer"
"Problem in connecting to $computer " >> $outfile
"***********************************">> $outfile

}
else
{
"Connected to $computer"
"Connected to $computer " >> $outfile


Copy-Item $Source -Destination '\\$computer\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt' -Force
if (Test-Path "\\$computer\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt\TraceConsolidator.exe.config") 
{
write-host "copying the file to the C:\$Destination in $computer is successful"
"copied the file to the C:\$Destination in $computer " >> $outfile
"**************************************************">> $outfile


}
else
{

write-host "copying the file to the C:\$Destination in $computer is not successful"
  "copying the file to the C:\$Destination in $computer is not successful " >> $outfile
  "***********************************">> $outfile
}
}

}

Open in new window


But the script says it is connected to the computer and tbe says as below

Copy-Item : Could not find a part of the path '\\171.128.6.10\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt.  This works fine if there is no space in the destination  path.

Can you help me with what I am missing here.
Avatar of Qlemo
Qlemo
Flag of Germany image

The message cannot be a result of above script. You are using
Copy-Item $Source -Destination '\\$computer\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt' -Force

Open in new window

with the destination path enclosed in single quotes, and that prevents resolution of any variable. It tries to copy to that literal path as provided, and will not succeed.
Leaving that aside, assuming you made an error when posting here only (as the error message tells), the only other reason is that the remote path does not exist (yet). Spaces in the path are no reason.
Did you try a
dir  '\\171.128.6.10\C$\Program Files\ndwill mynde\Sym\SyM_configMgmt'

Open in new window

for testing? If you get an error, start with '\\171.128.6.10\C$' and drill down the path until you get an error.
Errors with "Program Files" often originate from x86 software on x64 OS. in which case you need to use "Program Files (x86)".
ASKER CERTIFIED SOLUTION
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America 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 Michael Kimmel
Michael Kimmel

Have you tried enclosing the entire path (the one with the space or spaces) in quotes?