I'm trying to download files from *nix with powershell script using WinSCPnet.dll:
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "<srvr_url>"
UserName = "<user>"
Password = "<pass>"
SshHostKeyFingerprint = "ssh-rsa 2048 tw5wuHrOrBUrBIsv3j9HQdJ8stWQzPKI6J881afD5P0="
}
***
$fileInfos =
$session.EnumerateRemoteFiles(
$RemoteDir, "*$FileMask.xml",
([WinSCP.EnumerationOptions]::EnumerateDirectories -bor
[WinSCP.EnumerationOptions]::AllDirectories))
foreach ($fileInfo in $fileInfos)
{
$localFilePath = $fileInfo.Name -replace "[\0\\r\""\*\\a\/\\b\:\t\<\n\>\\v\?\\f\\\|\']+",''
$localFilePath =
[WinSCP.RemotePath]::TranslateRemotePathToLocal(
[WinSCP.RemotePath]::Combine($RemoteDir,$localFilePath), $RemoteDir, $LocalDir)
Write-Host $localFilePath
Write-Host "Downloading file $($fileInfo.FullName)..."
# Download file
$remoteFilePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
$transferResult = $session.GetFiles($remoteFilePath, $localFilePath)
# Did the download succeeded?
if (!$transferResult.IsSuccess)
{
# Print error (but continue with other files)
Write-Host (
"Error downloading file $($fileInfo.FullName): " +
"$($transferResult.Failures[0].Message)")
}
}
and catch
error:
get_adm.ps1 : 'WinSCP.TransferEventArgs.Destination':
Exception when calling "Create" with "2" arguments: "The path contains invalid characters."
line: 1 character: 1
+ <script_name>.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,<script_name>.ps1
...and catch same error if $localFilePath declared as in
example:
$localFilePath =
[WinSCP.RemotePath]::TranslateRemotePathToLocal(
$fileInfo.FullName, $remotePath, $localPath)