2016-11-28 52 views
1

我想通過Copy-Item cmdlet的參數-FromSession參數從遠程會話複製一些日誌文件。在調用機器上,我安裝了PS版本5。運行腳本時,我得到以下錯誤:Powershell版本5複製項目-FromSession無法找到

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版本安裝在目標上。

任何提示?

回答

4

當您嘗試從不同驅動器複製文件時,可能會遇到此PowerShell 5.0錯誤。如果其他系統上不存在驅動器,則會發生此錯誤。

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

作爲一種解決方法你可以:

  1. 將文件複製到在C的臨時文件夾 - 驅動(或另一種常見的驅動器)
  2. 複製這些文件到/從遠程計算機
  3. 的文件移動到最終目的地
0

當我那個錯誤,我必須指定完整的源路徑。

0

它似乎是PowerShell中的一個錯誤。對我而言,確保您試圖複製的遠程文件(例如D:\ remotefolder \ mylog.log)已存在於本地計算機上(完全相同的驅動器和路徑)。

# 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本地事先爲好。

如果您在本地和遠程沒有相同的驅動器號,則不起作用。你必須確保你做到了。

相關問題