簡體   English   中英

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

使用復制命令和網絡使用檢查此替代方法

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM