外部命令未从VBScript运行

时间:2017-10-20 20:07:19

标签: vbscript

我正在尝试执行外部程序,当满足某个条件时会有一些变量。据我所知,该命令不是试图运行。我尝试过只使用notepad,或只使用opcmon命令本身,它会生成一条用法消息。

我得到的唯一输出来自Echo,看起来格式正确。 E.g。

Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

opcmon.exe "TEST-Goober"=151 -object "C:\Tools"
' Script Name: FileCount.vbs
' Purpose: This script will send a message to OM with the number
'          of files which exist in a given directory.
' Usage: cscript.exe  FileCount.vbs [oMPolicyName] [pathToFiles]
' [oMPolicyName] is the name of the HPOM Policy
' [pathToFiles] is Local or UNC Path

Option Explicit
On Error Resume Next

Dim lstArgs, policy, path, fso, objDir, objFiles, strCommand, hr

Set WshShell = CreateObject("WScript.Shell")
Set lstArgs = WScript.Arguments

If lstArgs.Count = 2 Then
  policy = Trim(lstArgs(0))
  path   = Trim(lstArgs(1))
Else
  WScript.Echo "Usage: cscript.exe filecount.vbs [oMPolicyName] [pathToFiles]" &vbCrLf &"[oMPolicyName] HPOM Policy name" & vbCrLf &"[pathToFiles] Local or UNC Path"
  WScript.Quit(1)
End If

Set fso = WScript.CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(path) Then
  Set objDir = fso.GetFolder(path)

  If (IsEmpty(objDir) = True) Then
    WScript.Echo "OBJECT NOT INITIALIZED"
    WScript.Quit(1)
  End If

  Set objFiles = objDir.Files

  strCommand = "opcmon.exe """ & policy & """=" & objFiles.Count & " -object """ & path & """"
  WScript.Echo strCommand
  Call WshShell.Run(strCommand, 1, True)
  WScript.Quit(0)
Else
  WScript.Echo("FOLDER NOT FOUND")
  WScript.Quit(1)
End If

1 个答案:

答案 0 :(得分:0)

任何类型的VBScript调试的第一步:删除"exit"。或者更确切地说,从不在全局范围内使用On Error Resume Next。的 EVER!

删除该语句后,您会立即看到错误,因为您会收到以下错误:

  

script.vbs(6,1)Microsoft VBScript运行时错误:变量未定义:'WshShell'

On Error Resume Next语句使变量声明成为必需。但是,您没有声明Option Explicit,因此WshShell语句失败,但因为您还有Set WshShell = ...错误被禁止并且脚本继续。当执行到达On Error Resume Next语句时,它也会失败(因为没有对象可以调用Call WshShell.Run(...)方法),但是错误被抑制了。这就是为什么你看到Run输出,而不是正在执行的实际命令。

删除Echo并将On Error Resume Next添加到您的WshShell语句中,问题就会消失。