cscript.exe opens a window

孤者浪人 提交于 2019-12-08 03:45:23

问题


I have a vbs file that need to be run in 32-bit, even though I am running Windows 7 64-bit. I can launch this file with the command

C:\Windows\SysWOW64\cscript.exe my-file.vbs

and that works fine, but it leaves me with a redundant command prompt window that I have to close manually each time. It also makes it very cumbersome to run this vbs-file as a startup item.

Is there a way to start my 32-bit vbs-file in the background?


回答1:


Try this for the 64bit problem, if it works you can combine it with the other answers

EDIT: here a question that goes more in depth on the 32/64 bit issue

How do I check if wscript/cscript runs on x64 host OS?

here the modified version, should make sure the script runs on 64bit platform

On Error Resume Next
Dim WshShell, OsType
Set WshShell = CreateObject("WScript.Shell")
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
If OsType = "x86" then
  wscript.echo "Windows 32bit system detected"
else
  wscript.echo "Windows 64bit system detected"
  If InStr(LCase(WScript.FullName),"system32") Then 
    CreateObject("WScript.Shell").Run """%systemroot%\SysWOW64\wscript.exe"" """ & WScript.ScriptFullName & """" 
    Wscript.Quit 
  End If 
end if

Msgbox("I ran..")



回答2:


If you need to use cscript this is IMHO a cool solution

Const HIDDEN_WINDOW = 0

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW

Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")

objProcess.Create "Cscript.exe h:\Script\Test1.vbs", null, objConfig, intProcessID



回答3:


If you can use wscript you could do the following, it's the simplest approach

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "Wscript.exe h:\Script\Test1.vbs"

actually you can do it as a one-liner (i'm a Ruby guy 8>)

CreateObject("Wscript.Shell").Run("Wscript.exe h:\Script\Test1.vbs")


来源:https://stackoverflow.com/questions/10716682/cscript-exe-opens-a-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!