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

Copy-Item for copy files from local to remove server using credentials

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

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

我正在尝试使用凭据,但此命令不允许 -Credential 参数。我搜索了很多,在每个示例中,只需执行 Copy-Item $source -destination $destination 命令就非常简单,我想知道为什么在我的工作站中如此困难。

创建新的 PSDrive

我试图创建一个 New-PSDrive 但没有成功。

$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 : The network path was not found
At 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.NewPSDriveC

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

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

Major  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

我找到了解决方案。我使用的是 PowerShell 4.0 版,然后将我的版本升级到 5.0

在以前的版本中,Copy-Item 不允许凭据。现在可以通过服务器之间的会话复制文件:

$deploy_dest = "C:\backup"
$username = "$server\Administrator"
$password =  Get-Content C:\mypassword.txt | ConvertTo-SecureString -AsPlainText -Force

$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

$session = New-PSSession -ComputerName $server -Credential $creds

Copy-Item -Path .\copy_test.txt -Destination -ToSession $session

使用 copy 命令和 net use

检查此备选方案
net use \10.164.60.77\c$\Users\ana\Desktop password /user:username
copy "C:\Users\alex\Desktop\test.txt" "\10.164.60.77\c$\Users\ana\Desktop\test.txt"