powershell - ISE 中的 MyInvocation.MyCommand.Path 与常规 PS
问题描述
我有一个旧脚本,它在某种情况下使用一些额外的参数调用自己。如果通过常规的 powershell 窗口运行,这将有效
$spath = $script:MyInvocation.MyCommand.Path
$FilePathWithQuotes = '"{0}"' -f $spath
powershell -file $FilePathWithQuotes -NestedCall @ExtraArgs
但如果它通过 ISE 运行,我会收到此错误。
powershell:Add-Type:无法将参数“路径”绑定到目标。异常设置“路径”:“在 D:\Deploy\File Deploy.ps1:39 char:5 + powershell -file $FilePathWithQuotes -NestedCall @ExtraArgs + ~~~~~~~~~~~~~~~找不到路径~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定: (Add-Type : Cann...nnot find path :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
我添加了第二行以确保空格不是问题,但它在 ISE 中仍然失败。想法?我在 ps 5.1
解决方案
是的,检查$PSISE
并尽可能使用它。
$spath = If($PSISE){$psISE.CurrentFile.FullPath}else{$script:MyInvocation.MyCommand.Path}
$FilePathWithQuotes = '"{0}"' -f $spath
powershell -file $FilePathWithQuotes -NestedCall @ExtraArgs
推荐阅读
- reactjs - 在域的 nginx 服务器上显示反应应用程序时出现问题
- python - GCP Dataflow + Apache Beam - 缓存问题
- javascript - 在 React Js 中重新渲染画布
- javascript - 为什么 scrollWidth 不包括子元素的右边距?
- python - 秒表与 tkinter?
- forms - Wagtail Forms:如何从字段中进行预设,以便编辑器无法更改任何内容?
- azure - 使用 ARM 模板为 Azure 防火墙动态添加 natRuleCollections
- r - 如何在 R 中绘制“U”形图
- java - 使用 Google Cloud Spring 依赖项进行本地开发
- php - 仅在手动订单的 WooCommerce 管理员单个订单中显示产品自定义字段