无法将 System.Object[] 转换为类型 {System.String, System.Management.Automation.ScriptBlock}

Impossible to convert System.Object[] into type {System.String, System.Management.Automation.ScriptBlock}

当我在 Powershell 中使用 Invoke-command 时,Select-object 在程序中生成错误。

$pc='server1','server2'

$csv = "\mypc\c$\Users\me\Desktop\Script\PC2\Length.csv"
$command=Get-ChildItem -Path C:\Users -Exclude C:\Users\*\AppData -Recurse -Force -File | 
where Length -gt 300mb | Sort-Object -Property Length -descending | 
Out-String -stream | Select-Object -Property $pc,Directory,Name,$properties | 
Export-Csv -Path $csv -Delimiter ';' -Encoding UTF8 -NoTypeInformation

Invoke-Command -Computername $pc -ScriptBlock {$command}

这是错误代码:

Select-Object : Impossible de convertir System.Object[] dans l’un des types suivants {System.String, System.Management.Automation.ScriptBlock}.
Au caractère C:\Users\me\Desktop\Script\ScanPC.ps1:40 : 22
+ ... ng -stream | Select-Object -Property $pc,Directory,Name,$properties |

终于我发现了我的错误,我的变量没有声明到"Invoke-Command"。

但是,当我使用 Invoke-Command 在远程服务器中启动命令时,我在多个服务器上遇到此错误:

Impossible de trouver un paramètre correspondant au nom « File ».
+ CategoryInfo          : InvalidArgument : (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName        : Server1

我发送的命令是:

Get-ChildItem -Path C:\Users -Recurse -Force -File | 
Sort-Object -Property Length -descending | 
Select-Object -Property Directory,Name,$properties | 
Export-Csv -Path \Server2\c$\script\LogER\file.csv -Delimiter ';' -Encoding UTF8 - 
NoTypeInformation

当我在 Server2 中启动 Get-Child item 命令并将输出保存到 Server2 文件中时,它起作用了,但是 Server1 到 Server2 不起作用