Test-Path

Welcome Forums General PowerShell Q&A Test-Path

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 6 months ago.

  • Author
    Posts
  • #71123

    Participant
    Points: 1
    Rank: Member

    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
                    }
                }
            }
        }
    
  • #71126

    Participant
    Points: 179
    Helping Hand
    Rank: Participant

    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.

    • #71191

      Participant
      Points: 1
      Rank: Member

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

The topic ‘Test-Path’ is closed to new replies.