从 powershell 脚本生成的程序中正常退出

2023-12-05

假设我有以下脚本:

$originalPath=pwd
D:\code\ps1\misc\title.ps1 "dynamo db"

$CURPATH = "$PSScriptRoot\path.txt"
$DB_DIR= cat $CURPATH

cd $DB_DIR
java -D"java.library.path=./DynamoDBLocal_lib" -jar DynamoDBLocal.jar
cd $originalPath

这个脚本正确启动了java程序,但是,当我按CTRL+C时,最后一个cd命令未执行。

这是 powershell 设计的吗?或者我可以改变这个吗?


This answer applies up to at least PowerShell (Core) 7.3.5.

When you use Ctrl-C to abort an external program, the current PowerShell script (runspace) is invariably terminated too.

While you cannot generally prevent your script from getting terminated,[1] you can perform clean-up actions, namely in the finally block of a try / catch / finally statement:

$originalPath = $PWD

# ...

try {

  cd $DB_DIR
  java -D"java.library.path=./DynamoDBLocal_lib" -jar DynamoDBLocal.jar

} finally {

  # This block is *always* executed, but limitations apply
  # if Ctrl-C was pressed:
  #  * Execution invariably ends with this block.
  #  * You cannot output to the success output stream or error stream,
  #    neither explicitly nor implicitly: doing so quietly and instantly
  #    aborts processing.
  #  * However, the *other* output streams *can* be targeted, namely with
  #    the following cmdlets (but note that except for Write-Host and Write-Warning, 
  #    producing visible output is opt-in):
  #     Write-Host, Write-Warning, Write-Verbose, Write-Debug, Write-Information

  cd $originalPath

}

请注意有关内容的限制输出流您可以定位,如上面的代码注释中所述,这源于finally阻止在脚本终止过程的后期执行。

值得注意的是,尝试使用Write-Output或隐式成功流输出或Write-Error will 悄悄中止的执行finally那时,而Write-Host, Write-Verbose and Write-Warning仅举几个最重要的,可以安全使用。


[1] The only way to intercept Ctrl-C in order to prevent termination is via a custom keyboard-polling loop, after having set [Console]::TreatControlCAsInput = $false - see this answer for an example.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 powershell 脚本生成的程序中正常退出 的相关文章

随机推荐

  • 为什么这段代码会崩溃?

    以下代码会导致堆栈溢出 但我不明白为什么 int tmain int argc TCHAR argv cout lt lt start char bmp 1024 768 3 for int p 0 p lt 9000 p for int
  • 读取网页内容

    你好 我想使用java读取包含德语字符的网页内容 不幸的是 德语字符显示为奇怪的字符 请提供任何帮助 这是我的代码 String link some german link URL url new URL link BufferedRead
  • 如何设置Spark Kmeans初始中心

    我正在使用 Spark ML 来运行 Kmeans 我有一堆数据和三个现有中心 例如这三个中心是 1 0 1 0 1 0 5 0 5 0 5 0 9 0 9 0 9 0 那么我如何表明 Kmeans 中心是上述三个向量 我看到 Kmean
  • Ionic-Framework (4) - Openlayers 地图不工作/不可见

    我尝试将 Openlayers 与 Ionic 一起使用 但地图在 setTimeout 之前不可见 这是我的工作代码 import Component OnInit from angular core import OlMap from
  • 找不到类型或命名空间

    我目前正在尝试将 Visual Studio 2008 中的 WiX 3 5 自定义操作项目转换为 WiX 3 7 和 Visual Studio 2012 但出现以下异常 找不到类型或名称空间名称 MyNamespace 您是 缺少 us
  • 页面加载时,如何让六个函数互相执行?

    当页面加载时 在一个大div中 有六个不同的元素 这些元素有六个不同的函数 我想让这些函数在一定时间后互相执行 例如1000ms 但这六个函数不是绑定到一个元素 它们绑定到六个不同的元素 例如 当页面加载时 我想将Class line1 添
  • 如何使 在 div 内垂直居中? [复制]

    这个问题在这里已经有答案了 代码 div style border solid 1px gray width 400px padding 0px span style background e2e6f0 padding right 4px
  • Apache Commons 文件上传 - 流意外结束

    好吧 我不得不说 到目前为止 这个问题让我难住了 我们在 Tomcat 6 0 18 中运行的 Web 应用程序在文件上传期间失败 但是仅当客户端机器是Windows机器时 仅适用于某些机器 并且适用于所有浏览器 而不仅仅是IE 日志中有堆
  • 将函数应用于列表的每个元素[重复]

    这个问题在这里已经有答案了 假设我有一个类似的列表 mylis this is test another test 如何将函数应用于列表中的每个元素 例如 我该如何申请str upper to get THIS IS TEST ANOTHE
  • 如何在 C# 中执行 SHA1 文件校验和?

    我该如何使用SHA1CryptoServiceProvider 在文件上创建文件的 SHA1 校验和 using FileStream fs new FileStream C file location FileMode Open usin
  • python exec() 中的全局变量和局部变量

    我正在尝试使用 exec 运行一段 python 代码 my code class A object pass print locals s locals print A s A class B object a ref A global
  • 创建确定性有限自动机 (DFA) - Mercury

    我想要在 Mercury 中模拟确定性有限自动机 DFA 但我有几个地方很糟糕 形式上 DFA 具有以下特征 一组状态 S 一个输入字母 E 一个转换函数 S E gt S 一个startState s S setOfAcceptableF
  • Java [unchecked] 未检查的情况警告

    好吧 我环顾四周并做了很多谷歌搜索 但我仍然找不到避免此警告的方法 Integer result chooser showOpenDialog null if result equals 0 String tempHolder choose
  • 无法更改 Android 设备中的语言

    我正在尝试在应用程序中更改设备的语言 我有这个代码 Locale locale new Locale en US Locale setDefault locale Configuration config new Configuration
  • Python:访问另一个文件中函数中的变量

    我有两个文件 lib py global var def test var Hello return test py from lib import test print var 但是尽管它们位于同一文件夹中 当我运行 test py 时
  • 从按钮扩展获取所有数据 (Excel)

    我们有一个支持分页的数据表 没问题 需要包含 导出到 Excel 按钮 因此 我们添加了 按钮 扩展 并且可以正常导出数据 问题是这样的 它只导出当前页面的数据 而不是整个数据集 如何获取整组数据 是否可以 dataTable custom
  • 需要 WPF 线角度的算法

    计算 WPF 线的角度 以度为单位 的 正确 算法是什么 你需要atan2 然而 这将为您提供以弧度为单位的角度 转换为度数一定很容易 角度的计算方式为atan2 y2 y1 x2 x1 where x1 y1 and x2 y2 是你的线
  • Powershell:通过 UpgradeCode 卸载应用程序

    当我通过 Powershell 脚本升级 降级我的应用程序时 我想在运行新安装程序之前首先强制卸载当前安装的版本 如何使用应用程序的 UpgradeCode 使用 Powershell 来做到这一点 通过应用程序名称执行此操作会不太健壮 既
  • 单声道,shell 脚本?

    csharp 版本 Mono C 编译器版本 4 0 0 0 允许写入C 脚本 like usr bin csharp Console WriteLine Hello world 我尝试添加 main 函数 但出现解析错误 例如 inter
  • 从 powershell 脚本生成的程序中正常退出

    假设我有以下脚本 originalPath pwd D code ps1 misc title ps1 dynamo db CURPATH PSScriptRoot path txt DB DIR cat CURPATH cd DB DIR