0

I am using powershell to process csv files in a directory when no file found with current date stamp I want the process to raise an error notifying file not found and exit.

# Powershell raise error and exit
# File name: sale_2020_02_03.csv

$getLatestCSVFile = Get-ChildItem -Path $Folder -Filter "*.csv" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($getLatestCSVFile)
{
    try
    {
        # Process .csv file
    }
    catch
    {
        # on error
        $ErrorMessage = $_.Exception.Message
        throw "$ErrorMessage"
    }
}
else
{
    # If current date file not found raise error and exit
    Send-MailMessage
    throw "File not found"
}
pbj
  • 663
  • 1
  • 8
  • 19
  • What is the problem (the current actual behavior)? – iRon Feb 04 '20 at 14:18
  • Hi @iRon, I would like to have the powershell script to stop execution if the code gets into the else block in the else block I want to notify file not found via an email and stop execution. – pbj Feb 05 '20 at 04:14
  • Just send the mail like: `Send-MailMessage -From 'User01 ' -To 'User02 ' -Subject 'Test mail -Body 'File not found'`, for details see: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7. If it doesn't work as expected, please add that detail to the **question**. – iRon Feb 05 '20 at 07:00

1 Answers1

0

As for this...

I would like to have the powershell script to stop execution

... that is what the Exit keyword is for or the '-ErrorAction Stop' option is for.

As for this...

if the code gets into the else block in the else block I want to notify file not found

... as per my above, same thing, and you have to set that.

Meaning stuff like ...

Clear-Host
$Error.Clear()
try
{
    # Statement to try
    New-Item -Path 'D:\Temp\DoesNOtExist' -Name 'Test.txt' -ItemType File -ErrorAction Stop
}
catch 
{
    # What to do with terminating errors
    Write-Warning -Message $Error[0]
}

# Results
<#
WARNING: Could not find a part of the path 'D:\Temp\DoesNOtExist\Test.txt'.
#>

Or using multiple catch statements, like ...

Example: Force file warning

Clear-Host
$Error.Clear()
try
{
    # Results in NoSupportException
    # Statement to try
    New-Item -Path 'D:\Temp\Temp' -Name my:Test.txt -ItemType File -ErrorAction Stop

    # Results in DirectoryNotFoundException
    # New-Item -Path 'D:\Temp\Temp' -Name 'Test.txt' -ItemType File -ErrorAction Stop
}
catch [System.NotSupportedException]
{
    # What to do with terminating errors
    Write-Warning -Message 'Illegal chracter or filename.'
}
catch [System.IO.DirectoryNotFoundException]
{
    # What to do with terminating errors
    Write-Warning -Message 'The path is not valid.'
}
catch 
{
    # What to do with terminating errors
    Write-Warning -Message 'An unexpected error occurred.'
}

# Results
<#
WARNING: Illegal character or filename.
#>

Example: Force path warning

Clear-Host
$Error.Clear()
try
{
    # Results in NoSupportException
    # Statement to try
    # New-Item -Path 'D:\Temp\Temp' -Name my:Test.txt -ItemType File -ErrorAction Stop

    # Results in DirectoryNotFoundException
    New-Item -Path 'D:\Temp\Temp' -Name 'Test.txt' -ItemType File -ErrorAction Stop
}
catch [System.NotSupportedException]
{
    # What to do with terminating errors
    Write-Warning -Message 'Illegal chracter or filename.'
}
catch [System.IO.DirectoryNotFoundException]
{
    # What to do with terminating errors
    Write-Warning -Message 'The path is not valid.'
}
catch 
{
    # What to do with terminating errors
    Write-Warning -Message 'An unexpected error occurred.'
}

# Results
<#
WARNING: The path is not valid.
#>

See also this discussion, which is a similar use case. Pay particular attention the the 'Exit vs Return vs Break' answer.

Terminating a script in PowerShell

postanote
  • 15,138
  • 2
  • 14
  • 25