Test-FTPItem.ps1

Function Test-FTPItem
{
    <#
    .SYNOPSIS
        Test specific item on ftp server.
 
    .DESCRIPTION
        The Test-FTPItem cmdlet test specific item on ftp server if file or directory.
         
    .PARAMETER Path
        Specifies a path to ftp location.
             
    .PARAMETER Silent
        Hide result $True = File, $False = Directory.
         
    .PARAMETER Session
        Specifies a friendly name for the ftp session. Default session name is 'DefaultFTPSession'.
     
    .EXAMPLE
        PS> Test-FTPItem -Path "/myFolder"
        Directory
 
    .NOTES
        Author: Michal Gajda
        Blog : http://commandlinegeeks.com/
 
    .LINK
        Get-FTPChildItem
    #>
    

    [CmdletBinding(
        SupportsShouldProcess=$True,
        ConfirmImpact="Low"
    )]
    Param(
        [parameter(Mandatory=$true,
            ValueFromPipelineByPropertyName=$true,
            ValueFromPipeline=$true)]
        [Alias("FullName")]
        [String]$Path = "",
        [Switch]$Silent = $False,        
        $Session = "DefaultFTPSession"
    )
    
    Begin
    {
        if($Session -isnot [String])
        {
            $CurrentSession = $Session
        } else
        {
            $CurrentSession = Get-Variable -Scope Global -Name $Session -ErrorAction SilentlyContinue -ValueOnly
        }
        
        if($CurrentSession -eq $null)
        {
            Write-Warning "Cannot find session $Session. First use Set-FTPConnection to config FTP connection."
            Break
            Return
        }    
    }
    
    Process
    {
        Write-Debug "Native path: $Path"
        
        if($Path -match "ftp://")
        {
            $RequestUri = $Path
            Write-Debug "Use original path: $RequestUri"
            
        } else
        {
            $RequestUri = $CurrentSession.RequestUri.OriginalString+"/"+$Path
            Write-Debug "Add ftp:// at start: $RequestUri"
        }
        $RequestUri = [regex]::Replace($RequestUri, '/$', '')
        $RequestUri = [regex]::Replace($RequestUri, '/+', '/')
        $RequestUri = [regex]::Replace($RequestUri, '^ftp:/', 'ftp://')
        Write-Debug "Remove additonal slash: $RequestUri"
        
        if ($pscmdlet.ShouldProcess($RequestUri,"Remove item from ftp location")) 
        {    
            [System.Net.FtpWebRequest]$Request = [System.Net.WebRequest]::Create($RequestUri)
            $Request.Credentials = $CurrentSession.Credentials
            $Request.EnableSsl = $CurrentSession.EnableSsl
            $Request.KeepAlive = $CurrentSession.KeepAlive
            $Request.UseBinary = $CurrentSession.UseBinary
            $Request.UsePassive = $CurrentSession.UsePassive
            
            $Request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
            
            Try
            {
                [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$CurrentSession.ignoreCert}
                Write-Debug "Send Request"
                $Response = $Request.GetResponse()

                [System.IO.StreamReader]$Stream = New-Object System.IO.StreamReader($Response.GetResponseStream(),[System.Text.Encoding]::Default)
                [string]$Line = $Stream.ReadLine()
                
                if($Line -eq "\." -or $Line -match "/\.")
                {
                    if($Silent) { $Status = $False } else { $Status = "Directory" }
                } else {
                    if($Silent) { $Status = $True } else { $Status = "File" }
                }
                
                Return $Status
            }
            Catch
            {
                Write-Error $_.Exception.Message -ErrorAction Stop 
            }
        }
    }
    
    End{}                
}