我需要将文件从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"
这个方法给我以下错误
Copy-Item:拒绝访问+ CategoryInfo:PermissionDenied:(\ domain.or.ip \ sharedfolder \ file.zip:String)[Copy-Item],UnauthorizedAc cessException + FullyQualifiedErrorId:ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand ... Copy-Item:找不到路径......
所以,我尝试添加凭证参数
$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
所以,我打开了Explorer并在地址栏粘贴了\ 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
现在,一个新的挑战......加密明文密码。