0

I am trying to copy file from my local machine to a server destination

My Script: Copy-Item –Path D:\Test.txt –Destination '\\10.10.X.X\c$'

Error:

Copy-Item : The network path was not found
At D:\PS_Test_script.ps1:1 char:2
+  Copy-Item –Path D:\Test.txt –Destination '\\10.10.X.28X\c$'
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

The server has credentials, I am guessing that, I have to invoke something to use the credentials.

BlackCat
  • 1,932
  • 3
  • 19
  • 47
  • 1
    https://stackoverflow.com/a/65972741/11954025 – Daniel Jun 13 '21 at 07:32
  • Thanks @Daniel. Please give me idea of encryptedCred. What does it contain and in what format? – BlackCat Jun 13 '21 at 07:56
  • I think you would get a different message if the credentials are incorrect. Are you able to `ping` the ip address? – iRon Jun 13 '21 at 10:52
  • Yes, I am able to ping that @iRon – BlackCat Jun 13 '21 at 10:54
  • Do you have permission on C$ folder? try to test the copy on other folder (Copy-Item -Path $Localbatch -Destination "\\10.10.X.X\share" -Force) – Mahmood Shehab Jun 13 '21 at 10:58
  • `$Credetial = get-credential; Copy-Item –Path D:\Test.txt –Destination '\\10.10.X.X\c$ -credential $Credential` (for username, use: `.\LocalUsernameOnServer`) – iRon Jun 13 '21 at 11:01
  • @iRon, still getting error: Copy-Item : The network path was not found – BlackCat Jun 13 '21 at 11:12
  • I think we have to do with a clasic PowerShell gotcha here: You using em-dashes for the parameters. Please, try to *retype* the command (do not copy/paste). – iRon Jun 13 '21 at 11:23
  • @iRon, retyping and got this-> The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials. – BlackCat Jun 13 '21 at 11:29
  • 1
    Ok, that is a complete different message suggesting that I was right and the credentials aren't the issue... Or? – iRon Jun 13 '21 at 11:33
  • Conclusion: use the answer from [**@Daniel**](https://stackoverflow.com/users/11954025/daniel) and watchout for [unicode characters as e.g. em-dashes](https://stackoverflow.com/a/55053609/1701026) (note that your PowerShell version `Windows PowerShell` vs. `PowerShell Core` is also at stake here.) – iRon Jun 13 '21 at 12:06

2 Answers2

1

To keep it somewhat simple, to copy to a folder share which requires different credentials to access you can use New-PSDrive to map a drive using those credentials

$desiredMappedDrive = 'J'
$desiredMappedDrivePath = '\\10.10.X.X\c$' # Map to administrative C: drive share requires administrator credentials)

$source = 'D:\Test.txt'
$destination = "${desiredMappedDrive}:\temp" 

# Get-Credential cmdlet will request that you enter a username and password that has access to the share
New-PSDrive -Name $desiredMappedDrive -PSProvider FileSystem -Root $desiredMappedDrivePath -Credential (Get-Credential)

# after drive is mapped copy file over
Copy-Item -Path $source -Destination $destination -Verbose
Daniel
  • 4,792
  • 2
  • 7
  • 20
0

Here is a whole different approach using PSSession no need to map any drives:

$targetComputerName = "127.0.0.1"
$Session = New-PSSession $targetComputerName -Credential 'username'
$DestinationPath = "C:\temp"
$source = 'D:\Test.txt'

Copy-Item -Path $source -ToSession $Session -Destination $DestinationPath
$Session | Remove-PSSession

If you want to execute it as a script you would have to create a [SecureString] and build a credential object.

T-Me
  • 1,814
  • 1
  • 9
  • 22