Run a script via FTP connection from PowerShell

房东的猫 提交于 2021-02-08 08:23:10

问题


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.

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

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.

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


回答1:


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

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:

  • rename files using FTP; or
  • delete only the files that were downloaded.

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.


Or you can do it like:

  • 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.

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



来源:https://stackoverflow.com/questions/50489730/run-a-script-via-ftp-connection-from-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!