none
Get Computers VBscript Issue RRS feed

  • Question

  • My setup consists of 1 server and 1 to 9 computers at multiple locations containing the same setup at each location but may vary on the amount of computers.  These computers that are attached to the server are the same name at each location, but may only have 2 computers at one location, and 5 at another and so forth.  The following script is what i have to get each computer name within a array setup so that it will go through each computer and output to a text file the findings.

    The issue i am having is that if 1 location has less than 9 computers (as you can see listed in my script).  It will continue to output the last computer it found until the script reaches the 9th computer.  This is because i have the 'On Error Resume Next" of course.

    Now, the script works fine.  But i would like to find a way such as a "on error" or pull from the active directory the computers available in the domain without having to modify too much of my script.

    Another issue is, some computers at the each location might not be on while others will be.  For example: If i run a script for 1 server and 3 computers for that location, The script will of course find the server, but if comp1 is offline, and since it is the first computer in the array of the script.  The script will error out and not finish with the other 2 computers that are online.  

    On Error Resume Next
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")							'Creates and saves a text document of results to the desktop'
    Set objFile = objFSO.CreateTextFile("System Information.txt", True)
    
    
    arrComputers = Array("Server","comp1","comp2","comp3","comp4","comp5","comp6","comp7","comp8","comp9")   	'Computers Listed that the script will search for'
    For Each strComputer In arrComputers
        objFile.Writeline
        objFile.Writeline "===================================="
        objFile.Writeline "Computer: "& strComputer								'The name of the computer for the following information listed below'
        objFile.Writeline "===================================="
    
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")				'Authentication level to obtain information from WMI app for local and remote computers'
    '---------------------------------------------------------------------------------------------'
    
    
    Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
    For Each objComputer in colSettings 
    
        objFile.Writeline "System Name: " & objComputer.Name						'Gets the computer name'
        objFile.Writeline "System Model: " & objComputer.Model						'Gets the computer model'
        objFile.Writeline "Total Physical Memory: " & objComputer.TotalPhysicalMemory			'Gets the computer installed memory"
    
    '---------------------------------------------------------------------------------------------'
    
    Next
    
    Set colSettings = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    
    For Each objOperatingSystem in colSettings 
        objFile.Writeline "OS Name: " & objOperatingSystem.Name						'Gets the computer OS'
    
    '---------------------------------------------------------------------------------------------'
    
    Next
    
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct") 
    For Each objItem in colItems 
    
        objFile.Writeline "Serial Number: " & objItem.IdentifyingNumber					'Gets the computer serial number'
    
    '---------------------------------------------------------------------------------------------'
    
    
    	Next
    Next



    Friday, April 24, 2015 3:44 PM

Answers

All replies

  • This is all made much, much simpler if you use PowerShell.


    $computerList = @( "server" "comp1" "comp2" # ...etc. ) foreach ( $computer in $computerList ) { if ( Test-Connection $computer -Count 1 -Quiet ) { $cs = Get-WMIObject Win32_ComputerSystem -ComputerName $computer $os = Get-WMIObject Win32_OperatingSystem -ComputerName $computer $csProduct = Get-WMIObject Win32_ComputerSystemProduct -ComputerName $computer New-Object PSObject -Property @{ "System Name" = $cs.Name "System Model" = $cs.Model "Total Physical Memory" = $cs.TotalPhysicalMemory / 1gb "OS Name" = $os.Name.Split('|')[0] "Serial Number" = $csProduct.IdentifyingNumber } } }


    You can put this in a script and export to CSV by piping to Export-CSV.

    This is also easy to extend by adding additional properties.

    I would also recommend using a parameter instead of hard-coding the computer names at the top of the script, but I'll leave that as a learning exercise.


    -- Bill Stewart [Bill_Stewart]

    Friday, April 24, 2015 3:55 PM
    Moderator
  • The first step when debugging a VBScript program is to remove (or comment out) the "On Error Resume Next" statement. Then you can see the error message and find the problem. If you must ignore an error, determine which statement raises the expected error and place the "On Error Resume Next" statement right before it. Then either handle the error or ignore it, but add the statement "On Error GoTo 0" right after, to restore normal error handling. Otherwise it will be very difficult to troubleshoot problems.

    Richard Mueller - MVP Directory Services

    Friday, April 24, 2015 4:40 PM
    Moderator
  • Well another issue is that these computers are XP based and the server is either 2003 or 2008.  Doesn't XP need powershell installed on it for the server to run the script?  Cause i know powershell is installed by default on server 2003, vista and higher...

    Richard- I already have turned off the "on error resume next" and the script errors out when it does not find the computer.  Which is why i stated in my question...

    "Another issue is, some computers at the each location might not be on while others will be.  For example: If i run a script for 1 server and 3 computers for that location, The script will of course find the server, but if comp1 is offline, and since it is the first computer in the array of the script.  The script will error out and not finish with the other 2 computers that are online."

    Friday, April 24, 2015 7:57 PM
  • You only need PowerShell installed on the machine that will run the script. It does not need to be installed on the servers.

    PS - why do you still have XP machines? Tempting fate?


    EDIT: Also, are you the OP? If so, why a different account?

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, April 24, 2015 7:59 PM
  • Mike Laughlin is correct. You only need PowerShell on the computer from which you run the script. PowerShell is connecting using WMI, the same as your VBScript is doing.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by FUSINON CHA0S Friday, April 24, 2015 10:43 PM
    Friday, April 24, 2015 8:35 PM
    Moderator