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

Avatar of undefined
Last Comment
Michael Kimmel

8/22/2022 - Mon
Qlemo

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Michael Kimmel

Have you tried enclosing the entire path (the one with the space or spaces) in quotes?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck