Jump to content

Best WMI Reference?


PsaltyDS
 Share

Recommended Posts

Howdy all. :geek:

I never make time to learn things until I have to for some task. When I do try to learn something without a task that needs doing, it never sticks much in the swiss-cheese brain. :">

I've been meaning to learn some COM stuff in AutoIT, and I've been slapped with a task that needs doing to make it possible I may even remember it! :o

I need to detect the identity of a server's tape drive to pass to removable storage manager (RSM). Someone else had done it with Windows 2000 using a .vb script:

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_TapeDrive",,48)
For Each objItem in colItems
    Wscript.Echo objItem.Id 
Next

In Windows 2003, with a working tape drive, this .vbs script returns: "null"

That code used to work on that person's Windows 2000 load. Part of my problem in adapting it is I have no idea where he got "cimv2" from. What I need is a good reference to find out how proper calls to WMI are formatted, and if they changed in this area from 2000 to 2003. I don't even need anyone to look it up for me, I'd just like to know what y'all consider a good reference for looking that up, so I can apply this knowledge to other WMI functions beside tape drives in the future... I'd even like to just 'browse' the list and see what other kinds of trouble I can get into with WMI...

Thanks in advance. ;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Found another .vbs script on MSDN and added it to what I had:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
    "Select * from Win32_TapeDrive")
Wscript.Echo "Number of tape drives: " _
    & colItems.Count

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_TapeDrive",,48)
For Each objItem in colItems
    Wscript.Echo objItem.Id 
Next

Now it returns "Number of tape drives: 1" and then "null". So it does see that there is a tape drive, but does not get the ID correctly. I'm suspicious of that ("Select * from Win32_TapeDrive",,48) part now... or maybe it doesn't like "objItem.Id"... ???

Hmm.... :o

P.S. Thanks for the link, gafrost, I'm reading there now...

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_TapeDrive", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Availability: " & $objItem.Availability & @CRLF
      $strCapabilities = $objItem.Capabilities(0)
      $Output = $Output & "Capabilities: " & $strCapabilities & @CRLF
      $strCapabilityDescriptions = $objItem.CapabilityDescriptions(0)
      $Output = $Output & "CapabilityDescriptions: " & $strCapabilityDescriptions & @CRLF
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $Output = $Output & "Compression: " & $objItem.Compression & @CRLF
      $Output = $Output & "CompressionMethod: " & $objItem.CompressionMethod & @CRLF
      $Output = $Output & "ConfigManagerErrorCode: " & $objItem.ConfigManagerErrorCode & @CRLF
      $Output = $Output & "ConfigManagerUserConfig: " & $objItem.ConfigManagerUserConfig & @CRLF
      $Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF
      $Output = $Output & "DefaultBlockSize: " & $objItem.DefaultBlockSize & @CRLF
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF
      $Output = $Output & "ECC: " & $objItem.ECC & @CRLF
      $Output = $Output & "EOTWarningZoneSize: " & $objItem.EOTWarningZoneSize & @CRLF
      $Output = $Output & "ErrorCleared: " & $objItem.ErrorCleared & @CRLF
      $Output = $Output & "ErrorDescription: " & $objItem.ErrorDescription & @CRLF
      $Output = $Output & "ErrorMethodology: " & $objItem.ErrorMethodology & @CRLF
      $Output = $Output & "FeaturesHigh: " & $objItem.FeaturesHigh & @CRLF
      $Output = $Output & "FeaturesLow: " & $objItem.FeaturesLow & @CRLF
      $Output = $Output & "Id: " & $objItem.Id & @CRLF
      $Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
      $Output = $Output & "LastErrorCode: " & $objItem.LastErrorCode & @CRLF
      $Output = $Output & "Manufacturer: " & $objItem.Manufacturer & @CRLF
      $Output = $Output & "MaxBlockSize: " & $objItem.MaxBlockSize & @CRLF
      $Output = $Output & "MaxMediaSize: " & $objItem.MaxMediaSize & @CRLF
      $Output = $Output & "MaxPartitionCount: " & $objItem.MaxPartitionCount & @CRLF
      $Output = $Output & "MediaType: " & $objItem.MediaType & @CRLF
      $Output = $Output & "MinBlockSize: " & $objItem.MinBlockSize & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NeedsCleaning: " & $objItem.NeedsCleaning & @CRLF
      $Output = $Output & "NumberOfMediaSupported: " & $objItem.NumberOfMediaSupported & @CRLF
      $Output = $Output & "Padding: " & $objItem.Padding & @CRLF
      $Output = $Output & "PNPDeviceID: " & $objItem.PNPDeviceID & @CRLF
      $strPowerManagementCapabilities = $objItem.PowerManagementCapabilities(0)
      $Output = $Output & "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF
      $Output = $Output & "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF
      $Output = $Output & "ReportSetMarks: " & $objItem.ReportSetMarks & @CRLF
      $Output = $Output & "Status: " & $objItem.Status & @CRLF
      $Output = $Output & "StatusInfo: " & $objItem.StatusInfo & @CRLF
      $Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_TapeDrive" )
Endif


Func WMIDateStringToDate($dtmDate)

 Return (StringMid($dtmDate, 5, 2) & "/" & _
 StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
 & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

CODE

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10

$wbemFlagForwardOnly = 0x20

$colItems = ""

$strComputer = "localhost"

$Output=""

$Output = $Output & "Computer: " & $strComputer & @CRLF

$Output = $Output & "==========================================" & @CRLF

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_TapeDrive", "WQL", _

$wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then

For $objItem In $colItems

$Output = $Output & "Availability: " & $objItem.Availability & @CRLF

$strCapabilities = $objItem.Capabilities(0)

$Output = $Output & "Capabilities: " & $strCapabilities & @CRLF

$strCapabilityDescriptions = $objItem.CapabilityDescriptions(0)

$Output = $Output & "CapabilityDescriptions: " & $strCapabilityDescriptions & @CRLF

$Output = $Output & "Caption: " & $objItem.Caption & @CRLF

$Output = $Output & "Compression: " & $objItem.Compression & @CRLF

$Output = $Output & "CompressionMethod: " & $objItem.CompressionMethod & @CRLF

$Output = $Output & "ConfigManagerErrorCode: " & $objItem.ConfigManagerErrorCode & @CRLF

$Output = $Output & "ConfigManagerUserConfig: " & $objItem.ConfigManagerUserConfig & @CRLF

$Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF

$Output = $Output & "DefaultBlockSize: " & $objItem.DefaultBlockSize & @CRLF

$Output = $Output & "Description: " & $objItem.Description & @CRLF

$Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF

$Output = $Output & "ECC: " & $objItem.ECC & @CRLF

$Output = $Output & "EOTWarningZoneSize: " & $objItem.EOTWarningZoneSize & @CRLF

$Output = $Output & "ErrorCleared: " & $objItem.ErrorCleared & @CRLF

$Output = $Output & "ErrorDescription: " & $objItem.ErrorDescription & @CRLF

$Output = $Output & "ErrorMethodology: " & $objItem.ErrorMethodology & @CRLF

$Output = $Output & "FeaturesHigh: " & $objItem.FeaturesHigh & @CRLF

$Output = $Output & "FeaturesLow: " & $objItem.FeaturesLow & @CRLF

$Output = $Output & "Id: " & $objItem.Id & @CRLF

$Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF

$Output = $Output & "LastErrorCode: " & $objItem.LastErrorCode & @CRLF

$Output = $Output & "Manufacturer: " & $objItem.Manufacturer & @CRLF

$Output = $Output & "MaxBlockSize: " & $objItem.MaxBlockSize & @CRLF

$Output = $Output & "MaxMediaSize: " & $objItem.MaxMediaSize & @CRLF

$Output = $Output & "MaxPartitionCount: " & $objItem.MaxPartitionCount & @CRLF

$Output = $Output & "MediaType: " & $objItem.MediaType & @CRLF

$Output = $Output & "MinBlockSize: " & $objItem.MinBlockSize & @CRLF

$Output = $Output & "Name: " & $objItem.Name & @CRLF

$Output = $Output & "NeedsCleaning: " & $objItem.NeedsCleaning & @CRLF

$Output = $Output & "NumberOfMediaSupported: " & $objItem.NumberOfMediaSupported & @CRLF

$Output = $Output & "Padding: " & $objItem.Padding & @CRLF

$Output = $Output & "PNPDeviceID: " & $objItem.PNPDeviceID & @CRLF

$strPowerManagementCapabilities = $objItem.PowerManagementCapabilities(0)

$Output = $Output & "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF

$Output = $Output & "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF

$Output = $Output & "ReportSetMarks: " & $objItem.ReportSetMarks & @CRLF

$Output = $Output & "Status: " & $objItem.Status & @CRLF

$Output = $Output & "StatusInfo: " & $objItem.StatusInfo & @CRLF

$Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF

$Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF

if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop

$Output=""

Next

Else

Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_TapeDrive" )

Endif

Func WMIDateStringToDate($dtmDate)

Return (StringMid($dtmDate, 5, 2) & "/" & _

StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _

& " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))

EndFunc

Hmmm... :geek:

Two functions are not recognized by SciTE editor: ObjGet, and IsObj.

When I run your script with F5 it fails (as expected), but when I run it Alt-F5(Beta) is gets no errors, but finishes without any output at all. I'm running it under beta, but I'll be double checking my version (106, I thought).

Update: My AutoIT Beta was up to date (106), but I needed to update SciTE. Now it recognizes those functions, but the behavior has not changed. When I run your script Beta (Alt-F5) it ends without errors and produces no output or popups at all!

>"C:\Program Files\AutoIt3\SciTe\CompileAU3\CompileAU3.exe" /run /beta /ErrorStdOut /in "\\MyServer\MyShare\Scripting\MyFolder\MyScript.au3" /autoit3dir "C:\Program Files\AutoIt3\beta" /UserParams    
>Running AU3Check...C:\Program Files\AutoIt3\SciTe\Defs\Unstable\Au3Check\au3check.dat
>AU3Check Ended. No Error(s).
>Running: (3.1.1.106):C:\Program Files\AutoIt3\beta\autoit3.exe "\\MyServer\MyShare\Scripting\MyFolder\MyScript.au3"    
>AutoIT3.exe ended.
>Exit code: 0   Time: 0.998

I'm... more confused than normal... :o

Update II: OS version funkiness! I'm scripting on an XP box and symptoms are as above. I compiled the script Beta (Alt-F7) and tried it on a 2000 Advanced server with a tape, and got same symptoms as above. Finaly tried it on a 2003 server and it worked! So I can see bad data if earlier version of Windows don't support that particular object, but how can the OS version stop it from producing any output at all? Your If/Else/EndIf, for example, has a MsgBox() for both the If and Else portion, so how did it avoid showing either without any errors?

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

might try adding in the & "{impersonationLevel=impersonate}!\\"

Made the following change with no difference in symptoms:

$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\CIMV2")

Made the following change for debug puposes and determined that the For/Next loop is not executed at all except for Windows 2003:

If IsObj ($colItems) Then
    For $objItem In $colItems
        $Output = $Output & "Availability: " & $objItem.Availability & @CRLF
        MsgBox(64, "Debug Info", "$Output = " & $Output)
        $strCapabilities = $objItem.Capabilities (0)
        $Output = $Output & "Capabilities: " & $strCapabilities & @CRLF

Funkiness is everywhere!

Mind you, it works in 2003 now, which was my original question. I'm just trying to wrap my head around it NOT working for XP and 2000AS so I'll be smarter on my next try.

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...