Test-Path

This topic contains 2 replies, has 2 voices, and was last updated by  Stephen 4 months, 4 weeks ago.

  • Author
    Posts
  • #71123

    Stephen
    Participant

    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

    Rob Simmers
    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

      Stephen
      Participant

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

You must be logged in to reply to this topic.