使用Powershell重命名FTP服务器上的文件

时间:2019-05-20 11:26:32

标签: shell powershell ftp powershell-v3.0 powershell-remoting

我是PowerShell的新手,我正在尝试使用Powershell在FTP服务器上重命名文件。下面是提供的代码,但是当我尝试运行时遇到异常。

$FTPResponse = [System.Net.FtpWebResponse] $FTPRequest.GetResponse()
Exception calling "GetResponse" with "0" argument(s): "Unable to connect to the remote server".

我不确定为什么我无法连接,我已经给出了正确的凭据,并且通常可以登录到服务器,但是在运行此代码时却遇到异常。

$uri =  "ftp://XXX.XX.XXX.XXX/xyz.csv" 
$FTPRequest = [System.Net.FtpWebRequest]::Create($uri)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential('abc', 'xyz')
$FTPRequest.KeepAlive = $true
$FTPRequest.UsePassive = $true
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::Rename
$FTPRequest.RenameTo = "/zz.csv"
$FTPRequest.UseBinary = $true
$FTPResponse = [System.Net.FtpWebResponse] $FTPRequest.GetResponse()

我正在追随异常

Exception calling "GetResponse" with "0" argument(s): "Unable to connect to the remote server"
At line:9 char:1
+ $FTPResponse = [System.Net.FtpWebResponse] $FTPRequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

0 个答案:

没有答案