Reputation: 3141
I have the following FTP request that is saved as a CMD script. It works. I want to write an FTP request in PowerShell that does the same thing. In order for the PowerShell script to work, I need to specify the address of the remote file I want to get.
Here is the CMD script. From looking at this, can you tell what the file path is? FYI in case this is important: this is being pulled from an AS400.
@echo off
setlocal
set uname=USERNAME
set passw=USERPASS
set hostname=AS400NAME
set filespec=SPECNAME
echo %uname%> TEST.ftp
echo %passw%>> TEST.ftp
echo cd DIRECTORYNAME>> 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
Upvotes: 1
Views: 51
Reputation: 24340
You would need to reassemble the key variables from that script to build the new string in PowerShell. I believe you have the required pieces in the provided script:
$uname = "USERNAME"
$passw = "USERPASS"
$hostname = "AS400NAME"
$filespec = "SPECNAME"
$dir = "DIRECTORYNAME"
You just need to piece those together to build a valid FTP URI:
$uri = "ftp://$uname`:$passw@$hostname/$dir/$filespec"
In PowerShell, strings that are wrapped in double quotes will internally expand variables. It makes for a much cleaner string as compared to successive concatenations.
The last step would be to retrieve the file. I would recommend using the WebClient class:
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($uri, "C:\temp\$filespec")
Upvotes: 1