gpt4 book ai didi

windows - Copy-Item 用于使用凭据将文件从本地复制到远程服务器

转载 作者:可可西里 更新时间:2023-11-01 11:47:24 29 4
gpt4 key购买 nike

我正在尝试将一些文件和文件夹从我的本地计算机复制到远程服务器:

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.ps1New-PSDrive : The network path was not foundAt C:\Users\Administrator\Desktop\copyfiles.ps1:11 char:1+ New-PSDrive -Name X -PSProvider FileSystem -Root '\\$serverip\c$' -Credential $c ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (X:PSDriveInfo) [New-PSDrive], Win32Exception    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCCopy-Item : Cannot find drive. A drive with the name 'X' does not exist.At C:\Users\Administrator\Desktop\copyfiles.ps1:12 char:1+ Copy-Item '.\copy_test.txt' -Destination 'X:\backups'+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (X:String) [Copy-Item], DriveNotFoundException    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommandRemove-PSDrive : Cannot find drive. A drive with the name 'X' does not exist.At C:\Users\Administrator\Desktop\copyfiles.ps1:13 char:1+ Remove-PSDrive -Name X+ ~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : ObjectNotFound: (X:String) [Remove-PSDrive], DriveNotFoundExcepti    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand

我的服务器

我的服务器是 AWS 中的 Windows 实例。我拥有正确的权限,因为我能够运行其他命令,例如 Invoke-Command,以便检查远程服务器中的某些服务。

PS> $PSVersionTable.PSVersionMajor  Minor  Build  Revision-----  -----  -----  --------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 - Copy-Item 用于使用凭据将文件从本地复制到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41617778/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com