Test-Path

Hello, I’m writing a script to test for a path on a remote computer and if that path is found take a certain action. The problem I’m having is test-path returns True regardless. Below is a sample of my script. Any idea why I get True but if I manually remote to the computer and run the same test-path command it returns false?

Function Add-Tns1 {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True)]
        [string[]]$Computername
        )

 Begin{}
 Process{
        $path = 'c:\oracle11'
        $test = test-path -path $path
        $ses

        foreach ($cpu in $Computername) {
        
            Try {

                $ses = New-PSSession $cpu -ErrorAction Stop
                invoke-command -Session $ses -ScriptBlock {
                $test
                Write-Debug "$test"
                    }
                }
        
            Catch {
                Write-output "client unavailable"
                break
                }
            }
        }
    }

Check out the eBook link above and look for Secrets of Powershell Remoting. The first issue is that Test-Path is not in your script block, so it’s not being executed on the client. Second, anytime you pass something outside of the script block, you need to use the using: keyword and leverage -ArgumentList to pass those parameters.

Thanks Rob for the clarification and resource, this helps tremendously.