3

I have two files weblogic.jar and weblogic.policy in C:\Weblogic\wlserver\server\lib. With the first method, the script finds them and displays the name of the file:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Weblogic\wlserver\server\lib")
Set colFiles = objFolder.Files
For Each objFile in colFiles
    If(StrComp(objFile.Name, "weblogic.jar", 1) = 0 OR StrComp(objFile.Name, "weblogic.policy", 1) = 0) Then
        Wscript.Echo objFile.Name, objFile.Size
    End If
Next

When I try to use WMI with CIM_DataFile, the script doesn't find any file in the same folder (but finds some in other folders):

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select Name from CIM_DataFile where FileName = 'weblogic'",, 48)
For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next

I am on Windows Server 2012 R2, I run the script as administrator and the folder C:\Weblogic needs admin privileges.

Is it a WMI privilege problem? Someone already have this problem? What is the solution?

EDIT:

Thanks for your answer.

Sadly, that doesn't work. I get the same result. I run the 2 method on the same script. I try to create test files on my desktop named weblogic.jar, weblogic.policy, ... and WMI doesn't find them !

Maybe WMI no longer works properly on this server ?

This is my script:

If Not WScript.Arguments.Named.Exists("elevate") Then
    Wscript.Echo "Run"
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If

Set objFSO=CreateObject("Scripting.FileSystemObject")

outFile="C:\test.txt"
Set objFileLog = objFSO.CreateTextFile(outFile,True)

objFileLog.Write "Scripting.FileSystemObject :" & vbCrLf
Set objFolder = objFSO.GetFolder("C:\Weblogic\wlserver\server\lib")
Set colFiles = objFolder.Files
For Each objFile in colFiles
    If(StrComp(objFile.Name, "weblogic.jar", 1) = 0 OR StrComp(objFile.Name, "weblogic.policy", 1) = 0) Then
        Wscript.Echo objFile.Name, objFile.Size
        objFileLog.Write "  " & objFile.Path & " " & objFile.Size & vbCrLf
    End If
Next

objFileLog.Write "winmgmts :" & vbCrLf
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select Name from CIM_DataFile where FileName = 'weblogic'",, 48)
For Each objFile in colFiles
    Wscript.Echo objFile.Name
    objFileLog.Write "  " & objFile.Name & vbCrLf
Next

objFileLog.Close

And the result is:

Scripting.FileSystemObject :
    C:\Weblogic\wlserver\server\lib\weblogic.jar 5541
    C:\Weblogic\wlserver\server\lib\weblogic.policy 30888
winmgmts :
    c:\oracle\...\templates\wlserver\server\lib\weblogic.policy
    c:\oracle\...\wlserver\server\lib\weblogic.policy
    c:\oracle\...\sample\config\wls\web-inf\weblogic.xml

I don't get weblogic files with WMI in folders : "C:\Weblogic\wlserver\server\lib\" "C:...\Desktop\"

Harald
  • 31
  • 3
  • It's going to be a privilege issue, make sure you are running the VBScript from an elevated Command Window as @Hackoo has suggested. You haven't detailed in the question how you call the script? You need to have a elevated Command Window before calling the script using `cscript.exe` for example just adding the suggested script to your existing one will not work. As a simple test try opening `cmd.exe` with `Run As Administrator` that way it will have elevated privileges. Then run something like `cscript /nologo "path to your script file"`. – user692942 Jun 02 '16 at 12:57
  • I run the 2 functions (FSO and WMI) in the same instance of the script : FSO can access to files while WMI don't list them. I use since the start the cmd with "Run as Administrator". Moreover, i create some file with weblogic name on my dekstop and they don't need administrator rights, but WMI doesn't find them. I try the "cscript /nologo" but the result is the same as before. – Harald Jun 02 '16 at 13:25

1 Answers1

4

Try something like this to run the script with admin privileges :

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If
'Your code goes here 
Hackoo
  • 18,337
  • 3
  • 40
  • 70