Page 1 of 1

Passing argument to WMI Select statement in ExecQu

Posted: Mon Dec 03, 2007 8:29 am
by jdawg
Hi,

I want to pass arguments UserAccount and ManagerAccount to a vbscript. Then using WMI have it move the contents of the user accounts directory to the manager's account. Unfortunately, I can't find any examples of syntaxt that works in using a variable in the SELECT statement of the objWMIService.ExecQuery. If I set it statically it works, but I need to run the code for any user fed in.

I want to do something like this. What is the syntax?

UserAccount = WScript.Arguments.Item(1)ManagerAccount = WScript.Arguments.Item(2)UserDirectory = "c:users" & UserAccount

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Directory where name = UserDirectory", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)


Thanks,
J

Passing argument to WMI Select statement in ExecQu

Posted: Mon Dec 03, 2007 9:05 am
by jdawg
hmmm... it runs but doesn't return any colItems to enumerate. Here is the entire code (it is just an example) but works if I set the directory value statically in the code ('c:userstatlo01')


On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
ShareServer = WScript.Arguments.Item(0)
UserAccount = WScript.Arguments.Item(1)
ManagerAccount = WScript.Arguments.Item(2)
UserDirectory = "c:users" & UserAccount
ManagerDirectory = "c:users" & ManagerAccount


arrComputers = Array("USPLVMK3")
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="
WScript.Echo ShareServer
WScript.Echo UserAccount & " " & UserDirectory
WScript.Echo ManagerAccount & " " & ManagerDirectory
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Directory where name='" & UserDirectory & "'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
WScript.Echo "AccessMask: " & objItem.AccessMask
WScript.Echo "Archive: " & objItem.Archive
WScript.Echo "Caption: " & objItem.Caption
WScript.Echo "Compressed: " & objItem.Compressed
WScript.Echo "CompressionMethod: " & objItem.CompressionMethod
WScript.Echo "CreationClassName: " & objItem.CreationClassName
WScript.Echo "CreationDate: " & WMIDateStringToDate(objItem.CreationDate)
WScript.Echo "CSCreationClassName: " & objItem.CSCreationClassName
WScript.Echo "CSName: " & objItem.CSName
WScript.Echo "Description: " & objItem.Description
WScript.Echo "Drive: " & objItem.Drive
WScript.Echo "EightDotThreeFileName: " & objItem.EightDotThreeFileName
WScript.Echo "Encrypted: " & objItem.Encrypted
WScript.Echo "EncryptionMethod: " & objItem.EncryptionMethod
WScript.Echo "Extension: " & objItem.Extension
WScript.Echo "FileName: " & objItem.FileName
WScript.Echo "FileSize: " & objItem.FileSize
WScript.Echo "FileType: " & objItem.FileType
WScript.Echo "FSCreationClassName: " & objItem.FSCreationClassName
WScript.Echo "FSName: " & objItem.FSName
WScript.Echo "Hidden: " & objItem.Hidden
WScript.Echo "InstallDate: " & WMIDateStringToDate(objItem.InstallDate)
WScript.Echo "InUseCount: " & objItem.InUseCount
WScript.Echo "LastAccessed: " & WMIDateStringToDate(objItem.LastAccessed)
WScript.Echo "LastModified: " & WMIDateStringToDate(objItem.LastModified)
WScript.Echo "Name: " & objItem.Name
WScript.Echo "Path: " & objItem.Path
WScript.Echo "Readable: " & objItem.Readable
WScript.Echo "Status: " & objItem.Status
WScript.Echo "System: " & objItem.System
WScript.Echo "Writeable: " & objItem.Writeable
WScript.Echo
Next
Next

Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm:
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function

Passing argument to WMI Select statement in ExecQu

Posted: Tue Dec 04, 2007 2:04 am
by jvierra
Glad it was that easy. I had a big suspicion that, somewhere in the hard-to-read code, there wsa a simple coding mistake.

Consider functioning your code builds as it makes debugging easier and creates reusable code.

Consider the following as a demo and notice what it brings to the party:

Code: Select all

	

WMIGetFolder ".", "c:windows"
	
 
	
Function WMIGetFolder( sComputer, sFolderPath )
    Dim objWMIService,colItems
    
    sFolderPath = Replace(sFolderPath, "", "" )
    Set objWMIService = GetObject("winmgmts://" & sComputer & "/root/CIMV2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Directory where name = '" & sFolderPath & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	
    For Each objItem in colItems
        WScript.Echo "Caption: " & objItem.Caption
        WScript.Echo "Compressed: " & objItem.Compressed
        WScript.Echo "CreationDate: " & objItem.CreationDate
        WScript.Echo "CSName: " & objItem.CSName
        WScript.Echo "Description: " & objItem.Description
        WScript.Echo "Drive: " & objItem.Drive
        WScript.Echo "Extension: " & objItem.Extension
        WScript.Echo "FileName: " & objItem.FileName
        WScript.Echo "FileSize: " & objItem.FileSize
        WScript.Echo "FileType: " & objItem.FileType
    Next
    
End Function