Search code examples
powershellremote-accessinvoke-command

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


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 |

Solution

  • 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