2017-10-20 190 views
0

我試圖執行一個外部程序,當某些條件滿足時使用一些變量。據我所知,命令不是試圖運行。我試過只使用notepad,或者只是使用opcmon命令本身,這應該會產生一個用法消息。外部命令不能從VBScript運行

我得到的唯一結果是從Echo,看起來格式正確。例如。

 
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 

回答

0

第一步,任何一種的VBScript調試:刪除On Error Resume Next。或者說,在全球範圍內,從不使用On Error Resume Next永遠!

移除語句後,你會立即看到什麼是錯的,因爲你會得到以下錯誤:

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

Option Explicit語句使變量聲明強制性的。但是,您沒有聲明WshShell,因此Set WshShell = ...語句失敗,但因爲您也有On Error Resume Next,所以錯誤被抑制並且腳本繼續。當執行到達Call WshShell.Run(...)語句時,該語句也失敗(因爲沒有對象調用Run方法),但錯誤又被抑制。這就是爲什麼你看到Echo輸出,但不是正在執行的實際命令。

刪除On Error Resume Next並將WshShell添加到您的Dim聲明中,問題就會消失。

+0

謝謝你的信息!那樣做了 –

相關問題