translate CMD FTP request to PowerShell FTP request

女生的网名这么多〃 提交于 2020-01-06 07:17:33

问题


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 of WebClient. 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

回答1:


For DownloadFile, the second parameter must be a filename like "c:\$filespec" and not just a drive specifier.



来源:https://stackoverflow.com/questions/19983593/translate-cmd-ftp-request-to-powershell-ftp-request

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