如何使用VB脚本检查命令提示符是否已打开?

时间:2015-02-06 02:56:15

标签: visual-studio-2010 command-line vbscript window

如何使用VB脚本检查命令提示符/窗口是否已打开?

我正在使用Visual Studio 2010 - 安装项目,我希望我的VB脚本检查命令行是否已关闭,当它关闭时,它将引发错误并终止整个安装。

1 个答案:

答案 0 :(得分:0)

class Win32_ProcessStartTrace : Win32_ProcessTrace
{
  uint4 PageDirectoryBase;
  string ParentProcessName;
  uint32 ProcessID;
  string ProcessName;
  uint8 SECURITY_DESCRIPTOR[];
  uint32 SessionID;
  uint8 Sid[];
  uint8 TIME_CREATED;
};

此监视器启动和停止。上面的属性来自仅启动和与下面对象相同的属性。还有一个停止物体。我的代码只能做一个事件,有一个替代方案可以做多事件,但你必须设置事件处理程序。

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_ProcessTrace")

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.ProcessName
Loop

您可以从Process类中获取额外信息。

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
    'Get process id you want from other script
    If obj.ProcessID = 1004 Then
        msgbox objItem.ProcessID & " " & FormatDateTime(CDate(objItem.CreationDate))
    EndIf
Next

这些是它的属性。它也有方法。

class Win32_Process : CIM_Process
{
  string Caption;
  string CommandLine;
  string CreationClassName;
  datetime CreationDate;
  string CSCreationClassName;
  string CSName;
  string Description;
  string ExecutablePath;
  uint16 ExecutionState;
  string Handle;
  uint32 HandleCount;
  datetime InstallDate;
  uint64 KernelModeTime;
  uint32 MaximumWorkingSetSize;
  uint32 MinimumWorkingSetSize;
  string Name;
  string OSCreationClassName;
  string OSName;
  uint64 OtherOperationCount;
  uint64 OtherTransferCount;
  uint32 PageFaults;
  uint32 PageFileUsage;
  uint32 ParentProcessId;
  uint32 PeakPageFileUsage;
  uint64 PeakVirtualSize;
  uint32 PeakWorkingSetSize;
  uint32 Priority;
  uint64 PrivatePageCount;
  uint32 ProcessId;
  uint32 QuotaNonPagedPoolUsage;
  uint32 QuotaPagedPoolUsage;
  uint32 QuotaPeakNonPagedPoolUsage;
  uint32 QuotaPeakPagedPoolUsage;
  uint64 ReadOperationCount;
  uint64 ReadTransferCount;
  uint32 SessionId;
  string Status;
  datetime TerminationDate;
  uint32 ThreadCount;
  uint64 UserModeTime;
  uint64 VirtualSize;
  string WindowsVersion;
  uint64 WorkingSetSize;
  uint64 WriteOperationCount;
  uint64 WriteTransferCount;
};

她是帮助中的示例脚本,显示了如何执行多个事件处理程序。

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & _
    strComputer & "\root\CIMV2") 
Set EventSink = WScript.CreateObject( _
    "WbemScripting.SWbemSink","SINK_")

objWMIservice.ExecNotificationQueryAsync EventSink, _
    "SELECT * FROM Win32_ProcessTrace WITHIN 10"
WScript.Echo "Waiting for events..."

i = 0
While (True)
    Wscript.Sleep(1000)
Wend

Sub SINK_OnObjectReady(objObject, objAsyncContext)
    Wscript.Echo "Win32_ProcessTrace event has occurred."
    i = i+1
    If i = 3 Then WScript.Quit 0 
End Sub