找不到 Powershell 版本 5 Copy-Item -FromSession
Powershell version 5 Copy-Item -FromSession cannot be found
我正在尝试通过 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 5.0 错误。如果驱动器在其他系统上不存在,则会出现此错误。
作为解决方法,您可以:
- 将文件复制到 C: 驱动器(或其他常用驱动器)上的临时文件夹
- 复制那些文件to/from远程机器
- 将文件移动到最终目的地
当我收到该错误时,我必须指定完整的源路径。
这似乎是 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
。
如果您在本地和远程没有相同的驱动器号,这将不起作用。您必须确保自己做到了。
当您在源文件名中放置一个通配符时,例如 c:\*.log
,我也看到同样的错误出现。根据docs,-Path参数不接受通配符。
我正在尝试通过 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 5.0 错误。如果驱动器在其他系统上不存在,则会出现此错误。
作为解决方法,您可以:
- 将文件复制到 C: 驱动器(或其他常用驱动器)上的临时文件夹
- 复制那些文件to/from远程机器
- 将文件移动到最终目的地
当我收到该错误时,我必须指定完整的源路径。
这似乎是 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
。
如果您在本地和远程没有相同的驱动器号,这将不起作用。您必须确保自己做到了。
当您在源文件名中放置一个通配符时,例如 c:\*.log
,我也看到同样的错误出现。根据docs,-Path参数不接受通配符。