Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Wmi and Vbscript

  • 23-12-2011 11:00pm
    #1
    Closed Accounts Posts: 1,462 ✭✭✭


    Looking for a little help is someone has a second
    The below script Traverses AD and pulls computer names from it
    It then queries wmi and returns the type of GFX card and model of computer and writes it to a spreadsheet.
    It all works except that when it meets a machine it cannot query it uses the values from the last time the query ran sucessfully.
    I am having trouble figuring out a way to clear the variables
    IN my latest version I have I have tried setting ModelItem and Objitem to NUll at the end of the loop but that does not clear them
    As you can tell I am not much of programmer so any help would be appreciated :)
    Const ADS_SCOPE_SUBTREE = 2
    
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = True
    objExcel.Workbooks.Add
    row = 1
    col = 1
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand =   CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    
    Set objCOmmand.ActiveConnection = objConnection
    objCommand.CommandText = _
        "Select Name, Location from 'LDAP://DC=domain,DC=com' " _
            & "Where objectClass='computer'"  
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    
    Do Until objRecordSet.EOF
        Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
    
        row = row + 1
        On Error Resume Next
        col = 1
        'strComputer = "network5"
        strComputer = objRecordSet.Fields("Name").Value
        Wscript.Echo strComputer
        objExcel.Cells(row, 1 ) = strComputer
    Set objWMIService = GetObject(_
        "winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery( _
        "Select * from win32_videocontroller")
    For Each objItem in colItems
        Wscript.Echo "Device ID: " & objItem.DeviceID
        col = col + 1
        Wscript.Echo "PNP Device ID: " _
            & objItem.PNPDeviceID
        Wscript.Echo "Description: " _
            & objItem.Description
            col = col + 1
            objExcel.Cells(row, col) = objItem.Description
        Wscript.Echo
    Next
    On Error Resume Next
      Set modelItem = objWMIService.ExecQuery( _
        "Select * from win32_computersystem ")
    
        For Each objItem in ModelItem
            Wscript.Echo objItem.model
    
            Next
    
    ModelItem = Null
    ObjItem = Null
       objRecordSet.MoveNext
    Loop
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Well, it's been a while since I did vbscript but if I had to hazard a guess I'd say you're resumption on error probably isn't a good idea. Well, I mean you have to do it because that's the way VBScript is, but you probably need to check if the call failed or not!

    If this line fails:
    Set objWMIService = GetObject(_
        "winmgmts:\\" & strComputer & "\root\cimv2")
    

    Then my feeling is that objWMIService (or colitems) would remain unchanged and you'd query the last computer again instead. You really need to check if it failed by querying the Err object. Like this:
    Set objWMIService = GetObject("winmgmts://" & Computer)
    
    If Err.Number <> 0 Then
        Wscript.Echo Computer & " " & Err.Description
         Err.Clear
    Else
        Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk")
        For Each objLogicalDisk In colLogicalDisk
    
         .....
        Next
    End If
    

    etc etc. Programming is all about error checking and 99% of the time it ain't optional. Imagine if you were moving files to a remote computer:

    Connect : Fails (resumes next)
    Copy: Fails (resumes next)
    delete local files: Succeeds

    Goody. No files on remote or local computer!

    Hope this helps you...


  • Closed Accounts Posts: 1,462 ✭✭✭red menace


    I am sorry that I can only thank this once!
    You are my hero :)
    Thanks for this you have helped me no end and taught me an important lesson!

    Thanks again
    Shane


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Glad I could help!


Advertisement