通过PowerShell的FTP连接运行脚本 [英] Run a script via FTP connection from PowerShell

查看:66
本文介绍了通过PowerShell的FTP连接运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个脚本,该脚本可以执行一项非常基本的任务,它可以连接到远程FTP站点,检索XML文件,然后将其删除.

I have made a script that does a really basic task, it connects to a remote FTP site, retrieves XML files and deletes them afterward.

唯一的问题是,过去我们丢失了文件,因为它们是在运行delete语句时添加的.

The only problem is that in the past we lost files because they were added when the delete statement was run.

open ftp.site.com
username
password
cd Out
lcd "E:\FTP\Site"
mget *.XML
mdel *.XML

bye

为防止这种情况发生,我们想在FTP服务器上放置一个脚本( rename-files.ps1 ).该脚本会将 *.xml 文件重命名为 *.xml.copy .

To prevent this from happening, we want to put a script on the FTP server (rename-files.ps1). The script will rename the *.xml files to *.xml.copy.

唯一的是我不知道如何通过FTP连接运行脚本.

The only thing is I have no clue how to run the script through my FTP connection.

推荐答案

某些但很少的FTP服务器支持 SITE EXEC 命令.如果您的FTP服务器确实很少支持它,则可以使用:

Some, but very few, FTP servers support SITE EXEC command. If the very rare case that your FTP server does support it, you can use:

quote SITE EXEC powershell rename-files.ps1

尽管在大多数情况下,如果FTP是访问服务器的唯一方法,则您无法在FTP服务器上执行任何操作.您将不得不使用另一种方法来执行脚本,例如SSH,PowerShell Remoting等.

Though, in most cases, you cannot execute anything on the FTP server, if FTP is the only way you can access the server. You would have to use another method to execute the script, like SSH, PowerShell Remoting, etc.

但是您的问题还有其他解决方案:

But your problem has other solutions:

  • 使用FTP重命名文件;或
  • 仅删除下载的文件.

两者都是可行的,但是您将需要比Windows ftp.exe 更好的FTP客户端.
例如,请参见用于下载,重命名和移动文件的WinSCP脚本.

Both are doable, but you will need better FTP client than Windows ftp.exe.
See for example A WinSCP script to download, rename, and move files.

或者您可以这样做:

  • 运行 ftp.exe 一次以检索文件列表;
  • 在PowerShell中处理列表以使用 get del 命令为特定文件生成一个 ftp 脚本(不带通配符);
  • 使用生成的脚本再次运行 ftp.exe .
  • Run ftp.exe once to retrieve list of files;
  • Process the list in PowerShell to generate an ftp script with get and del commands for specific files (without wildcard);
  • Run ftp.exe again with generated script.

实际上,您不需要在PowerShell中使用 ftp.exe ..NET程序集具有自己的FTP实现: <代码> FtpWebRequest .

Actually you do not need to use ftp.exe in PowerShell. .NET assembly has its own FTP implementation: FtpWebRequest.

这篇关于通过PowerShell的FTP连接运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆