在vbscript中,如何使用当前cmd提示窗口的环境运行批处理文件或命令? [英] In vbscript, how do I run a batch file or command, with the environment of the current cmd prompt window?

查看:246
本文介绍了在vbscript中,如何使用当前cmd提示窗口的环境运行批处理文件或命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vbscript中,如何在当前的cmd提示窗口中运行批处理文件或命令



而不需要启动新进程。



例如。根据script56.chm(vbscript帮助显然)
Windows脚本宿主

运行方法(Windows Script Host)

在新进程中运行程序 p>

所以如果我有代码使用例如一个VBS文件和一个BAT文件。
环境变量g从该命令窗口中的值为abc g = abc

VBS文件使用Windows脚本主机Run调用BAT文件。
bat进程设置g = z。并完成..和vbs过程完成。
环境变量保持不变,因为g = abc。



我知道
CreateObject(Wscript.Shell)。运行c:\test.bat,0
启动一个新窗口当使用1而不是0时(由于0隐藏窗口)



如何
- 从vbs中的bat文件,在调用vbs的cmd环境相同,所以更改会影响调用的cmd环境?
- 在目前这两个窗口的情况下,如何从批处理文件访问父cmd窗口的环境?

解决方案


如何在当前cmd提示窗口中运行批处理文件或命令,而不启动新进程?


< blockquote>

我不认为你可以;您的vbscript运行在脚本主机引擎下(例如 cscript.exe wscript.exe ),批处理文件由命令解释器解释(通常 cmd.exe )。两者都是单独的可执行文件,据我所知,两者都不是作为进程内库,因此您不能在同一进程中解释.vbs和.cmd文件。我也非常怀疑运行VBScript的脚本主机引擎也可以在其父cmd.exe中运行批处理文件 - 我不认为你可以将一个新的批处理文件注入正在运行的cmd.exe。 p>


如何从批处理文件访问父cmd窗口的环境?


不只是访问,而是更改 - MSDN的更改环境变量是非常明确的:在进程创建过程中更改子进程的环境变量是一个进程可以直接更改另一个进程的环境变量的唯一方法一个进程永远不能直接改变另一个不是该进程的进程的进程的环境变量。您正在尝试改变而不是子进程的环境。 (我想知道在这个报价的上下文中,直接是什么意思)。



我猜这个原因是安全的;想象如果任意进程可能(恶意或意外)更改正在运行的进程(如vbscript主机引擎进程)的PATH(或COMSPEC)环境变量,那么可能会造成严重破坏 - 它可能无法完全启动蝙蝠文件,从而破坏您的程序。



似乎你没有运气 - 但是,有很多其他机制可以在进程之间传递信息。以下是几个建议,这些建议在批处理文件和vbscript,虽然并不详尽:




  • 退出代码

  • 写入&解析consoleoutput(stdout)或临时文件



如果你绝对需要来设置环境变量父cmd.exe(并且绝对需要 vbscript的中间步骤),那么您可能必须编写运行vbscript的包装器批处理文件,消耗由其生成的信息,然后设置环境变量;因为wrapper cmd在的顶层cmd进程中执行,所以可以在其中更改env var。



脚注:注意您可以从VBScript中更改永久系统/用户环境变量(而不是进程环境变量),但如果您尝试创建一个暂态状态,我不会建议这样做;除此之外,这不会影响已经运行的进程(如父cmd.exe)。


In vbscript, how do I run a batch file or command, in the current cmd prompt window,

without starting a new process.

For example. According to script56.chm (the vbscript help apparently) Windows Script Host
Run Method (Windows Script Host)
"Runs a program in a new process"

So if I have code that uses that e.g. a VBS file, and a BAT file. An environment variable g has the value abc g=abc from that command window, The VBS file calls the BAT file with windows scripting host Run. The bat process sets g=z. and finishes.. and the vbs process finishes. The environment variable is left untouched as g=abc.

I know CreateObject("Wscript.Shell").Run "c:\test.bat", 0 starts a new window as is clear when using 1 instead of 0. (since 0 hides the window)

How do I -run the bat file from the vbs, in the same cmd environment that the vbs was called in, so changes affect the cmd environment it was called in? -In the two windows case which this one is at the moment, how do I access the environment of the parent cmd window, from the batch file?

解决方案

how do I run a batch file or command, in the current cmd prompt window, without starting a new process?

I don't think you can; your vbscript runs under a script host engine (such as cscript.exe or wscript.exe), and batch files are interpreted by the command interpreter (typically cmd.exe). Both are separate executables and neither is, to my knowledge, available as an in-process library, so you cannot interpret .vbs and .cmd files within the same process. I also highly doubt that the script host engine that is running your VBScript also could run the batch file in its parent cmd.exe - I don't think you can 'inject' a new batch file into a running cmd.exe.

how do I access the environment of the parent cmd window, from the batch file?

Not just access, but change - MSDN's "Changing Environment Variables" is quite explicit on this: "Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process." You are trying to change the environment of the parent, not child, process. (I do wonder what 'directly' means in the context of this quote, though).

I would guess that the reason for this is security; imagine the havoc that could be wreaked if arbitrary processes could (maliciously or accidentally) change the PATH (or COMSPEC) environment variable of a running process, such as your vbscript host engine process - it could fail to launch your bat file entirely, breaking your program.

It would seem that you're out of luck - however, there are lots of other mechanisms for passing information between processes. Here are a couple of suggestions that are fairly simple to implement when talking between a batch file & vbscript, although it's by no means exhaustive:

  • Exit codes
  • Writing to & Parsing the consoleoutput (stdout) or a temp file

If you absolutely need to set environment variables in the parent cmd.exe (and also absolutely need the intermediate step of a vbscript), then you may have to write a wrapper batch file which runs the vbscript, consumes information produced by it and then sets environment variables; because the wrapper cmd is executing in the top-level cmd process, it will be able to change the env vars there.

Footnote: Note that you can change the permanent system/user environment variables (as opposed to process environment variables) from within a VBScript, but I wouldn't recommend this if you are trying to create a transient state; besides this won't affect already-running processes (like the parent cmd.exe) anyway.

这篇关于在vbscript中,如何使用当前cmd提示窗口的环境运行批处理文件或命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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