在Powershell中更改为cmd后无法通过批处理脚本文件调用bat文件 [英] In powershell after changing to cmd unable to call a bat file through a batch script file

查看:164
本文介绍了在Powershell中更改为cmd后无法通过批处理脚本文件调用bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过将目录更改为工作区目录并调用setupEnv.bat文件创建了一个bat文件来设置我的工作区.但是,当我在PowerShell中执行以下bat文件时, cmd 之后的指令未执行. 我需要在cmd中调用setupEnv.bat文件.如果删除 cmd ,它将正常工作.但是我不想在PowerShell中在cmd上调用setupEnv.bat.

I created a bat file to setup my workspace by changing the directory to the workspace directory and calling the setupEnv.bat file. But while I'm executing the below bat file in PowerShell, the instructions after cmd are not executing. I need to call the setupEnv.bat file in cmd. If I remove the cmd it will work fine. But I want call the setupEnv.bat on cmd not in PowerShell.

D:
cd D:\WorkSpace\
cmd
call setupEnv.bat
echo "Setup Completed"

  • 调用setupEnv.bat并调用cmd之后,是否会将所有环境变量设置保留在PowerShell中?
  • 推荐答案

    本文介绍了您的确切情况:

    This article addresses your exact scenario:

    Windows IT Pro-在PowerShell中负责环境变量

    变量消失的原因是.bat.cmd在单独的cmd.exe进程中运行(当进程终止时,您会丢失变量).

    The reason the variables disappear is that a .bat or .cmd runs in a separate cmd.exe process (when the process terminates, you lose the variables).

    本文介绍了一个PowerShell函数,可用于运行.bat.cmd脚本并保留其设置的环境变量:

    The article presents a PowerShell function you can use to run a .bat or .cmd script and retain the environment variables it sets:

    # Invokes a Cmd.exe shell script and updates the environment.
    function Invoke-CmdScript {
      param(
        [String] $scriptName
      )
      $cmdLine = """$scriptName"" $args & set"
      & $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
        Select-String '^([^=]*)=(.*)$' |
        ForEach-Object {
          $varName = $_.Matches[0].Groups[1].Value
          $varValue = $_.Matches[0].Groups[2].Value
          Set-Item Env:$varName $varValue
      }
    }
    

    本文还具有一些用于在PowerShell中设置和还原环境变量值的功能.

    The article also has a couple of functions for setting and restoring environment variable values in PowerShell.

    这篇关于在Powershell中更改为cmd后无法通过批处理脚本文件调用bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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