2

I have a very simple powershell script which to copy files from a networked server.

here's the script

Write-Output "Copying Backups"
copy \\sqlbox\SqlBackups\Client.bak c:\sqlbackups

It throws this exception

Invalid Path: '\\sqlbox\SqlBackups\Client.bak'.
At C:\Work\db\restore.ps1:5 char:1
+ copy \\sqlbox\SqlBackups\Client.bak c:\sqlbackups
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], ArgumentException
+ FullyQualifiedErrorId : CopyItemDynamicParametersProviderException

If I open a windows explorer up and goto \sqlbox\SqlBackups it opens fine and if I then run the powershel script it also starts working and will work until the next time I reboot the PC.

How can I address this in the script?

Twisted
  • 2,939
  • 4
  • 32
  • 54
  • When you open the share from the explorer for the first time after a reboot, are you prompted to enter a username and password? – Musaab Al-Okaidi Feb 15 '13 at 10:37
  • possible duplicate of [Need help on Powershell Copy-Item from network drives](http://stackoverflow.com/questions/14653851/need-help-on-powershell-copy-item-from-network-drives) – KyleMit Aug 11 '15 at 13:31

2 Answers2

7

Try mapping the drive first, then carry out the copy operation.

New-PSDrive -Name S -Root \\sqlbox\SqlBackups\ -PSProvider FileSystem
Write-Output "Copying Backups"
copy S:\Client.bak c:\sqlbackups
Musaab Al-Okaidi
  • 3,734
  • 22
  • 21
6

I have a similar problem when copying files over servers with this error:

Cannot find path '\\computer1\d$\path' because it does not exist.

It works after adding Microsoft.PowerShell.Core\FileSystem:: in front of file name:

copy-item "Microsoft.PowerShell.Core\FileSystem::\\computer1\d$\path\installer.msi" "Microsoft.PowerShell.Core\FileSystem::\\computer2\d$\path\installer.msi"
George
  • 6,006
  • 6
  • 48
  • 68