Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Error uploading file with square brackets in the file name

Open RobertH-Geberit opened this issue 5 years ago • 7 comments

I used Posh-SSH 2.2 in Powershell 4 on Windows Server 2012R2 to upload files from a local file share to a remote system file import hotfolder.

The command used to upload the file is: $Result = Set-SFTPItem -SessionId ($ThisSession).SessionId -Path $_.FullName -Destination $DestFolder

When I upload a file called "ASO_149178 Con_DK-da Sortimentsoversigt – Geberit Mepla kompatibilitet [1] og [2].pdf" I get the following error:

Set-SFTPFile : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
At E:\Scripts\Overview-PDF-to-STEP\Upload-PDF-files-Test.ps1:37 char:12
+     $Result = Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $_.FullNa ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-SFTPFile], ArgumentOutOfRangeException
    + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,SSH.SetSftpFile

When I rename the file so that the square brackets are changed to rounded brackets, the file uploads normally: "ASO_149178 Con_DK-da Sortimentsoversigt – Geberit Mepla kompatibilitet (1) og (2).pdf"

The error indicates that somehow the filename is being evaluated as code? This seems like there is a code injection issue. I did not test the 3.0-alpha yet as I would need to have Powershell upgraded on the machine.

I can upload the file manually with an SFTP client without errors. The filename is important in this case as the receiving system will use the filename to update an asset.

BR Robert

RobertH-Geberit avatar Feb 24 '21 10:02 RobertH-Geberit

I now rename the file before uploading and it gets renamed back afterwards. Rename-Item only works for this file if I use -LiteralPath rather than -Path.

Supporting LiteralPath in PoshSSH would it seems also fix the issue.

RobertH-Geberit avatar Mar 01 '21 07:03 RobertH-Geberit

Same issue, when using brackets in a file name the following error is generated:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Test code

#region Create SFTP credential
Write-Verbose 'Create SFTP credential'

$params = @{
    String      = 'xxx'
    AsPlainText = $true
    Force       = $true
    ErrorAction = 'Stop'
}
$sftpPassword = ConvertTo-SecureString @params

$params = @{
    TypeName     = 'System.Management.Automation.PSCredential'
    ArgumentList = 'sftpUser', $sftpPassword
}
$sftpCredential = New-Object @params
#endregion

$params = @{
    ComputerName      = 'localhost'
    Credential        = $sftpCredential
    Port              = 22
    ConnectionTimeout = 60
    AcceptKey         = $true
    Force             = $true
    Verbose           = $false
}

$sessionParams = New-SFTPSession @params


$params = @{
    Path        = 'T:\Test file [with brackets].txt'
    Destination = '\'
    Force       = $true
}
Set-SFTPItem @sessionParams @params

@darkoperator thank you for having another look at this.

DarkLite1 avatar Oct 03 '25 11:10 DarkLite1

Indeed. The funny thing is that for downloads this not an issue, so the logic there is already correct. But not for uploads it seems.

DarkLite1 avatar Oct 06 '25 07:10 DarkLite1

nope 👎

PS > Get-SFTPItem -Path "/home/xxx" -Destination "D:\Work" -SFTPSession $sftp -Verbose
VERBOSE: Downloading /home/xxx
PS > Get-SFTPItem -Path "/home/xxx" -Destination "D:\Test folder [with brackets]" -SFTPSession $sftp -Verbose
Get-SFTPItem: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

MVKozlov avatar Oct 06 '25 07:10 MVKozlov

Oh sorry, I'm incorrect. We use another script for transferring files too, and I mixed that up. Sorry about that.

DarkLite1 avatar Oct 06 '25 07:10 DarkLite1

And apparently still a problem. Running in Windows 11, powershell 7, and uploading files to a Linux distro

aersida avatar Feb 09 '26 04:02 aersida