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.
karkou12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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)".
Jason CrawfordTransport NinjaCommented:
At least part of the problem as the Qlemo pointed out is double vs. single quotes and their use in your script.  If you must use admin shares like c$ in your UNC path instead of a shared folder, try using Join-Path...so something like:
$a = "\\$computer"
$b = 'C$\Program Files\ndwill mynde\Sym\SyM_configMgmt'

$path = Join-Path $a $b

Open in new window

Either that or you can escape the dollar sign in c$ with a backtick like so:
"\\$computer\C`$\Program Files\ndwill mynde\Sym\SyM_configMgmt"

Open in new window

Another idea might be to save all server names or IPs in a .txt file (let's call it servers.txt) and use Invoke-Command to fan out to all servers concurrently to copy the file:
$a = "Insert source path here"
$b = "\\localhost\rest of target path"

Invoke-Command -ComputerName (Get-Content .\servers.txt) -ScriptBlock {Copy-Item $a $b}

Open in new window

You can use localhost in the target path because Invoke-Command will literally run whatever is contained in the ScriptBlock on the remote server.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael KimmelCommented:
Have you tried enclosing the entire path (the one with the space or spaces) in quotes?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.