我正在尝试将一些文件和文件夹从本地计算机复制到远程服务器:
Copy-Item .\copy_test.txt -destination "\\serverip\c$\backups\"
但出现错误:
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
这是错误消息:
PS C:\Users\Administrator\Desktop>。\copyfiles.ps1
New-PSDrive:找不到网络路径
在C:\Users\Administrator\Desktop\copyfiles.ps1:11 char:1
+ New-PSDrive-名称X -PSProvider文件系统-Root'\\$ serverip\c $'-凭据$ c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:InvalidOperation:(X:PSDriveInfo)[New-PSDrive],Win32Exception
+ FullyQualifiedErrorId:CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveC
复制项目:找不到驱动器。名称为“X”的驱动器不存在。
在C:\Users\Administrator\Desktop\copyfiles.ps1:12 char:1
+复制项目“。\copy_test.txt”-目标“X:\backups”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
+ CategoryInfo:ObjectNotFound:(X:String)[Copy-Item],DriveNotFoundException
+ FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Remove-PSDrive:找不到驱动器。名称为“X”的驱动器不存在。
在C:\Users\Administrator\Desktop\copyfiles.ps1:13 char:1
+删除-PSDrive-名称X
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:ObjectNotFound:(X:String)[Remove-PSDrive],DriveNotFoundExcepti
+ FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand
我的服务器
我的服务器是AWS中的Windows实例。我拥有正确的权限,因为我可以运行其他命令(例如
Invoke-Command
)来检查远程服务器中的某些服务。PS> $ PSVersionTable.PSVersion
重大次要版本修订
----- ----- ----- --------
4 0 -1 -1
最佳答案
如果访问远程共享需要凭据,则需要将其映射到(PS)驱动器,然后才能将其与其他cmdlet一起使用。
$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
关于windows - 使用凭证将文件从本地复制到远程服务器的复制项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41617778/