【问题标题】:PowerShell Command to Copy File on Remote MachinePowerShell命令在远程机器上复制文件
【发布时间】:2015-02-27 10:39:07
【问题描述】:

我需要使用 PowerShell 将文件从本地机器复制到远程机器。我可以使用以下命令将文件复制到远程计算机:

copy-item -Path d:\Shared\test.txt -Destination \\server1\Shared

上述命令使用网络共享路径来复制文件。我不想使用网络共享选项,因为该文件夹不会在远程机器上共享。我尝试了以下命令但无法正常工作。

copy-item -Path d:\Shared\test.txt -Destination \\server1\c$\Shared

Invoke-Command -ComputerName \\server -ScriptBlock {
  copy-item -Path D:\Shared\test.txt -Destination C:\Shared
}

请告诉我如何在不使用 UNC 路径的情况下使其工作。我对远程机器上的那个文件夹拥有完全权限。

【问题讨论】:

  • “不工作”到底是什么意思?你收到错误信息了吗?根本没有文件被复制?文件被复制到错误的位置?错误的文件被复制到正确的位置?还发生了什么事?
  • 另外,在运行 Invoke 命令时,路径将相对于正在运行的服务器,因此 D:\Shared\test.txt 需要是 UNC 路径
  • 我收到错误“找不到网络路径”。

标签: powershell


【解决方案1】:

Powershell 5 (Windows Server 2016)

还有downloadable 用于早期版本的 Windows。也可以使用-ToSession

$b = New-PSSession B
Copy-Item -FromSession $b C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt

早期版本的 PowerShell

不需要什么特别的,这些隐藏的共享存在于所有机器上。

Copy-Item -Path \\serverb\c$\programs\temp\test.txt -Destination \\servera\c$\programs\temp\test.txt

【讨论】:

  • 这对我有用。不想使用网络使用或连接驱动器,这完美无缺。
【解决方案2】:

这是一个适用于小文件的脚本。以管理员身份运行。

#pre 5.0 powershell copy-item to remote computer
Write-Host "Remote copy a file"
$servers = @("server01.dot.com", "server02.dot.com")
foreach($server in $servers) {
$username = 'USERNAME'
$password = 'PASSWORD'
$pw   = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $pw)
$myfile = [System.IO.File]::ReadAllBytes("C:\Temp\srctest.txt")
$s = New-PSSession -computerName $server -credential $cred
Enter-PSSession $s
Invoke-Command -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes("C:\Temp\desttest.txt", $args)}   
Write-Host "Completed"
Remove-PSSession $s
}

【讨论】:

    【解决方案3】:
    Invoke-Command -ComputerName compname -Credential $cred  -ScriptBlock { Get-Content C:\myfolder\result.txt } >>res.txt
    

    注意 C:\myfolder\result.txt 在远程计算机上

    【讨论】:

      【解决方案4】:

      由于使用的帐户是管理员,我发现最快的方法是执行以下操作:

      New-PSDrive -Name X -PSProvider FileSystem -Root \\MyRemoteServer\c$\My\Folder\Somewhere\
      cd X:\
      cp ~\Desktop\MyFile.txt .\
      ## Important, need to exit out of X:\ for unmouting share
      cd c:\
      Remove-PSDrive X
      

      每次都有效。

      【讨论】:

      • 您可以考虑使用Push-LocationPop-Location 来切换X:\ 目录。这将在代码执行后保持当前目录不变,从而更容易放置在函数或类似内容中。
      • 这仍然对我不起作用。不过,它让我走到了一半。 PS 5.1。复制后,我必须在源文件夹和目标上使用Get-ChildItem。否则Copy-Item 完成太快,文件没有被复制。
      【解决方案5】:

      如果没有可访问的共享,您必须将文件内容本身作为脚本的参数:

      Invoke-Command -ComputerName \\server -ScriptBlock {
        $args[0] | Set-Content  C:\Shared\test.txt
        } -ArgumentList (Get-Content D:\Shared\test.txt -Raw) 
      

      【讨论】:

      • -RawGet-Content 一起使用对于文本文件来说很好。对于二进制文件,使用-Encoding Byte -ReadCount 0 -Raw。或使用ReadAllBytes/WriteAllBytes
      【解决方案6】:

      您必须有一个共享文件夹才能将文件从一台主机复制到另一台主机,如果您想推送文件,可以在远程主机上:

      Copy-Item -Path D:\folder\test.txt -Destination \\server1\remoteshare\
      

      如果你想拉文件,也可以在本地主机上:

      Invoke-Command -ComputerName server1 -ScriptBlock {
        Copy-Item -Path \\localcomputer\localshare\test.txt -Destination C:\Shared\
      }
      

      只有当您的帐户在该特定计算机上具有管理员权限时,才能使用管理共享 (\\server1\c$)。

      【讨论】:

      • 我拥有远程机器的管理员权限,但 \\server1\c$ 不起作用。收到错误“找不到网络路径”。
      • @ParveenKumar server1 上的 Windows 防火墙是否允许 SMB 连接?你能telnet到server1上的445端口吗?
      猜你喜欢
      • 2021-08-08
      • 1970-01-01
      • 2010-10-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      相关资源
      最近更新 更多