Powershell copy from linux samba to local windows server folder

0

I need to copy a file from a Linux samba server to various Windows Server 2008. The shared folder has a specific login and is read-only. I can access and copy the shared file using Windows Explorer without a problem.

But, when using Powershell to copy the file, it always give an error as shown bellow. I have tried using Copy-item, robocopy and bitstransfer but they all give an error.

    $arq = "file.zip"
    $downloadSource = "\\domain.or.ip\sharedfolder\$arq"
    echo $downloadSource

    Copy-Item -Path "$downloadSource" -Destination ".\$arqAgenteZabbix"

this method gives me the following error

Copy-Item : Access denied + CategoryInfo : PermissionDenied: (\domain.or.ip\sharedfolder\file.zip:String) [Copy-Item], UnauthorizedAc cessException + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand ... Copy-Item: path not found...

So, I tried adding a credential parameter

    $credencial = New-PSSession -ComputerName "serverhostname" -Credential "serverhostname\sharedfolder"
    Copy-Item -Path "$downloadSource" -Destination ".\$arqAgenteZabbix" -ToSession $credencial

But received this error after typing my password:

"New-PSSession : [pxl0mon00013] Fail to connect to remote server >serverhostname ... WinRM cannot process the request... error 0x80090311 ... + CategoryInfo : OpenError (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin gTransportException + FullyQualifiedErrorId : AuthenticationFailed,PSSessionOpenFailed

Then, I decided to give BitsTransfer a shot.

Import-Module bitstransfer
    $arq = "file.zip"
    $downloadSource = "\\domain.or.ip\sharedfolder\$arq"

    Start-BitsTransfer -DisplayName DownloadName `
        -TransferType Download `
        -Source $downloadSource `
        -Destination .\$arq

And it also gave me an error:

Start-BitsTransfer : path not found '\domain.or.ip\sharedfolder\file.zip' does not exist. + CategoryInfo : ObjectNotFound: (\domain.or.ip\sharedfolder\file.zip:String) [Start-BitsTransfer], ParentC ontainsErrorRecordException + FullyQualifiedErrorId : PathNotFound,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand

How can I make this file copy, please?

EDIT - 20190403

I tried the following:

get-childitem \\domain.or.ip\sharedfolder\

wich resulted in:

Get-ChildItem : Cannot find path '\\domain.or.ip\sharedfolder\' because it does not exist.
At line:1 char:3
+ ls <<<<  \\domain.or.ip\sharedfolder\
    + CategoryInfo          : ObjectNotFound: (\domain.or.ip\sharedfolder\:String) [Get-ChildItem], ItemNotFo
   undException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

So, I openned Explorer and pasted \domain.or.ip\sharedfolder\ at the address bar. It asked me for username and password, then, the file was avaliable. After that, I returned to PowerShell and tried once again the same Get-ChildItem cmdlet. Then, I was able to list the shared folder contents as expected.

    Directory: \\domain.or.ip\sharedfolder
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        01/04/2019     10:06    3896455 file.zip

Finally, I tried:

Copy-Item -Path \\domain.or.ip\sharedfolder\file.zip -Destination ".\file.zip"

And it was copied successfully.

Well, only after I entered my login information in Explorer that PowerShell was able to find the shared folder. But I need it to copy without having to open explorer.

powershell
powershell-2.0
samba
asked on Stack Overflow Apr 2, 2019 by markfree • edited Apr 3, 2019 by markfree

2 Answers

0

You can provide alternate credentials with Invoke-Command:

Invoke-Command -ScriptBlock {
$arq = "file.zip"
$downloadSource = "\\domain.or.ip\sharedfolder\$arq"
echo $downloadSource
Copy-Item -Path "$downloadSource" -Destination ".\$arqAgenteZabbix"
} -Credential $Cred
answered on Stack Overflow Apr 2, 2019 by Bill • edited Apr 2, 2019 by HAL9256
0

I finally managed to successfully copy the shared file in a relative simple way. I used "NET USE" command to open a session and copy the file, like shown below.

$arq = "file.zip"
$downloadSource = "\\domain.or.ip\sharedfolder"

    net use $downloadSource /persistent:no /user:[user] [pass]
    Copy-Item -Path "$downloadSource\$arq" -Destination ".\$arq"
    net use $downloadSource /delete

Now, a new challenge... encrypt the clear-text password.

answered on Stack Overflow Apr 4, 2019 by markfree • edited May 1, 2019 by markfree

User contributions licensed under CC BY-SA 3.0