Jump to content

Not sure why this PowerShell script won't work


Recommended Posts

Posted

Hi everyone,

 

I wrote a script some time back to copy some files from one machine to another which worked (script below).

 

$machines = Import-Csv .\lockscreencomputerlist.csv

ForEach ($machine in $machines)


{ 

   $ComputerID = $Machine.computername  


   $Source1 = '\\taw.org.uk\zone7\Homes\taw700staff\user1\test.jpg'
   $Source2 = '\\taw.org.uk\zone7\Homes\taw700staff\user1\test2.jpg'
   


   $path1 = '\\' + $ComputerID + '\C$\Windows\web\screen'
   $path2 = '\\' + $ComputerID + '\C$\Windows\web'
       
   
   
   Copy-Item -Path $Source1 -Destination $path1 -Force 
   Copy-Item -Path $Source2 -Destination $path2 -Force 


} 

 

 

Today I have had the need to copy 2 files from the following location

 

\\domain\zone0\homes\staff\user1\Senso Version and Config files\FILE LOCATION.txt

 

To this location

 

c$\Program Files\Renato\senso.cloud' on the remote machine

 

 

Having done this before my first thought was to modify my script so it looks like this

 

 

 

$machines = Import-Csv .\SensoUpdate.csv

ForEach ($machine in $machines)

{ 

   $ComputerID = $Machine.computername  

   $Source1 ='\\domain\zone0\homes\staff\user1\Senso Version and Config files\FILE LOCATION.txt'
   $Source2 = '\\domain\zone0\homes\staff\user1\Senso Version and Config files\Version.txt'


   $path1 = '\\' + $ComputerID + 'c$\Program Files\Renato\senso.cloud'
   $path2 = '\\' + $ComputerID + 'c$\Program Files\Renato\senso.cloud'
   
      
   Copy-Item -Path $Source1 -Destination $path1 -Force 
   Copy-Item -Path $Source2 -Destination $path2 -Force 


}

 

 

Having done this I receive the following error message

 

Copy-Item : The network path was not found
At U:\Documents\Batch and Script Files\Powershell Scripts\Scripts Folder\Final Working Versions\Copy Senso files from computer1 D drive to remote machines.ps1:22 char:5
+     Copy-Item -Path $Source1 -Destination $path1 -Force
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : NotSpecified: ( [Copy-Item], IOException
   + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : The network path was not found
At U:\Documents\Batch and Script Files\Powershell Scripts\Scripts Folder\Final Working Versions\Copy Senso files from copmuter1 D drive to remote machines.ps1:23 char:5
+     Copy-Item -Path $Source1 -Destination $path1 -Force
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : NotSpecified: ( [Copy-Item], IOException
   + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

 

 

 

 

I have ran the script line by line and the errors are definitely showing as a problem with the copy function and not being able to find the destination however I don’t know why.

 

I have verified the remote computer is on and can be pinged, the unc path of the files to be copied are correct and obtained via path copy long unc path name. My csv file is made up of a single column with the header computername and the second row is just a computer name e.g. PC001

 

 

 

Can anyone spot what I’ve done wrong and maybe tell me how to fix it?

 

I can use a batch file with xcopy if I need to however I would rather start using PowerShell where I can so I become familiar with it.

 

Thanks for any help you can offer.

 

Noel

Posted (edited)
Should it be '\c$\Program Files\Renato\senso.cloud' - with the slash in?

 

Agree with that too, unless you include a \ at the end of the computer name in your csv, it should come before c$ other wise you are going to \\Computerc$\Program Files\Renato\senso.cloud... which obviously wouldn't exist.

Edited by DJ-1701
  • Thanks 1
Posted

you're right. I've been staring at that for the last 30 minutes and missed it. I must have deleted the \ during the script edit and hadn't noticed. Thanks for such a quick reply.

 

- - - Updated - - -

 

as soon as I put the \ back in it worked, can't believe I didn't spot it.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...