1

When I use Invoke-command in Powershell, Select-object generates an error on the program.

$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}

Here is the error code :

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 |
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • 1
    `$command = { ... }; ... Invoke-Command -ScriptBlock $command`. You will need additional `$using` references to pass remote variables this way. – Jeroen Mostert Apr 19 '19 at 14:34
  • 1
    Although there are more things wrong with this code, let me start by pointing out that `Select-Object -Property $pc,Directory,Name,$properties` this line should be fixed first. `$pc` is an array and not the name of a property. Also, you have not defined what `$properties` variable is. If `$properties` is a string that contains the name of a property, that is the only way I think your code would make sense. – Sid Apr 19 '19 at 16:06

1 Answers1

0

Finally i found my error, my variable was not declared into the "Invoke-Command".

However, when i launch command in a remote server with Invoke-Command, i get this error on several server :

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

The command I send is :

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

When I launch Get-Child item command into Server2 and save the output into Server2 file, it work, but Server1 to Server2 not working