【问题标题】:Powershell terminate executionPowershell 终止执行
【发布时间】:2020-05-19 21:40:26
【问题描述】:

当没有找到具有当前日期戳的文件时,我正在使用 powershell 处理目录中的 csv 文件我希望该进程引发错误,通知文件未找到并退出。

# 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"
}

【问题讨论】:

  • 有什么问题(当前的实际行为)?
  • 嗨@iRon,如果代码进入else块中的else块,我想让powershell脚本停止执行我想通过电子邮件通知找不到文件并停止执行。
  • 发送邮件如:Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Test mail -Body 'File not found',详情见:docs.microsoft.com/powershell/module/…。如果它没有按预期工作,请将该详细信息添加到问题。

标签: powershell


【解决方案1】:

至于这个……

我想让 powershell 脚本停止执行

...这就是 Exit 关键字或 '-ErrorAction Stop' 选项的用途。

至于这个……

如果代码进入我想要的 else 块中的 else 块 通知文件未找到

...根据我的上述,同样的事情,你必须设置它。

意思是...

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'.
#>

或者使用多个 catch 语句,比如 ...

示例:强制文件警告

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.
#>

示例:强制路径警告

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.
#>

另请参阅此讨论,这是一个类似的用例。请特别注意“退出 vs 返回 vs 中断”的答案。

Terminating a script in PowerShell

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2020-10-11
    • 2020-10-09
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    相关资源
    最近更新 更多