"Unable to find type [WinSCP.RemotePath]" 从 PowerShell 使用 WinSCP .NET 程序集时
"Unable to find type [WinSCP.RemotePath]" when using WinSCP .NET assembly from PowerShell
我正在尝试使用 WinSCP .NET 程序集下载最后修改的文件作为 ETL 过程的一部分。我被困住了,因为我对 Powershell 脚本编写知之甚少。
这是我目前在 Windows 10 PowerShell 中尝试过的方法:
param (
$localPath = "C:\download\*",
$remotePath = "/home/folder/"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "xxx.xxx.xxx.xxx"
UserName = "XXXXXXXX"
Password = "xxxxxxx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)
# Select the most recent file
$latest =
$directoryInfo.Files |
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}
# Download the selected file
$session.GetFiles(
[WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
}
我一直在尝试调试它并注意到它一直运行到 $session.GetFiles
调用,输出以下错误:
Error: Unable to find type [WinSCP.RemotePath].
我不知道为什么会这样。
这只是因为模块真的没有被看到。
尝试使用 MS PowerShellGallery 中的包装器作为此工作的一部分进行验证或代替您正在使用的包装器。
Find-Module -Name WinScp* | ft -a
Version Name Repository Description
------- ---- ---------- -----------
5.13.7.0 WinSCP PSGallery PowerShell Module Wrapper for WinSCP.
即使在 WinSCP 站点上使用它的说明,它也与您在此处显示的略有不同:
https://winscp.net/eng/docs/library_powershell#loading
Loading Assembly PowerShell script needs to load the assembly before
it can use classes the assembly exposes. To load assembly use Add-Type
cmdlet
Add-Type -Path "WinSCPnet.dll"
Had you need to run the script from other directory, you need to
specify a full path to the assembly. You can derive the path from the
script file path using $PSScriptRoot automatic variable:5
Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
If you are writing a script that you plan to use as a WinSCP extension
(a custom command), you can use the copy of the assembly installed
with WinSCP. In that case you can use the WINSCP_PATH environment
variable to resolve the path to the assembly. To allow the script run
even outside of WinSCP, you should fall back to the $PSScriptRoot
approach (as above), if the variable is not defined:
$assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot }
Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
在您的调试和验证工作中。尝试将引用放在脚本的最顶部。
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
param (
$localPath = "C:\download\",
$remotePath = "/home/folder/"
)
...
当您遇到 WinSCP.RemotePath
的问题而不是早期使用的 WinSCP 类 如 WinSCP.SessionOptions
或 WinSCP.Session
时,您很可能使用的是旧版本的 WinSCP还没有 WinSCP.RemotePath
class 的 .NET 程序集。
确保更新到程序集的最新版本。
我正在尝试使用 WinSCP .NET 程序集下载最后修改的文件作为 ETL 过程的一部分。我被困住了,因为我对 Powershell 脚本编写知之甚少。
这是我目前在 Windows 10 PowerShell 中尝试过的方法:
param (
$localPath = "C:\download\*",
$remotePath = "/home/folder/"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "xxx.xxx.xxx.xxx"
UserName = "XXXXXXXX"
Password = "xxxxxxx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)
# Select the most recent file
$latest =
$directoryInfo.Files |
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}
# Download the selected file
$session.GetFiles(
[WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
}
我一直在尝试调试它并注意到它一直运行到 $session.GetFiles
调用,输出以下错误:
Error: Unable to find type [WinSCP.RemotePath].
我不知道为什么会这样。
这只是因为模块真的没有被看到。
尝试使用 MS PowerShellGallery 中的包装器作为此工作的一部分进行验证或代替您正在使用的包装器。
Find-Module -Name WinScp* | ft -a
Version Name Repository Description
------- ---- ---------- -----------
5.13.7.0 WinSCP PSGallery PowerShell Module Wrapper for WinSCP.
即使在 WinSCP 站点上使用它的说明,它也与您在此处显示的略有不同:
https://winscp.net/eng/docs/library_powershell#loading
Loading Assembly PowerShell script needs to load the assembly before it can use classes the assembly exposes. To load assembly use Add-Type cmdlet
Add-Type -Path "WinSCPnet.dll"
Had you need to run the script from other directory, you need to specify a full path to the assembly. You can derive the path from the script file path using $PSScriptRoot automatic variable:5
Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
If you are writing a script that you plan to use as a WinSCP extension (a custom command), you can use the copy of the assembly installed with WinSCP. In that case you can use the WINSCP_PATH environment variable to resolve the path to the assembly. To allow the script run even outside of WinSCP, you should fall back to the $PSScriptRoot approach (as above), if the variable is not defined:
$assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot }
Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
在您的调试和验证工作中。尝试将引用放在脚本的最顶部。
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
param (
$localPath = "C:\download\",
$remotePath = "/home/folder/"
)
...
当您遇到 WinSCP.RemotePath
的问题而不是早期使用的 WinSCP 类 如 WinSCP.SessionOptions
或 WinSCP.Session
时,您很可能使用的是旧版本的 WinSCP还没有 WinSCP.RemotePath
class 的 .NET 程序集。
确保更新到程序集的最新版本。