我正在尝试执行一个外部程序,在满足特定条件时使用一些变量。据我所知,该命令并未尝试运行。我试过只使用notepad
,或者只是opcmon
命令本身,它应该会生成一条使用消息。
我得到的唯一输出来自Echo
, 并且看起来格式正确。例如
Microsoft (R) Windows Script Host 版本 5.812 版权所有 (C) Microsoft Corporation。版权所有。 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
任何类型的 VBScript 调试的第一步:删除On Error Resume Next
. 或者说,千万不要使用On Error Resume Next
在全球范围内。曾经!
删除该语句后,您将立即看到问题所在,因为您将收到以下错误:
script.vbs(6, 1) Microsoft VBScript 运行时错误:变量未定义:'WshShell'
该Option Explicit
语句使变量声明成为强制性的。但是,您没有声明WshShell
,因此Set WshShell = ...
语句失败,但是因为您也有On Error Resume Next
错误被抑制并且脚本继续。当执行到达Call WshShell.Run(...)
语句时,它也会失败(因为没有可以从中调用Run
方法的对象),但再次抑制了错误。这就是为什么您会看到Echo
输出,但看不到正在执行的实际命令的原因。
删除On Error Resume Next
并添加WshShell
到您的Dim
语句中,问题就会消失。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句