【问题标题】:Powershell version 5 Copy-Item -FromSession cannot be foundPowershell 版本 5 Copy-Item -FromSession 找不到
【发布时间】:2017-04-11 22:29:40
【问题描述】:

我正在尝试通过Copy-Item cmdlet 的-FromSession 参数从远程会话复制一些日志文件。在呼叫机器上,我安装了 PS 版本 5。运行脚本时出现以下错误:

Copy-Item : A parameter cannot be found that matches parameter name 'FromSession'.

当我在源计算机上调用 $PSVersionTable 时,我得到以下输出:

PSVersion                      5.0.10586.672
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.672
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

我还检查了会话状态,它已打开:

$session | select State

State
-----
Opened

脚本:

$globalTargets = @("machine1.domain", "machine2.domain")

function Copy-LogsFromRemotes {
   param (
      [Parameter(Mandatory=$true)]
      $Destination
   )

   $password = "xxxxx" | ConvertTo-SecureString -AsPlainText -Force
   $credentials = new-object -typename       System.Management.Automation.PSCredential -argumentlist "domain\user",$password

   foreach ($target in $globalTargets) {

     $session = New-PSSession -ComputerName $target -Credential $credentials
     if (!(Test-Path $Destination)){
         New-Item -Path $Destination -ItemType Directory
     }
     $copyDestination = ("{0}\{1}" -f $Destination, $session.ComputerName)

     if (!(Test-Path $copyDestination)){
        New-Item -Path $copyDestination -ItemType Directory
     }

     Invoke-Command -Session $session -ScriptBlock { Test-Path "D:\Logs"}
     Copy-Item -LiteralPath "D:\Logs" -Destination $copyDestination -Verbose -FromSession $session

     Remove-PSSession -Session $session
   }
}

我是否还需要在目标机器上安装 PS 版本 5?实际上目标上安装了PS版本。

有什么提示吗?

【问题讨论】:

    标签: powershell powershell-4.0 powershell-5.0


    【解决方案1】:

    当您尝试从不同的驱动器复制文件或将文件复制到不同的驱动器时,您可能会遇到此 PowerShell 5.0 错误。如果其他系统上不存在该驱动器,则会发生此错误。

    见:https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11306682-bug-copy-item-fromsession-fails-if-local-machine

    作为一种解决方法,您可以:

    1. 将文件复制到 C: 驱动器(或其他常用驱动器)上的临时文件夹中
    2. 将这些文件复制到远程计算机/从远程计算机复制这些文件
    3. 将文件移动到最终目的地

    【讨论】:

      【解决方案2】:

      当我收到该错误时,我必须指定完整的源路径。

      【讨论】:

        【解决方案3】:

        这似乎是 PowerShell 中的一个错误。对我有用的是确保您尝试复制的远程文件(例如 D:\remotefolder\mylog.log)已经存在于您的本地计算机上(完全相同的驱动器和路径)。

        更新:错误似乎已修复(至少在 5.1.14409.1018 中)

        # Workaround PowerShell bug by creating local dummy copy of remote file we want to fetch
        "" | Set-Content "D:\remotefolder\mylog.log"
        # Copy down remote file, overwriting dummy copy with -Force
        Copy-Item -FromSession $app01 -Path "D:\remotefolder\mylog.log" -Destination "D:\localfolder\" -Force
        

        当然,您还必须事先在本地创建D:\remotefolder。

        如果您在本地和远程没有相同的驱动器号,这将不起作用。你必须确保你只是这样做。

        【讨论】:

          【解决方案4】:

          当您在源文件名中放置通配符(例如c:\*.log)时,我也看到了同样的错误。根据docs,-Path参数不接受通配符。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-12-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-11
            相关资源
            最近更新 更多