Search code examples
powershellpowershell-3.0powershell-4.0powershell-remoting

Invoke-Command doesn't return anything from my Get-ChildItem


This script doesnt return anything to the $file parameter. if i do it without the Invoke-Command on the server itself it does find a file.

Remote Example:

$file =Invoke-command -ComputerName $server -Credential $cred -ArgumentList $fileName -scriptblock{param($fileName)(Get-ChildItem -Recurse -Force "C:" -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -eq $fileName) })}

write-host $file
write-host $file.PSPath

if($file -ne $null -or $file -ne ""){
        $path= $file.PSPath
    }
return $path

Local example:

$file = Get-ChildItem -Recurse -Force "C:" -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -eq $fileName) }
write-host $file
write-host $file.PSPath
if($file -ne $null -or $file -ne ""){
        $path= $file.PSPath
    }
return $path

Solution

  • I believe that your issue is with your Get-ChildItem statement. Here is an example to help you sort it out:

    PS C:\Temp> Get-ChildItem 'C:'
    
    
        Directory: C:\Temp
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----           3/9/18   6:42 AM              0 file
    
    
    PS C:\Temp> Get-ChildItem -Path 'C:\'
    
    
        Directory: C:\
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----          12/1/17   9:30 AM                PerfLogs
    d-r---           3/7/18   2:52 PM                Program Files
    d-r---          2/26/18   5:12 PM                Program Files (x86)
    d-----           3/9/18   6:42 AM                Temp
    d-r---          12/1/17  11:30 AM                Users
    d-----           3/6/18  11:46 AM                Windows
    
    
    PS C:\Temp>
    

    Also, please keep in mind that as of PowerShell version 3 you can use -Directory or -File instead of a Where-Object PSIsContainer logic comparison:

    PS C:\Temp> Get-ChildItem -Path 'C:\' -File
    PS C:\Temp> Get-ChildItem -Path 'C:\Temp\' -File
    
    
        Directory: C:\Temp
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----           3/9/18   6:42 AM              0 file
    
    
    PS C:\Temp> Get-ChildItem -Path 'C:\Temp\' -Directory
    PS C:\Temp> Get-ChildItem -Path 'C:\' -Directory
    
    
        Directory: C:\
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----          12/1/17   9:30 AM                PerfLogs
    d-r---           3/7/18   2:52 PM                Program Files
    d-r---          2/26/18   5:12 PM                Program Files (x86)
    d-----           3/9/18   6:42 AM                Temp
    d-r---          12/1/17  11:30 AM                Users
    d-----           3/6/18  11:46 AM                Windows
    
    
    PS C:\Temp> 
    

    I think that this greatly aids in readability, but it does sacrifice backward compatibility if you are ever going to run this script in PowerShellv2 for some reason.

    TL;DR

    I think that you need to do this:

    $file = Get-ChildItem `
        -Recurse `
        -Force `
        -Path 'C:\' `
        -File `
        -ErrorAction SilentlyContinue | 
            ?{$_.Name -eq $fileName}
    

    A simple test that I performed:

    PS C:\Temp> $fileName='file'
    PS C:\Temp> $file = Get-ChildItem `
    >>     -Recurse `
    >>     -Force `
    >>     -Path 'C:\' `
    >>     -File `
    >>     -ErrorAction SilentlyContinue |
    >>         ?{$_.Name -eq $fileName}
    PS C:\Temp> $file.PSPath
    Microsoft.PowerShell.Core\FileSystem::C:\Temp\file
    PS C:\Temp>
    

    I hope that this helps!

    EDIT

    Now that I think about it you could use -Filter as well to filter for your filename:

    PS C:\Temp> New-Item demo.csv
    
    
        Directory: C:\Temp
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----           3/9/18   7:12 AM              0 demo.csv
    
    
    PS C:\Temp> Get-ChildItem -Path C:\Temp\ -Filter "$fileName"
    
    
        Directory: C:\Temp
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----           3/9/18   6:42 AM              0 file
    
    
    PS C:\Temp> $fileName = '*.csv'
    PS C:\Temp> Get-ChildItem -Path C:\Temp\ -Filter "$fileName"
    
    
        Directory: C:\Temp
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----           3/9/18   7:12 AM              0 demo.csv
    
    
    PS C:\Temp>
    

    This would remove your need for a Where-Object pipe.

    And... I just noticed that mklement0 pointed out this second point as well in the comment section above. My bad.