2

I am trying to copy some files and folder from my local machine to a remote server:

Copy-Item .\copy_test.txt -destination "\\serverip\c$\backups\"

but I'm getting an error:

Copy-Item : Logon failure: unknown user name or bad password.
At line:1 char:10
+ Copy-Item <<<<  .\copy_test.txt -destination "\\serverip\c$\backups\" -verbose
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

I was trying using credentials but this command does not allow -Credential argument. I was searching a lot and in every example the command is pretty easy just executing the Copy-Item $source -destination $destination and I wonder why is so hard in my workstation.

Creating New PSDrive

I tried to create a New-PSDrive but it didn't work.

$creds = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $password

New-PSDrive -Name X -PSProvider FileSystem -Root '\\$serverip\c$' -Credential $creds -Persist
Copy-Item '.\copy_test.txt' -Destination 'X:\backups'
Remove-PSDrive -Name X

It is the error message:

PS C:\Users\Administrator\Desktop> .\copyfiles.ps1
New-PSDrive : The network path was not found
At C:\Users\Administrator\Desktop\copyfiles.ps1:11 char:1
+ New-PSDrive -Name X -PSProvider FileSystem -Root '\\$serverip\c$' -Credential $c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (X:PSDriveInfo) [New-PSDrive], Win32Exception
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveC

Copy-Item : Cannot find drive. A drive with the name 'X' does not exist.
At C:\Users\Administrator\Desktop\copyfiles.ps1:12 char:1
+ Copy-Item '.\copy_test.txt' -Destination 'X:\backups'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (X:String) [Copy-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Remove-PSDrive : Cannot find drive. A drive with the name 'X' does not exist.
At C:\Users\Administrator\Desktop\copyfiles.ps1:13 char:1
+ Remove-PSDrive -Name X
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (X:String) [Remove-PSDrive], DriveNotFoundExcepti
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand

My servers

My server are windows instances in AWS. I have the right permission because I am able to run other command like Invoke-Command in order to inspect some services into the remote server.

PS> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Robert
  • 10,403
  • 14
  • 67
  • 117

3 Answers3

1

If credentials are required for access to a remote share you need to map it to a (PS)drive before you can use it with other cmdlets.

$cred = Get-Credential
New-PSDrive -Name X -PSProvider FileSystem -Root "\\$serverip\c$" -Credential $cred -Persist
Copy-Item '.\copy_test.txt' -Destination 'X:\backups'
Remove-PSDrive -Name X
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • @Robert If you want to use variables in the UNC path string you must put it in double quotes, not single quotes. – Ansgar Wiechers Jan 12 '17 at 17:15
  • I did it. Now in double quotes... the error is the same. So the quotes aren't the root of the problem. – Robert Jan 12 '17 at 17:20
  • Do I need to share the server folder in order to create the PSDrive? – Robert Jan 12 '17 at 17:26
  • Well, of course you cannot map a shared folder if that folder isn't shared in the first place. Note that `C$` is a administrative share, so the account you're authenticating with needs to have admin privileges on the remote host to be able to access it. If that's not the case you need to create a share that's accessible with the given credentials. Also make sure that the firewall(s) allows access to the share. – Ansgar Wiechers Jan 12 '17 at 17:36
1

I found the solution. I was using PowerShell version 4.0 and then upgrade my version to 5.0

In previous version the Copy-Item doesn't allow credentials. Now is possible to copy files through the sessions between servers:

$deploy_dest = "C:\backup"
$username = "$server\Administrator"
$password =  Get-Content C:\mypassword.txt | ConvertTo-SecureString -AsPlainText -Force

$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

$session = New-PSSession -ComputerName $server -Credential $creds

Copy-Item -Path .\copy_test.txt -Destination -ToSession $session
Robert
  • 10,403
  • 14
  • 67
  • 117
1

Check this alternative using copy command and net use

net use \\10.164.60.77\c$\Users\ana\Desktop password /user:username
copy "C:\Users\alex\Desktop\test.txt" "\\10.164.60.77\c$\Users\ana\Desktop\test.txt"
Dorcioman
  • 499
  • 5
  • 6