Powershell 从 linux samba 复制到本地 windows 服务器文件夹

Powershell copy from linux samba to local windows server folder

我需要将文件从 Linux samba 服务器复制到各种 Windows Server 2008。 共享文件夹具有特定的登录名并且是只读的。 我可以使用 Windows Explorer 毫无问题地访问和复制共享文件。

但是,使用PowerShell复制文件时,总是报如下错误。 我试过使用 Copy-item、robocopy 和 bitstransfer,但它们都报错。

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

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

此方法出现以下错误

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...

所以,我尝试添加一个凭证参数

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

但在输入我的密码后收到此错误:

"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

然后,我决定尝试一下 BitsTransfer。

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

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

而且它也给了我一个错误:

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

请问如何复制这个文件?

编辑 - 20190403

我尝试了以下方法:

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

这导致:

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

于是,我打开资源管理器,在地址栏粘贴了\domain.or.ip\sharedfolder\。它要求我输入用户名和密码,然后文件就可用了。 之后,我返回到 PowerShell 并再次尝试相同的 Get-ChildItem cmdlet。然后,我能够按预期列出共享文件夹内容。

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

最后,我尝试了:

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

并且复制成功

嗯,只有在我在资源管理器中输入我的登录信息后,PowerShell 才能找到共享文件夹。 但是我需要它在不打开资源管理器的情况下进行复制。

您可以使用 Invoke-Command 提供备用凭据:

Invoke-Command -ScriptBlock {
$arq = "file.zip"
$downloadSource = "\domain.or.ip\sharedfolder$arq"
echo $downloadSource
Copy-Item -Path "$downloadSource" -Destination ".$arqAgenteZabbix"
} -Credential $Cred

我终于用比较简单的方法成功复制了共享文件。我使用 "NET USE" 命令打开会话并复制文件,如下所示。

$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

现在,新的挑战...加密明文密码。