1

I'm a bit confused as to why the piping of path's to my function works perfectly and returns 2 times $true when the path exists and we can write to it. But when I don't pipe it and append the paths after the function, it's only returning one time $true as if the second path isn't checked.

This works fine and returns 2 times $true:

"L:\Scheduled Task\Auto_Clean", "L:\Scheduled Task" | Can-WriteToFolder

This only returns one result of $true:

Can-WriteToFolder "L:\Scheduled Task\Auto_Clean", "L:\Scheduled Task"

I think there's something wrong with my parameters, but I can't seem to figure it out. Thank you for your help.

Function:

Function Can-WriteToFolder {
        [CmdletBinding(SupportsShouldProcess=$True)]
    Param(
         [Parameter(ValueFromPipeline=$true,mandatory=$true)]
         [ValidateNotNullOrEmpty()]
         [ValidateScript({Test-Path $_ -PathType Container})]
         [String[]]
         $Path
    )

   Process {
            try {
                 Write-Verbose "Write a new file to the folder '$Path'"
                 $TestPath = Join-Path $Path Get-Random
                 New-Item -Path $TestPath -ItemType File -ErrorAction Stop > $null
                 Write-Verbose "Return TRUE for '^$Path'"
                 return $true
            } 
            catch {
                   Write-Verbose "Catch return FALSE for '$Path'"
                   return $false
            } 
            finally {
                     Remove-Item $TestPath -ErrorAction SilentlyContinue
            }
    }
}
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • read here: http://stackoverflow.com/a/887406/520612 – CB. Jul 21 '14 at 09:59
  • I literally copied the whole code block of `process` in the example you proposed and it's still returning the same result. Even when putting it in a `foreach` loop like in the example `foreach ($_ in $Path)`. – DarkLite1 Jul 21 '14 at 10:26
  • Read my answer please ;) – CB. Jul 21 '14 at 10:53

1 Answers1

0

try this:

[CmdletBinding(SupportsShouldProcess=$True)]
    Param(
         [Parameter(ValueFromPipeline=$true,mandatory=$true)]
         [ValidateNotNullOrEmpty()]
         #[ValidateScript({Test-Path $_ -PathType Container})]
         [String[]]
         $Path
    )

   Process {
            foreach ( $_ in $Path)
            {

            try {
                 Write-Verbose "Write a new file to the folder '$_'"
                 $TestPath = Join-Path $_ Get-Random
                 New-Item -Path $TestPath -ItemType File -ErrorAction Stop > $null
                 Write-Verbose "Return TRUE for '^$_'"
                 $true
            } 
            catch {
                   Write-Verbose "Catch return FALSE for '$_'"
                   $false
            } 
            finally {
                     Remove-Item $TestPath -ErrorAction SilentlyContinue
            }

            }
    }

I've removed the key return that stop the foreach!

CB.
  • 58,865
  • 9
  • 159
  • 159
  • I should have seen that! But it's still strange that it worked when piping.. Thank you for your help CB, really appreciate it. – DarkLite1 Jul 21 '14 at 11:26