PowerShell 错误 - Copy-Item:找不到与名称“Stream”匹配的参数

问题描述 投票:0回答:1

我想创建一个 PowerShell 脚本来将 Templates 文件夹从远程计算机复制到本地计算机:

# Ask the administrator to enter the name of the remote PC
$RemotePCName = Read-Host "Please enter the name of the remote PC"

# Ask the administrator to enter their credentials
$Credential = Get-Credential -Message "Enter your administrator credentials to connect to remote PC $RemotePCName"

$IdenUser = Read-Host "Please enter user account ID"

#Templates folder path:
$path_outlook_templates = "C:\Users\$IdenUser\AppData\Roaming\Microsoft\Templates"
# Local PC variable declaration:
$local_pc_path = "C:\Users\ba89260\Documents\test"

$LocalPCName = $env:COMPUTERNAME

# Creating a session between the remote PC and the local PC
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"

# Copy Outlook Templates folder
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"#

# Displaying a Windows window
#[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")

该脚本按照我的要求执行,除了控制台在循环中向我返回“Stream”错误:

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进行了测试,但结果是相同的。我想指出的是,我能够将此文件夹从本地计算机复制到远程计算机......并且我没有收到任何错误。 你能帮我找到解决方案吗?谢谢你的帮助

powershell path stream copy-item
1个回答
0
投票

您遇到的错误是因为 Copy-Item cmdlet 需要一个名为 -Stream 的参数,但该参数不存在。 -Stream 参数不是 Copy-Item cmdlet 的有效参数。

正确代码:

# Copy Outlook Templates folder
Write-Host "Copying the Templates folder..."
Get-ChildItem -Path $outlook_templates_path -Recurse | Copy-Item -Destination $local_pc_path -Force
Write-Host "Success! Next step"

此代码使用 Get-ChildItem cmdlet 获取 $outlook_templates_path 目录中的文件和子文件夹,然后将结果通过管道传输到 Copy-Item cmdlet。这样,它将远程目录中的所有文件和子文件夹复制到本地目录。

另请注意,在这种情况下不需要 -FromSession 参数,因为它不会将文件从本地计算机复制到远程计算机。相反,它将文件从远程计算机复制到本地计算机。

© www.soinside.com 2019 - 2024. All rights reserved.