将CMD FTP请求转换为PowerShell FTP请求 [英] translate CMD FTP request to PowerShell FTP request
问题描述
我有一个完整的FTP请求保存到.CMD文件。
这是我的CMD脚本:
@echo off
setlocal
set uname = exUsername
set passw = exPassword
set hostname = exHostname
set filespec = exSpec
echo%uname%> test.ftp
echo%passw%>> test.ftp
echo pwd>> test.ftp
echo cd exDir>> test.ftp
echo binary>> test.ftp
echo get%filespec%>> test.ftp
echo bye>> test.ftp
ftp -s:test.ftp%hostname%
if errorlevel 1 pause
endlocal
我要将上述代码翻译成PowerShell脚本。
这是我的PowerShell脚本:
$ uname =exUsername
$ passw =exPassword
$ hostname =exHostname
$ filespec = exSpec
$ dir =exDir
$ uri =ftp:// $ uname`:$ passw @ $ hostname / $ dir / $ filespec
$ wc = New-Object System.Net.WebClient
$ wc.DownloadFile($ uri,C:\)
我的PowerShell脚本到目前为止不工作。这是我的错误:
异常调用具有2参数的DownloadFile:异常
a我们bClient请求。 at line:20 char:17
+ $ wc.DownloadFile<<< ($ uri,C:\)
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:DotNetMethodException
我最初认为我指定了错误的FTP地址,所以我将 $ uri
的值输入到Windows资源管理器并进入服务器,但是注意,没有可用的文件夹可供访问。
但是,完全相同的 $ uri
文件路径
有关我错误地点的任何想法吗?
EDIT - 根据@KeithHill的要求,我重新编写了脚本,使用 ftpWebRequest
而不是 WebClient
。代码如下,但我得到以下错误:
异常调用GetResponse参数为0 remote
服务器返回错误:(431)431请求的安全机制不是
此时可用。 at line:23 char:39
+ $ ftpresponse = $ ftprequest.GetResponse<<< ()
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:DotNetMethodException术语Write-Out不能识别为cmdlet函数
脚本的名称文件或可操作程序。检查名称的拼写,或
(如果包含路径),请验证路径是否正确,然后重试
。
以下代码:
$ uname =exUsername
$ passw =exPassword
$ hostname =exHostname
$ filespec =exSpec
$ dir =exDir
$ uri =exUri
#创建一个FTPWebRequest对象来处理与FTP服务器的连接
$ ftprequest = [System.Net.FtpWebRequest] :: Create($ uri)
#设置请求的验证连接的网络凭据
$ ftprequest.Credentials = New-Object System.Net.NetworkCredential($ uname,$ passw)
#将FTPWebRequest方法设置为ListDirectory
$ ftprequest .Method = [System.Net.WebRequestMethods + Ftp] :: ListDirectory
$ ftprequest.EnableSsl = $ True
$ ftprequest.UseBinary = $ True
$ ftprequest.UsePassive = $ True
$ ftprequest.KeepAlive = $ False
$ ftpresponse = $ ftprequest.GetResponse()
Write-Out $ ftpresponse.StatusCode
Write-Out $ ftpresponse .StatusDescription
解决方案
DownloadFile
,第二个参数必须是像c:\ $ filespec
的文件名,而不仅仅是驱动器说明符。I have a fully-working FTP request saved to a .CMD file.
Here is my CMD script:
@echo off setlocal set uname=exUsername set passw=exPassword set hostname=exHostname set filespec=exSpec echo %uname%> test.ftp echo %passw%>> test.ftp echo pwd>> test.ftp echo cd exDir>> test.ftp echo binary>> test.ftp echo get %filespec%>> test.ftp echo bye>> test.ftp ftp -s:test.ftp %hostname% if errorlevel 1 pause endlocal
I want to translate the above code to a PowerShell script.
Here is my PowerShell script:
$uname = "exUsername" $passw = "exPassword" $hostname = "exHostname" $filespec = "exSpec" $dir = "exDir" $uri = "ftp://$uname`:$passw@$hostname/$dir/$filespec" $wc = New-Object System.Net.WebClient $wc.DownloadFile($uri, "C:\")
My PowerShell script so far does not work. Here is my error:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a We bClient request." At line:20 char:17 + $wc.DownloadFile <<<< ($uri, "C:\") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
I initially thought that I am specifying the wrong FTP address, so I entered the
$uri
value into Windows explorer and got onto the server, but noticed that there were no (visible) folders available to access.However, the exact same
$uri
filepath is getting me the requested file thru the CMD script.Any ideas about where I'm going wrong?
EDIT - at the request of @KeithHill , I reworked the script to use
ftpWebRequest
instead ofWebClient
. Code is below, but I am getting the following error:Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (431) 431 Requ ested security mechanism not available at this time. ." At line:23 char:39 + $ftpresponse = $ftprequest.GetResponse <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException The term 'Write-Out' is not recognized as the name of a cmdlet, function, script file, or operable program . Check the spelling of the name, or if a path was included, verify that the path is correct and try again .
Code below:
$uname = "exUsername" $passw = "exPassword" $hostname = "exHostname" $filespec = "exSpec" $dir = "exDir" $uri = "exUri" # Create an FTPWebRequest object to handle the connection to the FTP server $ftprequest = [System.Net.FtpWebRequest]::Create($uri) # Set the request's network credentials for an authenticated connection $ftprequest.Credentials = New-Object System.Net.NetworkCredential($uname,$passw) # Set FTPWebRequest method to ListDirectory $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory $ftprequest.EnableSsl = $True $ftprequest.UseBinary = $True $ftprequest.UsePassive = $True $ftprequest.KeepAlive = $False $ftpresponse = $ftprequest.GetResponse() Write-Out $ftpresponse.StatusCode Write-Out $ftpresponse.StatusDescription
解决方案For
DownloadFile
, the second parameter must be a filename like"c:\$filespec"
and not just a drive specifier.这篇关于将CMD FTP请求转换为PowerShell FTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!