【问题标题】:Powershell copy from linux samba to local windows server folderPowershell从linux samba复制到本地windows服务器文件夹
【发布时间】:2019-08-23 21:23:45
【问题描述】:

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

但是,当使用 PowerShell 复制文件时,它总是会报错,如下所示。 我尝试过使用 Copy-item、robocopy 和 bitstransfer,但它们都给出了错误。

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

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

这个方法给了我以下错误

复制项目:访问被拒绝

  • CategoryInfo : PermissionDenied: (\domain.or.ip\sharedfolder\file.zip:String) [Copy-Item], UnauthorizedAc 进程异常
  • FullyQualifiedErrorId:ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand ... 复制项:找不到路径...

所以,我尝试添加凭据参数

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

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

"New-PSSession : [pxl0mon00013] 无法连接到远程服务器 >serverhostname ... WinRM 无法处理请求...错误 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:找不到路径 '\domain.or.ip\sharedfolder\file.zip' 不存在。

  • 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 才能找到共享文件夹。 但是我需要它来复制而不必打开资源管理器。

【问题讨论】:

    标签: powershell powershell-2.0 samba


    【解决方案1】:

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

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

    【讨论】:

    • 我试过了,它给了我这个错误: + Invoke-Command -Credential $credencial -ScriptBlock { + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
    【解决方案2】:

    我终于设法以相对简单的方式成功复制了共享文件。我使用“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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2011-11-08
      相关资源
      最近更新 更多