Powershell/CMD问题

huangapple go评论72阅读模式
英文:

Powershell/CMD issue

问题

我试图编写一个快速的批处理文件,但它只完成了一半的代码,所以我改成了PowerShell,因为我记得以前能够让它工作,结果这次确实有效。

我的问题本质上是我有一些用户希望能够访问这个“它只关闭一个程序然后重新打开”的功能。他们熟悉批处理文件(但我无法让它重新打开程序),但不习惯使用PowerShell脚本,并且必须右键单击运行PowerShell,我预计这会导致问题,并且很多用户可能根本不使用该脚本。

是我在批处理文件上做错了什么,导致它不能重新打开,还是有办法更改PowerShell上的左键单击选项(当前左键单击会在记事本中打开脚本;理想情况下,左键单击会以PowerShell方式运行)?

代码对PowerShell和CMD都是相同的,直接复制粘贴如下:

taskkill /IM ADM.TrayApp.exe /F
Start-Process "C:\Program Files (x86)\athenahealth, Inc\aNetDeviceManager.1.4.0\TrayApp\CoreModule\ADM.TrayApp.EXE"
英文:

So I was trying to write a quick Batch file but it only did half the code, so I switched it to PowerShell because I remembered a while back I was able to get it to work and turns out this did work.

My issue is essentially I have a handful of users I want to have access to this "it just closes a program and reopens". They're familiar with Batch files (which I couldn't get to reopen the program) but would not be used to using a PowerShell script and having to right click to run as PowerShell which I'm expecting will cause issues and many of the users to not use the script in the first place.

Is there either something I did wrong on the batch file for it not to reopen or is there a way to change the left click option on the PowerShell(currently left click opens the script in notepad; ideally it would just run as PowerShell on left click)

The code is the same for both PowerShell and CMD copied and pasted directly to.

taskkill /IM ADM.TrayApp.exe /F
Start-Process  "C:\Program Files (x86)\athenahealth, Inc\aNetDeviceManager.1.4.0\TrayApp\CoreModule\ADM.TrayApp.EXE"

答案1

得分: 0

  • 你不能直接在批处理文件中使用 PowerShell 的 Start-Process - 你需要通过 powershell.exe,Windows PowerShell CLI 或 pwsh,PowerShell (Core) CLI 来调用。

  • 但是,正如 Stephan 指出的那样,cmd.exe 的内部 start 命令提供了类似的功能,所以在你的情况下使用它应该足够了(正如 Stephan 指出的,如果要启动的可执行文件也用 "..." 括起来,作为第一个参数需要提供一些窗口标题,也要用 "..." 括起来;"" 可以使用):

    start "" "C:\Program Files (x86)\athenahealth, Inc\aNetDeviceManager.1.4.0\TrayApp\CoreModule\ADM.TrayApp.EXE"
    
  • PowerShell 脚本可以在从文件资源管理器或桌面上(双击)左键单击时默认执行,但在每台计算机上需要进行一些复杂的设置:

    • 参见 此答案
    • 链接的答案还描述了提供简单的_伴随_批处理文件的替代技术,其唯一目的是执行相关的 PowerShell 脚本。
英文:
  • You cannot use PowerShell's Start-Process directly in a batch file - you'd have to call via powershell.exe, the Windows PowerShell CLI or pwsh, the PowerShell (Core) CLI).

  • However, as Stephan points out, cmd.exe's internal start command provides similar functionality, so its use should be sufficient in your case (as Stephan notes, some window title enclosed in "..." is needed as the first argument if the executable to launch is enclosed in "..." too; "" will do):

    start "" "C:\Program Files (x86)\athenahealth, Inc\aNetDeviceManager.1.4.0\TrayApp\CoreModule\ADM.TrayApp.EXE"
    
  • It is possible to make PowerShell scripts execute by default when (double-)left-clicked from File Explorer or the desktop, but requires nontrivial setup on each machine:

    • See this answer.
    • The linked answer also describes an alternative technique of providing simple companion batch files whose sole purpose is to execute an associated PowerShell script.

huangapple
  • 本文由 发表于 2023年6月2日 03:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定