PowerShell远程复制Templates文件夹遇Stream参数错误求助
问题描述
我编写了一个PowerShell脚本,用于将远程电脑的Templates文件夹复制到本地。脚本能完成预期的复制操作,但控制台反复返回以下错误:
Copy-Item: Could not find a parameter matching the name "Stream".
To the character Line 31: 1
- Copy-Item -Path $outlook_templates_path -Destination $pc_path ...
+ CategoryInfo: InvalidArgument: (:) [Get-Item], ParameterBindingException + FullyQualifiedErrorId: NamedParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand
我尝试过用-LiteralPath替换-Path参数,结果一致。另外,将本地文件夹复制到远程电脑的操作完全正常,无报错。
脚本内容如下:
# 让管理员输入远程电脑名称 $RemotePCName = Read-Host "Please enter the name of the remote PC" # 让管理员输入凭据 $Credential = Get-Credential -Message "Enter your administrator credentials to connect to remote PC $RemotePCName" $IdenUser = Read-Host "Please enter user account ID" # Templates文件夹路径 $path_outlook_templates = "C:\Users\$IdenUser\AppData\Roaming\Microsoft\Templates" # 本地电脑路径 $local_pc_path = "C:\Users\ba89260\Documents\test" $LocalPCName = $env:COMPUTERNAME # 建立远程与本地的会话 Write-Host "Creating a session between the remote PC $RemotePCName and the local PC $LocalPCName:" $Session = New-PSSession -ComputerName $RemotePCName -Credential $Credential if ($Session -eq $null) { [System.Windows.Forms.MessageBox]::Show("The connection between computer $RemotePCName and computer $LocalPCName could not be established.", "Session error", "OK", "Error" ) exit } Write-Host "Success! Next step" # 复制Outlook Templates文件夹 Write-Host "Copying the Templates folder..." Copy-Item -Path $outlook_templates_path -Destination $local_pc_path -FromSession $Session -Recurse -Force Write-Host "Success! Next step"# # 显示Windows弹窗 #[Opening Windows message box]::Show("Message to display in the window", "Title in the window", "button", "icon in the message") [System.Windows.Forms.MessageBox]::Show("Copying data from $RemotePCName to $LocalPCName is complete.", "Done", "OK", "Information")
原因分析
- 变量名拼写错误:脚本中定义的路径变量是
$path_outlook_templates,但Copy-Item命令里误用了$outlook_templates_path,这会导致路径参数异常(虽然你提到复制能完成,但这个错误必须修正)。 - PowerShell版本不兼容:
Copy-Item的-Stream参数是PowerShell 5.1及以后版本引入的特性。当本地使用高版本PowerShell发起远程复制时,远程电脑的低版本PowerShell无法识别该参数,从而抛出错误。
解决方案
1. 修正变量名拼写错误
将Copy-Item中的变量名改为正确的$path_outlook_templates:
Copy-Item -Path $path_outlook_templates -Destination $local_pc_path -FromSession $Session -Recurse -Force
2. 解决版本不兼容问题
方法1:指定远程会话使用兼容的PowerShell版本
创建会话时,通过-ConfigurationName指定使用PowerShell 5.1(确保远程电脑已安装该版本):
$Session = New-PSSession -ComputerName $RemotePCName -Credential $Credential -ConfigurationName Microsoft.PowerShell.5.1
方法2:强制不传递-Stream参数
在Copy-Item命令中显式添加-Stream $null,避免远程会话因识别不了参数报错:
Copy-Item -Path $path_outlook_templates -Destination $local_pc_path -FromSession $Session -Recurse -Force -Stream $null
方法3:升级远程电脑的PowerShell版本
将远程电脑的PowerShell升级到与本地相同的版本(如5.1或7.x),从根源解决参数兼容问题。
3. 额外调试建议
- 验证远程路径是否存在:
Invoke-Command -Session $Session -ScriptBlock { Test-Path $using:path_outlook_templates }
- 检查本地与远程的PowerShell版本:
# 本地版本 $PSVersionTable.PSVersion # 远程版本 Invoke-Command -Session $Session -ScriptBlock { $PSVersionTable.PSVersion }
内容的提问来源于stack exchange,提问作者user24950367
相关产品推荐
相关产品推荐

