WinSCP PowerShell Module

Base functionallity to use the WinSCP Secure FTP Client with PowerShell Cmdlets
2,181 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Executable not valid application for OS
    1 Posts | Last post April 19, 2018
    • Exception calling "Open" with "1" argument(s): "The specified executable is not a valid application for this OS platform."
      At line:1 char:12
      +            $session.Open($sessionOptions)
      +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : Win32Exception
      Could you please advise on why i would receive this error?
  • Invalid Argument on parameter WinSCPSession
    2 Posts | Last post May 18, 2016
    • When trying to upload a file, I'm getting an error on the Send-WinSCPItem CMDlet: "Send-WinSCPItem : Cannot validate argument on parameter 'WinSCPSession'.  The argument is null, empty, or an element of the argument collection contains a null value.  Supply a collection that does not contain any null values and then try the command again."
      A sample of my code is below:
      # Import WinSCP Module if needed
      Import-Module WinSCP
      # Create Credential Object
      $username = "user"                                                                                              
      $password = ConvertTo-SecureString "pwd123" -AsPlainText -Force                                                 
      $credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password 
      #Set Hostname to FTPs site
      $Hostname = ""           
      # Create new WinSCP session using captured credentials.
      $session = New-WinSCPSession -Credential $credential -HostName $Hostname -Protocol Ftp -FtpSecure Implicit
      # Upload a file to the directory.
      Send-WinSCPItem -WinSCPSession $session -Path "C:\file.txt" -Destination "/folder/folder/folder/"
      # Close the session object.
      Remove-WinSCPSession -WinSCPSession $session
      Any direction is appreciated - thank you! :)
    • my first guess after reading this, is remove the 'ftp://' from the host name, and just have it set to ''.  Basically the cmdlet New-WinSCPSession will create the proper connection string.
      also, i would reccommend grabing the latest version from either my GitHub page: or from the PowerShell Gallery:  because i rarely update my stuff here anymore.  (sorry, between github and the gallery, there is just no point, as far as PowerShell goes anyway).
      Let me know if that works.
      Thomas Malkewitz @dotps1
  • Wildcard file retrieval
    4 Posts | Last post September 28, 2015
    • How can we retrieve a specific file type using a wildcard file name?
      Receive-WinSCPItem -WinSCPSession $session -Path "./remote/path/*.xml" -Destination C:\Download
      Receive-WinSCPItem : Cannot find path: ./remote/path/*.xml/ because it does not exist.
      At line:1 char:1
      + Receive-WinSCPItem -WinSCPSession $session -Path "./remote/path/*.xml" -Destinat ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
          + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Receive-WinSCPItem
      From the parameter info, which doesn't seem to work properly...
      .PARAMETER Path
          Full path to remote directory followed by slash and wildcard to select files or subdirectories to download. When wildcard is omitted (path ends with slash), all files and subdirectories in the remote directory are downloaded.
    • I think its an issue with the function allity,
      can you open the following file:
      then delete lines 45-49.  save the module, reimport it, and try again.
      basically, it is testing for /remote/path/*.xml  which doesn't exists because of the *.  please let me know the outcome.  thank you.
    • The error is no longer in PowerShell, but the transfer still fails. From the session log:
      > 2015-09-25 16:34:29.712 Script: get  -nopermissions -preservetime -transfer="binary" -- "./remote/path/*.xml/" "C:\Download\"
      . 2015-09-25 16:34:29.712 Listing directory "/remote/path/*.xml".
      > 2015-09-25 16:34:29.712 Type: SSH_FXP_OPENDIR, Size: 27, Number: 523
      < 2015-09-25 16:34:29.761 Type: SSH_FXP_STATUS, Size: 33, Number: 523
      < 2015-09-25 16:34:29.761 Status code: 2, Message: 523, Server: File not found, Language: en 
      . 2015-09-25 16:34:29.761 Asking user:
      . 2015-09-25 16:34:29.761 Error listing directory './remote/path/*.xml'. ("No such file or directory.
      . 2015-09-25 16:34:29.761 Error code: 2
      . 2015-09-25 16:34:29.761 Error message from server (en): File not found")
      < 2015-09-25 16:34:29.761 Script: Error listing directory './remote/path/*.xml'.
      < 2015-09-25 16:34:29.761 Script: No such file or directory.
      < 2015-09-25 16:34:29.762 Error code: 2
      < 2015-09-25 16:34:29.762 Error message from server (en): File not found
      . 2015-09-25 16:34:29.762 Script: Failed
      I was able to work around it using a combination of another function. I'm definitely curious if there is a better way to do accomplish this kind of wildcard download, but this method appears to work.
      $RemoteXmlFile = Get-WinSCPChildItem -WinSCPSession $session -Path '/remote/path/' -Filter *.xml
      $LocalPath = "C:\Download\"
      ForEach ($file in $RemoteXmlFile) {Receive-WinSCPItem -WinSCPSession $session -Path $file -Destination $LocalPath}
    • Great find, this was actually an issue with my Help Function for formatting the Path to be used by WinSCP.  It is fixed now, you can use Receive-WinSCPItem -Path './remotePath/*.xml'.
      I have moved this thread to GitHub as well, for better issue tracking:
      You can get the latest version from here, GitHub, or the PowerShellGallery.
      Thanks again for all your feed back.
  • SSH key authentication example
    1 Posts | Last post September 25, 2015
    • An example for connections using SSH key authentication with a username only:
      # Add the WinSCP module
      Import-Module WinSCP
      # Configure session options and variables
      $Credential = New-Object System.Management.Automation.PSCredential ("username", (new-object System.Security.SecureString))
      $HostName = ""
      $SshPrivateKeyPath = "C:\path\to\key.ppk"
      $SshHostKeyFingerprint = "ssh-rsa 2048 ho:st:ke:yf:in:ge:rp:ri:nt"
      # Open the SFTP session
      $session = New-WinSCPSession -HostName $HostName -Credential $Credential -SshPrivateKeyPath $SshPrivateKeyPath -SshHostKeyFingerprint $SshHostKeyFingerprint
  • sFTP issue with HostKey
    3 Posts | Last post April 28, 2015
    • I can't seem to get GiveUpSecurityAndAcceptAnySshHostKey to work with the sFTP protocol option. It still forces me to enter the SshHostKeyFingerprint option. If the GiveUpSecurityAndAcceptAnySshHostKey is used I would suggest the HostKey option not be required.
    • Great find.  I have fixed this and will be avaialbe in the next release, however, i am implimenting quite a few other things as well, so it will be a few days before it will be released.
      I have opened a bug for this on the GitHub site for this project.
      Thanks again and i will update this post and the GitHub Issue when is released.
      Also, you may want to get my modules from my GitHub page, as it is harder to keep this gallery updated.
    • ok, finished quicker then i planed :)  should be all set, thanks.