Reputation: 359
I want to connect with DTCC FTP server to get some credential files.
The program just creates the request file and then retrieves the produced data. Just need a script that does those two things.
$server="ftp 123.123.123.123"
$user="123456"
$passwd="123456"
$data_Password = "1234"
$RequestCode = "ABCD"
$locationCode= "ABCD"
$lastUsedSeqNum = "0560"
# Create file name for RequestFile
$scriptName = "FTX"+$user.Substring(0,4)+".P" + $user.Substring(5) + ".PB101" + $requestCode + "." + $locationCode + $lastUsedSeqNum
$scriptPath = (Resolve-Path .\).Path
Write-Host "Current Path is: "$scriptPath
$ScriptName = $MyInvocation.MyCommand.Nameame
Write-Host "Current running script name is: "$scriptName
$ftpFile=$scriptPath + "\$scriptName.I"
# Create RequestFile
$line1 = " "+"PPASSWD0102"+ " " +$user.Substring(0,4) +"-"+$user.Substring(5)+$data_Password + " " +$RequestCode.Substring(0,4) +$((Get-Date).ToString('HHMM'))+$lastUsedSeqNum
$line2 = "TD" +$RequestCode +"01HDR"
# Create RequestFile
Add-content $ftpFile $line1
Add-content $ftpFile $line2
I want to use powershell to implement this. Above is the code i am currently working on. This is to create a request file.
There is one thing that the scriptname is not working....
And i want to upload this to the server. Then wait for 15 min, to download the file from FTP server. I am really confused about powershell...
Help is always appreciated! thanks.
Upvotes: 0
Views: 1130
Reputation: 32155
I'm confused by one thing. You spend all this effort building your filename:
$scriptName = "FTX"+$user.Substring(0,4)+".P" + $user.Substring(5) + ".PB101" + $requestCode + "." + $locationCode + $lastUsedSeqNum
And then immediately try to overwrite it.
$ScriptName = $MyInvocation.MyCommand.Nameame
I'm not sure what you're trying to do here.
As for your actual question, I'd adapt the script from here.
This script below requires you to reformat your FTP server into a URI format. It will upload the file at $ftpFile
to the location at $ftp
, wait 15 minutes, and then download the same file from the FTP server and save it to $ftpFile
, overwriting it.
# Get a FileInfo object for $ftpFile
$ftpFileInfo = Get-Item -Path $ftpFile
# Be sure to include the complete folder path on the server if there is one and
# also be sure to include the trailing /
$ftp = "ftp://ftp.example.com/dir/"
$user = "user"
$pass = "Pass"
# Build a URI to the FTP destination location
$ftpUri = New-Object -TypeName System.Uri -ArgumentList $ftp
$ftpFileUri = New-Object -TypeName System.Uri -ArgumentList $ftpuri, $ftpFileInfo.Name
# Create the web client
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $user, $pass
Write-Host "Uploading from $($ftpFileInfo.FullName) to $($ftpFileUri.AbsoluteUri)..."
$webclient.UploadFile($ftpFileUri, $ftpFileInfo.FullName)
# Wait 15 minutes
Write-Host "Waiting until $((Get-Date).AddMinutes(15).ToString('HH:mm:ss'))..."
Start-Sleep -Seconds 900
# Download the file at $ftpFileUri and save it to the path for $ftpFileInfo.FullName
Write-Host "Downloading from $($ftpFileUri.AbsoluteUri) to $($ftpFileInfo.FullName)..."
$webclient.DownloadFile($ftpFileUri, $ftpFileInfo.FullName)
Depending on the FTP server's requirements, that may or may not work. You may need to use methods similar to those described here.
Also, keep in mind this method only works for plain FTP. If you're using FTPS or if you're calling it FTP but you really mean SFTP, you'll need to look into something else. WinSCP has a more robust .Net library you can script with PowerShell for use with FTP, FTPS, or SFTP.
Upvotes: 2