Powershell突然打开cmd.exe执行bats [英] Powershell suddenly opens cmd.exe for executing bats

查看:71
本文介绍了Powershell突然打开cmd.exe执行bats的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到昨天我在 powershell 中使用任一脚本执行了一个脚本

Until yesterday when i executed a script inside powershell using either

"foo.bat"

"cmd /c commandhere"

它将在 Powershell 中运行命令并打印输出.现在从今天起它不再找到cmd",当使用cmd.exe"或执行 bat 脚本时,它会打开 cmd 窗口,而不是在 powershell 中运行脚本.

It would run the command inside the Powershell and also print the output. Now since today it does not find "cmd" anymore and when use "cmd.exe" or execute a bat script it will open the cmd window instead of running the script inside powershell.

有人知道这是否可以配置,或者我如何才能回到在 powershell 中执行脚本?

Does someone know if this is configurable or how i can get back to executing scripts just inside the powershell?

我的环境是:

PS C:\> echo $env:Path
C:\Windows\system32;C:\Windows\;C:\Windows\System32\Wbem;C:\Windows\system32\WindowsPowerShell\v1.0\;C:\xampp\php;C:\code\azure\bin;%SystemRoot%\system32\WindowsPowerShell\v1.0\;C:\Windows\System32

推荐答案

要补充 Justin 的答案,机器"可能不是您在这里寻找的目标,尽管代码片段确实有效.

To add to Justin's answer, "Machine" might not be the target you're looking for here, despite the fact that the code snipet does work.

>

首先,如果PATHEXT不再包含.BAT"或.CMD",则问题出在别处.

First of all, if PATHEXT does not contain ".BAT" or ".CMD" anymore, the problem is elsewhere.

如果这是您环境中的正确行为,请继续阅读.

If this is a correct behavior in your environment, then read on.

$pathext = [Environment]::GetEnvironmentVariable("PATHEXT", "Process")
[Environment]::SetEnvironmentVariable("PATHEXT", $pathext+';.BAT', "Process")

  • 机器"目标将变量放在HKEY_LOCAL_MACHINE"中注册表,准备为下一个进程读取
  • 用户"目标将变量放在HKEY_CURRENT_USER"注册表中,准备为下一个进程读取
  • 进程"目标将变量置于当前运行的 PowerShell.exe 进程的可用状态
  • 当我说进程"时,我的意思是一些其他可执行文件,通过脚本以外的其他方式启动.由您的脚本启动的进程(作为您的 bat/cmd 命令)是子进程并继承当前环境,因此所有定义的变量,除非您指定它作为第二个线程start-process".

    When I say "process", I really mean some other executable, launched by other means than your script. Processes launched by your script (as your bat/cmd command) are child-processes and inherit the current environment, thus all the defined variables, unless you specify it otherwise like a second thread "start-process".

    据我所知,这就是您要实现的目标.PATHEXT 变量及其新添加的内容将仅通过 PowerShell 脚本/进程存在,并且可用于您的 bat/cmd 脚本.

    For what I can read, this is what you're trying to achieve. the PATHEXT variable, with it's new added content, will only live through the PowerShell script/process and will be available to your bat/cmd script.

    这篇关于Powershell突然打开cmd.exe执行bats的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆