0
votes

I'm trying to execute an external program, with some variables when a certain condition is met. As far as I can tell, the command isn't attempting to run. I've tried just using notepad, or just the opcmon command itself, which should generate a usage message.

The only output I get is from the Echo, and that looks formatted properly. 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

1 Answers

0
votes

First step to any kind of VBScript debugging: remove On Error Resume Next. Or rather, NEVER use On Error Resume Next in the global scope. EVER!

After removing that statement you'll immediately see what's wrong, because you'll get the following error:

script.vbs(6, 1) Microsoft VBScript runtime error: Variable is undefined: 'WshShell'

The Option Explicit statement makes variable declarations mandatory. However, you didn't declare WshShell, so the Set WshShell = ... statement fails, but because you also have On Error Resume Next the error is suppressed and the script continues. When the execution reaches the Call WshShell.Run(...) statement, that too fails (because there's no object to call a Run method from), but again the error is suppressed. That's why you see the Echo output, but not the actual command being executed.

Remove On Error Resume Next and add WshShell to your Dim statement, and the problem will disappear.