Jump to content
Sign in to follow this  
fu2m8

WMI Calls in AutoIT

Recommended Posts

fu2m8

Hey Guys,

Just a quick question regarding if I am doing something the right way.

Basically I need to get the asset tag value from the computer bios (this is passed to a program that renames the machine as they are built using our imaging process). I use the awesome CompInfo UDF by JSPatriot to get information for different types of machines (serial number, manufacturer etc) but for another certain type of machine I need the asset tag value (which I couldn't find a way to grab in his UDF).

Looking at his UDF I managed to whip up something similar in nature to the way he's functions work and it is working atm just wondering if I am doing it the right way or if there is an easier way to do it.

Global $cI_CompName = @ComputerName
Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20

$assetTag = ""

$wmiObject = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
$objectCollection = $wmiObject.ExecQuery("Select * from Win32_SystemEnclosure", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

For $objItem In $objectCollection
    $assetTag = $objItem.SMBIOSAssetTag
Next

ConsoleWrite("Asset Tag: " & $assetTag)

I don't really understand how to use objects properly and this was the best I could come up with and it is working however the thing I'm not sure about is do I need the For loop to set $assetTag. I was hoping i could do something like $assetTag = $objectCollection.SMBIOSAssetTag but this doesn't work. If someone could point out if this is fine to do it this way or show me an alternate method it would be much appreciated.

Thanks

Share this post


Link to post
Share on other sites
November

Hi,

Try to use my old code. I tried to do that, but i had some difficulties because in my network, some computers are not in the same domain, and therefore diferent secutity.

have fun... i hope this helps:

#include <GuiConstants.au3>
#include <file.au3>
#include <array.au3>

Opt ("TCPTimeout", 10)
Opt("TrayIconHide", 1)


Dim $strComputer, $askmachine, $Label_2, $Label_3, $Edit_3, $Button_4, $Button_5, $Button_6, $colItems, $gui, $aRecords, $lala, $lolo, $x, $Button_8, $filecopy, $location, $Button_9, $Errorhandle, $wbemFlagForwardOnly, $wbemFlagReturnImmediately, $bemFlagForwardOnly, $objWMIService

$strComputer = ""
$select = ""
$filecopy = 0
$Output = ""
$Errorhandle = ObjEvent("AutoIt.Error","Fatalerror")
$wbemFlagReturnImmediately = 0x10
$bemFlagForwardOnly = 0x20

GuiCreate("VendorProb", 395, 320,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$Radio_1 = GuiCtrlCreateRadio("Several Hosts", 10, 10, 100, 20)
$Radio_2 = GuiCtrlCreateRadio("One Host", 10, 30, 100, 20)
$askmachine = GUICtrlCreateDummy()
$Label_2 = GUICtrlCreateDummy()
$Edit_3 = GuiCtrlCreateEdit("", 10, 150, 370, 130)
$Button_4 = GuiCtrlCreateButton("Scan", 170, 20, 65, 30)
$Button_8 = GuiCtrlCreateButton("Export", 240, 20, 65, 30)
$Button_5 = GuiCtrlCreateButton("Exit", 310, 20, 65, 30)
$Button_7 = GuiCtrlCreateButton("Help", 310, 60, 65, 30)
$Button_6 = GUICtrlCreateDummy()
$Button_9 = GuiCtrlCreateButton("Clear Result", 240, 60, 65, 30)
$errorstate = GUICtrlCreateLabel("State : ", 10, 290, 200, 20)

GuiSetState()
While 1
    
    $msg = GuiGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exitn()
        Case $msg = $Button_5
            Exitn()
        Case $msg = $Button_7
            Help()
        Case $msg = $Button_4
            Select
                case $select = "many"
                    $status = $aRecords
                    If $status = "" Then
                        GUICtrlCreateLabel("State : Choose host file", 10, 290, 200, 20)
                        ContinueLoop
                    EndIf
                    prescanmany()
                case $select= "one"
                    $Status = GUICtrlRead($askmachine)
                    If $status = "" Then
                        GUICtrlCreateLabel("State : Type server name", 10, 290, 200, 20)
                        ContinueLoop
                    EndIf
                    prescanone()
            EndSelect
        Case $msg = $Button_6
            file()  
        Case $msg = $radio_2 And BitAND(GUICtrlRead($radio_2), $GUI_CHECKED) = $GUI_CHECKED
                $select = "One"
                GUICtrlDelete($Button_6)
                GUICtrlDelete($Label_3)
                $askmachine = GuiCtrlCreateInput("", 40, 105, 150, 20)
                $Label_2 = GuiCtrlCreateLabel("Server Name", 10, 80, 80, 20)
                GuiSetState()
        Case $msg = $radio_1 And BitAND(GUICtrlRead($radio_1), $GUI_CHECKED) = $GUI_CHECKED
                $select = "many"
                GUICtrlDelete($askmachine)
                GUICtrlDelete($Label_2)
                $Label_3 = GuiCtrlCreateLabel("Locate Host File", 10, 80, 150, 20)
                $Button_6 = GuiCtrlCreateButton("Host File", 40, 105, 150, 20)
                GuiSetState()
        Case $msg = $Button_8
            Export()
        Case $msg = $Button_9
            if $Output = 0 Then
                ContinueLoop
            EndIf
            GUICtrlSetData($Edit_3, "")
            $Output = ""
            GUICtrlCreateLabel("State : Last scan data was purged " & $strComputer, 10, 290, 200, 20)
    EndSelect
    
WEnd

Func file();Opens Host File and removes empty lines
    
    $host = FileOpenDialog("Host file location", @DesktopDir, "(*.txt)", 8+2)
        if @error = 1 Then
            $host = 0
            Return
        EndIf
    _FileReadToArray($host,$aRecords)
    while 1
        $newvar = _ArraySearch($aRecords, "", 0, 0, 0, false)
        if @error = 6 Then
            ExitLoop
        Else
            _ArrayDelete($aRecords, $newvar)
        EndIf
    wend
    GUICtrlCreateLabel("State : " & $aRecords[0] & " Serveres Loaded", 10, 290, 310, 20)
    _ArrayDelete($aRecords, 0)
    Return
    
EndFunc

Func prescanmany()
    
    If $status = "" Then
        GUICtrlCreateLabel("State : Choose a host file", 10, 290, 310, 20)
        Return
    EndIf
    Scanmany()
    
EndFunc

Func prescanone()
    
    If $status = "" Then
        GUICtrlCreateLabel("State : Type server name", 10, 290, 310, 20)
        Return
    EndIf
    Scanone()
    
EndFunc

Func scanmany()

$temp = UBound($aRecords, 1) - 1
for $x = 0 to $temp Step 1
    $strComputer = $status[$x]
    $ping = ping($status[$x], 2)
    if @error = 0 then
        GUICtrlCreateLabel("State : Scanning " & $strComputer, 10, 290, 310, 20)
        $colItems = ""
        $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
        if @error = 1 Then
            GUICtrlCreateLabel("State : " & $strComputer & " No permissions to scan server", 10, 290, 310, 20)
            $Output = $Output & "Server: " & $strComputer & "cannot be scanned" & @CRLF
        EndIf
        $colItems = $objWMIService.ExecQueryAsync("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
        ConsoleWrite($colItems)
        If IsObj($colItems) then
            For $objItem In $colItems
                $Output = $Output & "Server: " & $strComputer  & @CRLF
                $Output = $Output & "Vendor: " & $objItem.Vendor & @CRLF
                $Output = $Output & "Model: " & $objItem.Name & @CRLF
                $Output = $Output & "Serial Number: " & $objItem.IdentifyingNumber & @CRLF
                $Output = $Output & @CRLF
            Next
        EndIf
        $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
        If IsObj($colItems) Then
            for $objItem in $colItems
                $Output = $Output & "BIOS Version: " & $objItem.SMBIOSBIOSVersion & @CRLF
            Next
        EndIf
        ConsoleWrite($Output)
        ;Else
            ;Msgbox(0,"WMI Output","No WMI Objects Found. Check WMI for Server: " & $strComputer)
    else
        GUICtrlCreateLabel("State : " & $strComputer & " Error with server", 10, 290, 310, 20)
        $Output = $Output & "Server: " & $strComputer & " Not Found" & @CRLF
        $Output = $Output & @CRLF
    EndIf
        GUICtrlSetData($Edit_3, $Output)
Next
GUICtrlCreateLabel("State : Scan completed", 10, 290, 310, 20)

Return

EndFunc

Func scanone()

    $colItems = ""
    $strComputer = $status
    $Output=""
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
        If $objWMIService = 0 Then
            $Output = $Output & "Server: " & $strComputer  & " Not Found"
            GUICtrlSetData($Edit_3, $Output)
            Return
        EndIf
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    $colItems2 = $objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)                                      
    If IsObj($colItems) then
        For $objItem In $colItems
            $Output = $Output & "Server: " & $strComputer  & @CRLF
            $Output = $Output & "Vendor: " & $objItem.Vendor & @CRLF
            $Output = $Output & "Model: " & $objItem.Name & @CRLF
            $Output = $Output & "Serial Number: " & $objItem.IdentifyingNumber & @CRLF
        Next
    Else
        GUICtrlCreateLabel("State : " & $strComputer & " Error with server", 10, 290, 310, 20)
        $Output = $Output & "Server: " & $strComputer  & @CRLF & "Not Found"
        $Output = $Output & @CRLF
    Endif
    If IsObj($colItems2) Then
        for $objItem in $colItems2
            $Output = $Output & "BIOS Version: " & $objItem.SMBIOSBIOSVersion & @CRLF
        Next
    EndIf
GUICtrlSetData($Edit_3, $Output)
GUICtrlCreateLabel("State : Scan completed", 10, 290, 310, 20)
Return

EndFunc

Func logfile()
    
    MsgBox(0, "error", "NÂș do array : " & $x & " - Server : " & $strComputer)
    _ArrayDisplay($strComputer, "teste")
    
EndFunc

Func Export()
    
    if $Output = "" Then
        GUICtrlCreateLabel("State : Scan first " & $strComputer, 10, 290, 200, 20)
        Return
    Else
        $location = FileSelectFolder("Choose folder", "")
        If @error = 1 Then
            Return
        EndIf
        _FileCreate($location & "\exportvendor.txt")
        $outfile = GUICtrlRead($Edit_3, 1)
        $filecopy = FileWrite($location & "\exportvendor.txt",$outfile)
        If $filecopy = 0 Then
            GUICtrlCreateLabel("State : Export file not created ", 10, 290, 200, 20)
            $filecopy = 0
            Return
        EndIf
    EndIf
    GUICtrlCreateLabel("State : File created at " & $location & "\exportvendor.txt")
    
EndFunc

Func Fatalerror()
    
    GUICtrlCreateLabel("State : No access to server : " & $strComputer , 10, 290, 310, 20)
    $Output = $Output & "Server: " & $strComputer  & @CRLF & "Not Found"
    $Output = $Output & @CRLF
    $colItems = False
    $x = $x + 1
    ;MsgBox(0, "","" )
    ;MsgBox(0, "", $strComputer)
    ;ConsoleWrite($colItems)
    ConsoleWrite($objWMIService)
    Return
    
EndFunc

Func help()
    
    $help = MsgBox(64, "HELP"," VendorProb 1.1 " & @LF & "" & @LF & "Queries the Vendor, Model and S/N " & @LF & "" & @LF & "November 2007" & @LF & "Designed for Free use")
    GUICtrlSetBkColor($help,0xD3D3D3)
    Return
    
EndFunc

Func exitn()
    
    FileDelete($lolo)
    Exit
    
EndFunc

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
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
Sign in to follow this  

×