locked
False Power Shell Error RRS feed

  • Question

  • I am having a problem with getting a false error with the script below. The false error is random and I am looking for help to determine why the error is happening. Below is a sample of a false and real error.

    False Error

    "This file was the last file to process:  at"

    What a real\legit error looks like:

    "This file was the last file to process: \\jcc.jcc.com\omafiles\SPC-ProductionDatabases\Mailstream\1.66750400 14023 RPT10.pdf at 10/10/2014 09:27:25"

    $file=Get-ChildItem -path \\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\* -Include *.pdf |
        Sort LastWriteTime -desc |
        Select-Object -first 1
        Where-Object{ $_.LastWriteTime -gt ([DateTime]::Now.AddHours(-14)) } 
    
    if ($file.LastWriteTime -gt ([DateTime]::Now.AddHours(-14))){
        # file newer than 10     
    }else{ 
        # file older than 10
       $mailprops=@{
            SmtpServer='1.2.2.15'
            From='ELECTRONIC_FOLDER@jcc.com'
          
            To='list-adf@jcc.com'
            Subject='Please check the Electronic Process'
    	}
        $body="This file was the last file to process: $($file.Fullname) at $($file.LastWriteTime)"
        Send-MailMessage @mailprops -Body $body
    }

    • Moved by Bill_Stewart Friday, November 28, 2014 5:11 PM This is not "rewrite script for me" forum
    Monday, October 20, 2014 9:09 PM

Answers

  • Not a false error in fact it is not an error at all. It is just the message that you wrote in your script when it process null values.

    I recommend fixing the script so that it does what you want with null values.

    The problem is one of a lack of understanding when you get nothing back and that nothing will not have a file name.

    To state it simply - if the file does not exist you will always execute the "ELSE" clause with a null file name.

    You have a design problem. You have to figure out what to do if NO file is found.


    ¯\_(ツ)_/¯


    • Proposed as answer by Bill_Stewart Thursday, October 23, 2014 1:22 AM
    • Edited by jrv Thursday, October 23, 2014 1:56 AM
    • Marked as answer by Just Karl Friday, May 1, 2015 9:54 PM
    Monday, October 20, 2014 9:51 PM