在 PowerShell 中发送任意 FTP 命令
Send arbitrary FTP commands in PowerShell
我一直在使用 PSFTP Michal Gajda 的模块做很多事情
直到我想发送任意命令,例如:
quote SITE LRECL=132 RECFM=FB
or
quote SYST
我发现这无法使用 FTPWebRequest
实现,只能通过第三方 FTP 实现。
我想问一下与 PowerShell 兼容的最佳开源 FTP 实现是什么?
您可以使用 WinSCP .NET assembly used from PowerShell using the Session.ExecuteCommand
method 发送任意 FTP 命令:
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "password"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Execute command
$session.ExecuteCommand("SITE LRECL=132 RECFM=FB").Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
还有 PowerShell module 构建在 WinSCP .NET 程序集之上,您可以像这样使用它:
$session = New-WinSCPSessionOptions -Protocol Ftp -Hostname example.com -Username user -Password mypassword | Open-WinSCPSession
Invoke-WinSCPCommand -WinSCPSession $session -Command "SITE LRECL=132 RECFM=FB"
(我是WinSCP的作者)
我一直在使用 PSFTP Michal Gajda 的模块做很多事情
直到我想发送任意命令,例如:
quote SITE LRECL=132 RECFM=FB
or
quote SYST
我发现这无法使用 FTPWebRequest
实现,只能通过第三方 FTP 实现。
我想问一下与 PowerShell 兼容的最佳开源 FTP 实现是什么?
您可以使用 WinSCP .NET assembly used from PowerShell using the Session.ExecuteCommand
method 发送任意 FTP 命令:
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "password"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Execute command
$session.ExecuteCommand("SITE LRECL=132 RECFM=FB").Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
还有 PowerShell module 构建在 WinSCP .NET 程序集之上,您可以像这样使用它:
$session = New-WinSCPSessionOptions -Protocol Ftp -Hostname example.com -Username user -Password mypassword | Open-WinSCPSession
Invoke-WinSCPCommand -WinSCPSession $session -Command "SITE LRECL=132 RECFM=FB"
(我是WinSCP的作者)