this is what the comdata_download.ps1 file has in it:
$ftpPath = "
ftp://XX.XX.XX.XX/ecb"
$ftpUser = "user"
$ftpPass = "password"
$localPath = 'c:\temp\'
# get the file listing of the FTP folder
function Get-FtpDir ($url, $credentials)
{
$request = [Net.FtpWebRequest]::Create($url)
if ($credentials) { $request.Credentials = $credentials }
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
# Don't want Binary, Keep Alive unecessary.
$request.UseBinary = $False
$request.KeepAlive = $False
$response = $request.GetResponse()
$responseStream = $response.GetResponseStream()
# Create a nice Array of the detailed directory listing
$streamReader = New-Object System.IO.Streamreader $responseStream
$dirListing = (($streamReader.ReadToEnd()) -split [Environment]::NewLine)
$streamReader.Close()
# Remove first two elements ( . and .. ) and last element (\n)
$dirListing = $dirListing[2..($dirListing.Length-2)]
# Close the FTP connection so only one is open at a time
$response.Close()
# This array will hold the final result
$fileTree = @()
# Loop through the listings
foreach ($curLine in $dirListing) {
# Split line into space separated array
$lineTok = ($curLine -split '\ +')
# Get the filename (can even contain spaces)
$curFile = $lineTok[8..($lineTok.Length-1)]
$curFile = $curFile.Trim()
# Figure out if it's a directory. Super hax.
$dirBool = $LineTok[0].StartsWith("d")
# Determine what to do next (file or dir?)
If($curFile){
# Add the output to the file tree
$fileTree += ,"$curFile"
}
}
Return $fileTree
}
Write-output "Starting..."
try{
# Needed for the download
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)
$webclient.BaseAddress = $ftpPath
$files = Get-FTPDir $ftpPath $webclient.Credentials
Write-output "Files found: "$files.Length
foreach( $file in $files){
#Write-output $file
$dir, $filePart = $file.split('/',2)
$localFile = $localPath+$filePart+$('.txt')
Write-output "Checking if file exists: " + $localFile
if(![System.IO.File]::Exists($localFile)){
$webClient.DownloadFile($file, $localFile)
Write-output "Downloading: " + $file + " to " + $localFile
}
}
Write-output "Done."
}
catch{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-output "Error: " + $ErrorMessage + " Item: " + $FailedItem
Send-MailMessage -From x@email.com -To y@email.com -Subject "Comdata Download Failed!" -SmtpServer server.com -Body "Comdata FTP Script failed on: $FailedItem. The error message was $ErrorMessage"
Break
}
when I changed the powershell script to execute to:
. "c:\scripts\comdata_download.ps1" | "c:\scripts\logs\comdata_%Timestamp:yyyy_MM_ddTHH_mm_ss%
.txt"
I get this error:
At line:1 char:39
2016-11-28 06:20:00,537 ERROR ProcessTrigger [] stderr: + . "c:\scripts\comdata_download.ps1" | "c:\scripts\logs\comdata_2016_11_28T06_20_ ...
2016-11-28 06:20:00,537 ERROR ProcessTrigger [] stderr: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-11-28 06:20:00,537 ERROR ProcessTrigger [] stderr: Expressions are only allowed as the first element of a pipeline.
2016-11-28 06:20:00,537 ERROR ProcessTrigger [] stderr: + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
2016-11-28 06:20:00,537 ERROR ProcessTrigger [] stderr: + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline