Connection Acknowledgement to FTP site: Failure

Welcome Forums General PowerShell Q&A Connection Acknowledgement to FTP site: Failure

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

 
Participant
3 weeks, 4 days ago.

  • Author
    Posts
  • #112556

    Participant
    Points: 0
    Rank: Member
    Import-Module -Name PSFTP
    
    $ftp = "Hostname"
    
    $user = "Username"
    
    $ppass = "password"
    
    $pass = ConvertTo-SecureString $ppass -Force -AsPlainText
    
    $cred = New-Object Management.Automation.PSCredential($user,$pass)
    
    $success = Set-FTPConnection -$Server $ftp -$Credentials $cred -$Session MyTestSession -UsePassive
    
    if ($success -eq $true) { Write-Host "Connection Done" }
    
    else
    
    { Write-Host "failed" }
    
    

    If i manual try to access the files/folder, i can able to establish the connection.

    When i try with the PS script, it will throw an error—>

    "Set-FTPConnection : A positional parameter cannot be found that accepts argument

    'System.Management.Automation.PSCredential'.

    At C:\Users\mpradeep\OneDrive – Campus Management Corp\Desktop\pf.ps1:9 char:12

    + $success = Set-FTPConnection -$Server $ftp -$Credentials $cred -$Sess ...

    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Set-FTPConnection], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Set-FTPConnection"

     

    Also, It will give like connection failure or Set-FTPConncection cannot call 0 arguments !!

    And if i want to include port value, where should i pass ?

    Not sure, that is causing the issue.

    Please do help me out.

    Thanks

  • #112618

    Participant
    Points: 5
    Rank: Member

    Have you double checked that line.
    The parameter options should not begin with a $.

    $success = Set-FTPConnection -$Server $ftp -$Credentials $cred -$Session MyTestSession -UsePassive
    

    Should be:

    $success = Set-FTPConnection -Server $ftp -Credentials $cred -Session MyTestSession -UsePassive
    
  • #112622

    Participant
    Points: 0
    Rank: Member

    If that is the change:

    i got this err:

    
    Set-FTPConnection : Exception calling "GetResponse" with "0" argument(s): "The underlying connection was
    
    closed: The server committed a protocol violation."
    
    
  • #112624

    Participant
    Points: 5
    Rank: Member

    Well it looks lite it was correct since you got a response this time even though you still have a failure.

    Probably easiest to grab the code from the Set-FTPConnection.ps1 file and try it line by line with your information.
    Just to see where it fails.

    Or use break points if you're familiar with that in your editor.

    Edit: You should probably also check wether SSL is used, self-signed or not and so forth.

    • #112637

      Participant
      Points: 0
      Rank: Member
      
      Function Set-FTPConnection
      
      {
      
      [CmdletBinding(
      
      SupportsShouldProcess=$True,
      
      ConfirmImpact="Low"
      
      )]
      
      Param(
      
      [parameter(Mandatory=$true)]
      
      [Alias("Credential")]
      
      $Credentials,
      
      [parameter(Mandatory=$true)]
      
      [String]$Server,
      
      [Switch]$EnableSsl = $False,
      
      [Switch]$ignoreCert = $False,
      
      [Switch]$KeepAlive = $False,
      
      [Switch]$UseBinary = $False,
      
      [Switch]$UsePassive = $False,
      
      [String]$Session = "DefaultFTPSession"
      
      )
      
       
      
      Begin
      
      {
      
      if($Credentials -isnot [System.Management.Automation.PSCredential])
      
      {
      
      $Credentials = Get-Credential $Credentials
      
      }
      
      }
      
       
      
      Process
      
      {
      
      if ($pscmdlet.ShouldProcess($Server,"Connect to FTP Server"))
      
      {
      
      if(!($Server -match "ftp://"))
      
      {
      
      $Server = "ftp://"+$Server
      
      Write-Debug "Add ftp:// at start: $Server"
      
      }
      
       
      
      Write-Verbose "Create FtpWebRequest object."
      
      [System.Net.FtpWebRequest]$Request = [System.Net.WebRequest]::Create($Server)
      
      $Request.Credentials = $Credentials
      
      $Request.EnableSsl = $EnableSsl
      
      $Request.KeepAlive = $KeepAlive
      
      $Request.UseBinary = $UseBinary
      
      $Request.UsePassive = $UsePassive
      
      $Request | Add-Member -MemberType NoteProperty -Name ignoreCert -Value $ignoreCert
      
      $Request | Add-Member -MemberType NoteProperty -Name Session -Value $Session
      
       
      
      $Request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
      
      Try
      
      {
      
      [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$ignoreCert}
      
      $Response = $Request.GetResponse()
      
      $Response.Close()
      
       
      
      if((Get-Variable -Scope Global -Name $Session -ErrorAction SilentlyContinue) -eq $null)
      
      {
      
      Write-Verbose "Create global variable: $Session"
      
      New-Variable -Scope Global -Name $Session -Value $Request
      
      }
      
      else
      
      {
      
      Write-Verbose "Set global variable: $Session"
      
      Set-Variable -Scope Global -Name $Session -Value $Request
      
      }
      
       
      
      Return $Response
      
      }
      
      Catch
      
      {
      
      Write-Error $_.Exception.Message -ErrorAction Stop
      
      }
      
      }
      
      }
      
       
      
      End{}
      
      }
      
      

       

      Here is my Set-FTPConnection.ps1 file.

      Can you check it and let me know is there anything needs to be modify.

  • #112640

    Participant
    Points: 0
    Rank: Member

    Here is the actual requirement,

     
    I wanted to lock/disable the ftp users by passing 5times bogus password or run the script 5times.
    Before that,
    I want to make sure, whether the connection establishmemt is success or not.
    For that i wrote a script, but it failed.Above is the script i used

    Err with set-ftpconnection.

    Even i cross checked with filezilla,
    I can able to connect for it.
    If i enter wrong passwd 5times, the acc ll get locked.
    Thats what i needed.

    If i use psftp single cmdline with correct password, i can able to access for it.

     1..5 | foreach { psftp user@host -pw password } 

    If i gave wrong password, it ll ask for prompt to correct password.....if i enter wrong password 5 times, acc will get locked.
    How to write script for this process !!??

  • #112651

    Participant
    Points: 65
    Published
    Rank: Member

    Curious to know, Why are you trying to lock the account ?

    • #112654

      Participant
      Points: 0
      Rank: Member

      Hi Kvprasoon,

      Some users don't want to access for their FTP so for time being we do automate this process by scripting.

      Later if they requested, we can enable it on some specific tools.

    • #112655

      Participant
      Points: 65
      Published
      Rank: Member

      Do you think this is the right way to do this ? I don't think so.
      If you organization is having a good IT security team, you will be in trouble.

    • #112687

      Participant
      Points: 0
      Rank: Member

      Yes, even i was not aware of this process.

      I did checked with network and IT team, they said the procedure to automate this.

      They are okay for this.

  • #112690

    Participant
    Points: 0
    Rank: Member

    Hi all, Can anybody help me on this ??

  • #112784

    Participant
    Points: 0
    Rank: Member

    Is there anybody to look into the issue ??

  • #112801

    Participant
    Points: 37
    PublishedHelping Hand
    Rank: Member

    As for this...

    "Set-FTPConnection : A positional parameter cannot be found that accepts argument

    'System.Management.Automation.PSCredential'.

    At C:\Users\mpradeep\OneDrive – Campus Management Corp\Desktop\pf.ps1:9 char:12

    + $success = Set-FTPConnection -$Server $ftp -$Credentials $cred -$Sess …

    This is because that is what you are passing it.

    $user = "Username"
    
    $ppass = "password" # BTW, never do this. Always prompt for the cred.
                        # If you need to resuse, use secure storage, CLIXML, Credentialmanager, etc...
    
    $pass = ConvertTo-SecureString $ppass -Force -AsPlainText # you are cast as plain text, but this is still securestring
                                                              # and you need a plain text password
    
    $cred = New-Object Management.Automation.PSCredential($user,$pass) # then using a securestring, this is a logic effort
                                                                       # and thus duplicate
    
    # Results
    
     $user = "Username"
    
     $user
    Username
    
     $ppass = "password"
    
     $ppass
    password
    
     $pass = ConvertTo-SecureString $ppass -Force -AsPlainText
    
     $pass
    System.Security.SecureString
    
     $cred = New-Object Management.Automation.PSCredential($user,$pass)
    
     $cred
    
    UserName                     Password
    --------                     --------
    Username System.Security.SecureString
    
    

    Just do this

    $cred = Get-Credential -Credential 'testuser'
    
    $cred.GetNetworkCredential().UserName
    $cred.GetNetworkCredential().Password
    
    # Resutls
    
     $cred = Get-Credential -Credential 'testuser'
    
     $cred
    
    UserName                      Password
    --------                      --------
    testuser System.Security.SecureString
    
    
    
     $cred.GetNetworkCredential().UserName
    testuser
    
     $cred.GetNetworkCredential().Password
    password
    
    • #112820

      Participant
      Points: 0
      Rank: Member

      Thanks for your reply.

      The problem is with set-FTPconnection right ??

      And, Where i need to include the script part, which you sent ?

You must be logged in to reply to this topic.