Credentials issue using Add-FTPItem function

Hi all.

I needed to write a script to upload file to a FTP server and
from what I read, the PSFTP module was recommended. So I
downloaded and installed it.

I then proceeded to write my script and that's when I ran into
my problem which I can't seem to figure out where it lies, but
I have a credentials issue when using the Add-FTPItem.

So I wrote another (simpler) script to test and have the same
issue in the following code. Import-Module PSFTP $FtpServer = 'ftp.site.com' $User = 'userid' $Password = ConvertTo-SecureString 'password' -AsPlainText -Force $FtpCredentials = New-Object System.Management.Automation.PSCredential ($User, $Password) Set-FTPConnection -Credentials $FtpCredentials -Server $FtpServer -Session MyFtpSession `
-UsePassive $FtpSession = Get-FTPConnection -Session MyFtpSession $FileSize = 0 $FileSize = Get-FTPItemSize -Path "/import.xxx" -Session $FtpSession
Get-FTPItem -Path "/import.xxx" -LocalPath "c:\temp" -Session $FtpSession -Overwrite
Add-FTPItem -Path "/" -LocalPath "c:\temp\test.txt" -Session $FtpSession Both the Get-FTPItemSize and Get-FTPItem function calls work
properly, but the Add-FTPItem generates the following error
messages: Exception setting "Credentials": "Value cannot be null. Parameter name: value" At C:\Documents and Settings\lamothed\My Documents\WindowsPowerShell\Modules\PSFTP
\Add-FTPItem.ps1:150 char:18 + $Request. <<<< Credentials = $CurrentSession.Credentials + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyAssignmentException Exception calling "GetRequestStream" with "0" argument(s): "The remote server returned an error:
(550) File unavailable (e.g., file not found, no access)." At F:\home\dlamothe\Upload.ps1:11 char:12 + Add-FTPItem <<<< -Path "/" -LocalPath "c:\temp\test.txt" -Session $FtpSession + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-FTPItem I've tried different ftp sites (both internal and external) to
make sure it wasn't the site. I've tested this code in both
version 4 (on Windows 7) and version 2 (on Windows 7,
XP mode) with identical results.

Any help would be appreciated. Thanks.
  • Hi, did you find the solution for this ? I'm running into the same issue and getting a little bit crazy here ;)
    Thanks,
    Guy
    (guydeschepper @ hotmail dot com)
  • In reply to steurm:

    I do not have an FTP server to play with, but IMHO it is the way the credentials
    are being populated.

    this...

    $User = 'userid'
    $Password = ConvertTo-SecureString 'password' -AsPlainText -Force
    $FtpCredentials = New-Object System.Management.Automation.PSCredential ($User, $Password)

    is really over kill and not really a secure way of doing this. You should never embed password in plain text in a script.

    One should be doing

    If internal auth and on in an ADDS domain this will throw a normal Windows logon dialog pre-populated with the
    domain and username string and prompt for the password

    $FtpCredentials = Get-Credential -Credential 'contoso\testuser'

    Or

    $FtpCredentials = Get-Credential -Credential 'testuser'

    The you can just use $FtpCredentials as shown
    Set-FTPConnection -Credentials $FtpCredentials



    you view the pieces
    ADDS

    $FtpCredentials.UserName
    $FtpCredentials.Password

    Result

    UserName Password
    -------- --------
    contoso\testuser System.Security.SecureString

    contoso\testuser
    System.Security.SecureString


    Non-Domain

    $FtpCredentials
    $FtpCredentials.UserName
    $FtpCredentials.Password

    UserName Password
    -------- --------
    testuser System.Security.SecureString

    testuser
    System.Security.SecureString

    Also remember that the variables have scope. They have to be visible to all requests inside and outside of functions and modules.