Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.2k views
in CompleteFTP by (120 points)
trying to execute powershell command on time event:

. "c:\scripts\comdata_download.ps1" | out-file "c:\scripts\logs\comdata_%Timestamp:yyyy_MM_ddTHH_mm_ss% .txt"

 

Get the error of illegal character on out-file.  I want to pipe my powershell write commands to an external log file.

2016-11-27 17:20:00,040 DEBUG Event.ProcessTrigger [Server.1] Piping process's stdin for rule Comdata Test (4):
2016-11-27 17:20:00,040 DEBUG Event.ProcessTrigger [Server.1] . "c:\scripts\comdata_download.ps1" | out-file "c:\scripts\logs\comdata_2016_11_27T17_20_00.txt"
2016-11-27 17:20:00,040 DEBUG Event.ProcessTrigger [Server.1]
2016-11-27 17:20:00,040 DEBUG Event.ProcessTrigger [Server.1]
2016-11-27 17:20:00,040 DEBUG Event.ProcessTrigger [Server.1]
2016-11-27 17:20:00,040 DEBUG Event.ProcessTrigger [Server.1]
2016-11-27 17:20:00,727 ERROR Event.ProcessTrigger [] stderr: out-file : Illegal characters in path.
2016-11-27 17:20:00,727 ERROR Event.ProcessTrigger [] stderr: At line:1 char:39
2016-11-27 17:20:00,727 ERROR Event.ProcessTrigger [] stderr: + . "c:\scripts\comdata_download.ps1" | out-file "c:\scripts\logs\comdata_2016_11_ ...
2016-11-27 17:20:00,727 ERROR Event.ProcessTrigger [] stderr: +                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-11-27 17:20:00,727 ERROR Event.ProcessTrigger [] stderr:     + CategoryInfo          : OpenError: (:) [Out-File], ArgumentException
2016-11-27 17:20:00,727 ERROR Event.ProcessTrigger [] stderr:     + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
2016-11-27 17:20:00,742 INFO Event.ProcessTrigger [Server.1] Finished process for rule Comdata Test (4) after 0.703 seconds
by (2.7k points)
It seems fairly clear that Powershell is having trouble parsing a path that's on the first line of the script.  Can you please paste the full script that you're trying to execute into your reply?
by (120 points)
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
by (2.7k points)
The command that you're trying to execute is more like a batch command.  I think you might need to use the Invoke-Expression cmdlet.  Try something like this:

Invoke-Expression "c:\scripts\comdata_download.ps1 | c:\scripts\logs\comdata_%Timestamp:yyyy_MM_ddTHH_mm_ss%
.txt"
by (120 points)
Thanks! This worked:
Invoke-Expression "c:\scripts\comdata_download.ps1 | out-file c:\scripts\logs\comdata_%Timestamp:yyyy_MM_ddTHH_mm_ss%.txt"

Pipes all ps write-output to the cmdlet out-file.

Please log in or register to answer this question.

Categories

...