Solved

How can I add additional code to a server inventory script to calculate disk space as well?

Posted on 2011-02-28
14
232 Views
Last Modified: 2012-05-11
I have a script that I use to calculate my server inventory but I need to add disk space to it as well.  I have attached the existing script that I use.

Thanks,
sOU1 = "LDAP://OU=Domain Controllers,
sOU2 = "LDAP://OU=Servers,
sOU3 = "LDAP://OU=Terminal Servers,
sOU4 = "LDAP://OU=Domain Controllers,
sAdminUserName = 
sAdminPassword = 

On Error Resume Next

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oOutputFile = oFS.CreateTextFile("All Servers " & Replace(Replace(Now, "/", "_"), ":", ".") & ".csv", True)

oOutputFile.WriteLine "Server Name,Operating System,Service Pack,IP Address,Manufacturer,Model,ModelNumber,Serial Number"

Call EnumerateObject(GetObject(sOU1))
Call EnumerateObject(GetObject(sOU2))
Call EnumerateObject(GetObject(sOU3))
Call EnumerateObject(GetObject(sOU4))

oOutputFile.Close
Set oOutputFile = Nothing
Set oFS = Nothing
Msgbox "Complete."

Sub EnumerateObject(oContainer)
    On Error Resume Next

    For Each oObject In oContainer
        Select Case LCase(oObject.Class)
               Case "computer"
                   sComputerName = Left(oObject.sAMAccountName, Len(oObject.sAMAccountName) - 1)
                   sPCData = GetPCData(sComputerName)
                   aFields = Split(sPCData, "|")
                   oOutputFile.WriteLine Quote(oObject.Get("Name")) & "," & Quote(oObject.operatingSystem) & "," & Quote(oObject.operatingSystemServicePack) & "," & _
                                         Quote(aFields(0)) & "," & Quote(aFields(1)) & "," & Quote(aFields(2)) & "," & Quote(aFields(3)) & "," & Quote(aFields(4))
                   sPCData = ""
               Case "organizationalunit", "container"
                    Call EnumerateObject(oObject)
        End Select
    Next
End Sub

'************************************************************************************
' Put the passed text into quotes
'************************************************************************************
Function Quote(sText)
    Quote = chr(34) & sText & chr(34)
End Function

'************************************************************************************
' Get WMI data from the PC
'************************************************************************************
Function GetPCData(sTempName)
    On Error Resume Next

    If Ping(sTempName) = True Then
        Set oLocator = CreateObject("WbemScripting.SWbemLocator")
        oLocator.Security_.ImpersonationLevel = 3
        oLocator.Security_.AuthenticationLevel = 0

        Set oWMI = oLocator.ConnectServer(sTempName, "root\cimv2", sAdminUserName, sAdminPassword)

        'IPAddress
        Set cIPAddresses = oWMI.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
        For Each oIPAddress In cIPAddresses
            sIPAddress = oIPAddress.IPAddress(0)
        Next
        Set cIPAddresses = Nothing

        'Manufacturer and Model Number
        Set cComputerSystems = oWMI.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem")
        For Each oComputerSystem In cComputerSystems
            sManufacturer = oComputerSystem.Manufacturer
            sModelNumber = oComputerSystem.Model
        Next
        Set cComputerSystems = Nothing

        'Model and Serial Number
        Set cComputerSystemProducts = oWMI.ExecQuery("SELECT IdentifyingNumber,Version FROM Win32_ComputerSystemProduct")
        For Each oComputerSystemProduct In cComputerSystemProducts
            sSerialNumber = oComputerSystemProduct.IdentifyingNumber
            sModel = oComputerSystemProduct.Version
        Next
        Set cComputerSystemProducts = Nothing

        Set oWMI = Nothing
        Set oLocator = Nothing
    End If

    GetPCData = sIPAddress & "|" & sManufacturer & "|" & sModel & "|" & sModelNumber & "|" & sSerialNumber
End Function

'************************************************************************************
' Ping the passed computer name or IP
'************************************************************************************
Function Ping(sHost)
    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")

    Set cPings = oWMILocal.ExecQuery("SELECT * FROM Win32_PingStatus " &_
        "WHERE Address = '" & sHost & "' AND timeout = 500")
    For Each oPing In cPings
        iPingResult = oPing.StatusCode
        If (iPingResult = 0) Then
            Ping = True
            Exit Function
        Else
            Ping = False
        End If
    Next
End Function

Open in new window

0
Comment
Question by:bob_kochanski
  • 7
  • 7
14 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 34998679
Try this code

sOU1 = "LDAP://OU=Domain Controllers,
sOU2 = "LDAP://OU=Servers,
sOU3 = "LDAP://OU=Terminal Servers,
sOU4 = "LDAP://OU=Domain Controllers,
sAdminUserName =
sAdminPassword =

On Error Resume Next

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oOutputFile = oFS.CreateTextFile("All Servers " & Replace(Replace(Now, "/", "_"), ":", ".") & ".csv", True)

oOutputFile.WriteLine "Server Name,Operating System,Service Pack,IP Address,Manufacturer,Model,ModelNumber,Serial Number"

Call EnumerateObject(GetObject(sOU1))
Call EnumerateObject(GetObject(sOU2))
Call EnumerateObject(GetObject(sOU3))
Call EnumerateObject(GetObject(sOU4))

oOutputFile.Close
Set oOutputFile = Nothing
Set oFS = Nothing
MsgBox "Complete."

Sub EnumerateObject(oContainer)
    On Error Resume Next
   
    For Each oObject In oContainer
        Select Case LCase(oObject.Class)
            Case "computer"
            sComputerName = Left(oObject.sAMAccountName, Len(oObject.sAMAccountName) - 1)
            sPCData = GetPCData(sComputerName)
            aFields = Split(sPCData, "|")
            oOutputFile.WriteLine Quote(oObject.Get("Name")) & "," & Quote(oObject.operatingSystem) & "," & Quote(oObject.operatingSystemServicePack) & "," & _
            Quote(aFields(0)) & "," & Quote(aFields(1)) & "," & Quote(aFields(2)) & "," & Quote(aFields(3)) & "," & Quote(aFields(4))
            sPCData = ""
            Case "organizationalunit", "container"
            Call EnumerateObject(oObject)
        End Select
    Next
End Sub

'************************************************************************************
' Put the passed text into quotes
'************************************************************************************
Function Quote(sText)
    Quote = Chr(34) & sText & Chr(34)
End Function

'************************************************************************************
' Get WMI data from the PC
'************************************************************************************
Function GetPCData(sTempName)
    On Error Resume Next
   
    If Ping(sTempName) = True Then
        Set oLocator = CreateObject("WbemScripting.SWbemLocator")
        oLocator.Security_.ImpersonationLevel = 3
        oLocator.Security_.AuthenticationLevel = 0
       
        Set oWMI = oLocator.ConnectServer(sTempName, "root\cimv2", sAdminUserName, sAdminPassword)
       
        'IPAddress
        Set cIPAddresses = oWMI.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
        For Each oIPAddress In cIPAddresses
            sIPAddress = oIPAddress.IPAddress(0)
        Next
        Set cIPAddresses = Nothing
       
        'Manufacturer and Model Number
        Set cComputerSystems = oWMI.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem")
        For Each oComputerSystem In cComputerSystems
            sManufacturer = oComputerSystem.Manufacturer
            sModelNumber = oComputerSystem.Model
        Next
        Set cComputerSystems = Nothing
       
        'Model and Serial Number
        Set cComputerSystemProducts = oWMI.ExecQuery("SELECT IdentifyingNumber,Version FROM Win32_ComputerSystemProduct")
        For Each oComputerSystemProduct In cComputerSystemProducts
            sSerialNumber = oComputerSystemProduct.IdentifyingNumber
            sModel = oComputerSystemProduct.Version
        Next
        Set cComputerSystemProducts = Nothing
       
        'Disk Space Check
       
        strdisk=""
       
        Set colItems = oWMI.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
        strdisk=strdisk & vbCrLf & vbCrLf & "Drive" & vbTab & "DiskSize" & vbTab & "FreeSpace" & vbTab & "FreeSpace" & "%" & vbCrLf
       
        For Each objItem In colItems
            pctFreeSpace = Round(((objItem.FreeSpace / objItem.Size) * 1000)/10,2)
            strDiskSize = Round((objItem.Size /1073741824),2) & "Gb"
            If Round((objItem.Size /1073741824),2) < 1 Then
                strFreeSpace=Round((objItem.FreeSpace /1048576),2) & "Mb"
            Else
                strFreeSpace = Round((objItem.FreeSpace /1073741824),2) & "Gb"
            End If                
            strUsedSpace = Round(((objItem.Size-objItem.FreeSpace)/1073741824),2) & "Gb"
            strdisk = strdisk & objItem.Name & vbTab & strDiskSize & Space(8-Len(strDiskSize)) & vbTab & strFreeSpace & Space(8-Len(strFreeSpace))& vbTab & pctFreeSpace & vbCrLf
        Next

       
        Set oWMI = Nothing
        Set oLocator = Nothing
    End If
   
    GetPCData = sIPAddress & "|" & sManufacturer & "|" & sModel & "|" & sModelNumber & "|" & sSerialNumber & vbCrLf & strdisk
End Function

'************************************************************************************
' Ping the passed computer name or IP
'************************************************************************************
Function Ping(sHost)
    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")
   
    Set cPings = oWMILocal.ExecQuery("SELECT * FROM Win32_PingStatus " &_
    "WHERE Address = '" & sHost & "' AND timeout = 500")
    For Each oPing In cPings
        iPingResult = oPing.StatusCode
        If (iPingResult = 0) Then
            Ping = True
            Exit Function
        Else
            Ping = False
        End If
    Next
End Function

0
 

Author Comment

by:bob_kochanski
ID: 34998848
This didn't work, it gave me everything but the disk space.
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 34999072
Some minor changes, please try now, I have also given a msg box for diskspace, to test if it is returning values
       

sOU1 = "LDAP://OU=Domain Controllers,
sOU2 = "LDAP://OU=Servers,
sOU3 = "LDAP://OU=Terminal Servers,
sOU4 = "LDAP://OU=Domain Controllers,
sAdminUserName =
sAdminPassword =

On Error Resume Next

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oOutputFile = oFS.CreateTextFile("All Servers " & Replace(Replace(Now, "/", "_"), ":", ".") & ".csv", True)

oOutputFile.WriteLine "Server Name,Operating System,Service Pack,IP Address,Manufacturer,Model,ModelNumber,Serial Number"

Call EnumerateObject(GetObject(sOU1))
Call EnumerateObject(GetObject(sOU2))
Call EnumerateObject(GetObject(sOU3))
Call EnumerateObject(GetObject(sOU4))

oOutputFile.Close
Set oOutputFile = Nothing
Set oFS = Nothing
MsgBox "Complete."

Sub EnumerateObject(oContainer)
    On Error Resume Next
   
    For Each oObject In oContainer
        Select Case LCase(oObject.Class)
            Case "computer"
            sComputerName = Left(oObject.sAMAccountName, Len(oObject.sAMAccountName) - 1)
            sPCData = GetPCData(sComputerName)
            aFields = Split(sPCData, "|")
            oOutputFile.WriteLine Quote(oObject.Get("Name")) & "," & Quote(oObject.operatingSystem) & "," & Quote(oObject.operatingSystemServicePack) & "," & _
            Quote(aFields(0)) & "," & Quote(aFields(1)) & "," & Quote(aFields(2)) & "," & Quote(aFields(3)) & "," & Quote(aFields(4)& Quote(aFields(5))
            sPCData = ""
            Case "organizationalunit", "container"
            Call EnumerateObject(oObject)
        End Select
    Next
End Sub

'************************************************************************************
' Put the passed text into quotes
'************************************************************************************
Function Quote(sText)
    Quote = Chr(34) & sText & Chr(34)
End Function

'************************************************************************************
' Get WMI data from the PC
'************************************************************************************
Function GetPCData(sTempName)
    On Error Resume Next
   
    If Ping(sTempName) = True Then
        Set oLocator = CreateObject("WbemScripting.SWbemLocator")
        oLocator.Security_.ImpersonationLevel = 3
        oLocator.Security_.AuthenticationLevel = 0
       
        Set oWMI = oLocator.ConnectServer(sTempName, "root\cimv2", sAdminUserName, sAdminPassword)
       
        'IPAddress
        Set cIPAddresses = oWMI.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
        For Each oIPAddress In cIPAddresses
            sIPAddress = oIPAddress.IPAddress(0)
        Next
        Set cIPAddresses = Nothing
       
        'Manufacturer and Model Number
        Set cComputerSystems = oWMI.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem")
        For Each oComputerSystem In cComputerSystems
            sManufacturer = oComputerSystem.Manufacturer
            sModelNumber = oComputerSystem.Model
        Next
        Set cComputerSystems = Nothing
       
        'Model and Serial Number
        Set cComputerSystemProducts = oWMI.ExecQuery("SELECT IdentifyingNumber,Version FROM Win32_ComputerSystemProduct")
        For Each oComputerSystemProduct In cComputerSystemProducts
            sSerialNumber = oComputerSystemProduct.IdentifyingNumber
            sModel = oComputerSystemProduct.Version
        Next
        Set cComputerSystemProducts = Nothing
       
        'Disk Space Check
       
        strdisk=""
       
        Set colItems = oWMI.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
        strdisk=strdisk & vbCrLf & vbCrLf & "Drive" & vbTab & "DiskSize" & vbTab & "FreeSpace" & vbTab & "FreeSpace" & "%" & vbCrLf
       
        For Each objItem In colItems
            pctFreeSpace = Round(((objItem.FreeSpace / objItem.Size) * 1000)/10,2)
            strDiskSize = Round((objItem.Size /1073741824),2) & "Gb"
            If Round((objItem.Size /1073741824),2) < 1 Then
                strFreeSpace=Round((objItem.FreeSpace /1048576),2) & "Mb"
            Else
                strFreeSpace = Round((objItem.FreeSpace /1073741824),2) & "Gb"
            End If                
            strUsedSpace = Round(((objItem.Size-objItem.FreeSpace)/1073741824),2) & "Gb"
            strdisk = strdisk & objItem.Name & vbTab & strDiskSize & Space(8-Len(strDiskSize)) & vbTab & strFreeSpace & Space(8-Len(strFreeSpace))& vbTab & pctFreeSpace & vbCrLf
        Next

        MsgBox strdisk
       
        Set oWMI = Nothing
        Set oLocator = Nothing
    End If
   
    GetPCData = sIPAddress & "|" & sManufacturer & "|" & sModel & "|" & sModelNumber & "|" & sSerialNumber & "|" & vbCrLf & strdisk
End Function

'************************************************************************************
' Ping the passed computer name or IP
'************************************************************************************
Function Ping(sHost)
    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")
   
    Set cPings = oWMILocal.ExecQuery("SELECT * FROM Win32_PingStatus " &_
    "WHERE Address = '" & sHost & "' AND timeout = 500")
    For Each oPing In cPings
        iPingResult = oPing.StatusCode
        If (iPingResult = 0) Then
            Ping = True
            Exit Function
        Else
            Ping = False
        End If
    Next
End Function

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:bob_kochanski
ID: 34999759
OK, it's definitely pulling the disk space now but it's not listing it in the .xlsx spreadsheet along with the rest of the server information.

Thanks,
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 34999801
can you give the .xls format for disk space, as we will have multiple values
0
 

Author Comment

by:bob_kochanski
ID: 35000028
OK, so I changed the format to .xls but I am not sure where to make the change on the column formatting because it seems to be in the same column with the serial number.  See attached.
Disk-Space.jpg
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35000134
Try this code, made some changes to reflect formatting in new column

sOU1 = "LDAP://OU=Domain Controllers,
sOU2 = "LDAP://OU=Servers,
sOU3 = "LDAP://OU=Terminal Servers,
sOU4 = "LDAP://OU=Domain Controllers,
sAdminUserName =
sAdminPassword =

On Error Resume Next

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oOutputFile = oFS.CreateTextFile("All Servers " & Replace(Replace(Now, "/", "_"), ":", ".") & ".csv", True)

oOutputFile.WriteLine "Server Name,Operating System,Service Pack,IP Address,Manufacturer,Model,ModelNumber,Serial Number"

Call EnumerateObject(GetObject(sOU1))
Call EnumerateObject(GetObject(sOU2))
Call EnumerateObject(GetObject(sOU3))
Call EnumerateObject(GetObject(sOU4))

oOutputFile.Close
Set oOutputFile = Nothing
Set oFS = Nothing
MsgBox "Complete."

Sub EnumerateObject(oContainer)
    On Error Resume Next
   
    For Each oObject In oContainer
        Select Case LCase(oObject.Class)
            Case "computer"
            sComputerName = Left(oObject.sAMAccountName, Len(oObject.sAMAccountName) - 1)
            sPCData = GetPCData(sComputerName)
            aFields = Split(sPCData, "|")
            oOutputFile.WriteLine Quote(oObject.Get("Name")) & "," & Quote(oObject.operatingSystem) & "," & Quote(oObject.operatingSystemServicePack) & "," & _
            Quote(aFields(0)) & "," & Quote(aFields(1)) & "," & Quote(aFields(2)) & "," & Quote(aFields(3)) & "," & Quote(aFields(4)& "," & Quote(aFields(5))
            sPCData = ""
            Case "organizationalunit", "container"
            Call EnumerateObject(oObject)
        End Select
    Next
End Sub

'************************************************************************************
' Put the passed text into quotes
'************************************************************************************
Function Quote(sText)
    Quote = Chr(34) & sText & Chr(34)
End Function

'************************************************************************************
' Get WMI data from the PC
'************************************************************************************
Function GetPCData(sTempName)
    On Error Resume Next
   
    If Ping(sTempName) = True Then
        Set oLocator = CreateObject("WbemScripting.SWbemLocator")
        oLocator.Security_.ImpersonationLevel = 3
        oLocator.Security_.AuthenticationLevel = 0
       
        Set oWMI = oLocator.ConnectServer(sTempName, "root\cimv2", sAdminUserName, sAdminPassword)
       
        'IPAddress
        Set cIPAddresses = oWMI.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
        For Each oIPAddress In cIPAddresses
            sIPAddress = oIPAddress.IPAddress(0)
        Next
        Set cIPAddresses = Nothing
       
        'Manufacturer and Model Number
        Set cComputerSystems = oWMI.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem")
        For Each oComputerSystem In cComputerSystems
            sManufacturer = oComputerSystem.Manufacturer
            sModelNumber = oComputerSystem.Model
        Next
        Set cComputerSystems = Nothing
       
        'Model and Serial Number
        Set cComputerSystemProducts = oWMI.ExecQuery("SELECT IdentifyingNumber,Version FROM Win32_ComputerSystemProduct")
        For Each oComputerSystemProduct In cComputerSystemProducts
            sSerialNumber = oComputerSystemProduct.IdentifyingNumber
            sModel = oComputerSystemProduct.Version
        Next
        Set cComputerSystemProducts = Nothing
       
        'Disk Space Check
       
        strdisk=""
       
        Set colItems = oWMI.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
        strdisk=strdisk & vbCrLf & vbCrLf & "Drive" & vbTab & "DiskSize" & vbTab & "FreeSpace" & vbTab & "FreeSpace%" & vbCrLf
       
        For Each objItem In colItems
            pctFreeSpace = Round(((objItem.FreeSpace / objItem.Size) * 1000)/10,2)
            strDiskSize = Round((objItem.Size /1073741824),2) & "Gb"
            If Round((objItem.Size /1073741824),2) < 1 Then
                strFreeSpace=Round((objItem.FreeSpace /1048576),2) & "Mb"
            Else
                strFreeSpace = Round((objItem.FreeSpace /1073741824),2) & "Gb"
            End If                
            strUsedSpace = Round(((objItem.Size-objItem.FreeSpace)/1073741824),2) & "Gb"
            strdisk = strdisk & objItem.Name & vbTab & strDiskSize & Space(8-Len(strDiskSize)) & vbTab & strFreeSpace & Space(8-Len(strFreeSpace))& vbTab & pctFreeSpace & vbCrLf
        Next

        MsgBox strdisk
       
        Set oWMI = Nothing
        Set oLocator = Nothing
    End If
   
    GetPCData = sIPAddress & "|" & sManufacturer & "|" & sModel & "|" & sModelNumber & "|" & sSerialNumber & "|" & strdisk
End Function

'************************************************************************************
' Ping the passed computer name or IP
'************************************************************************************
Function Ping(sHost)
    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")
   
    Set cPings = oWMILocal.ExecQuery("SELECT * FROM Win32_PingStatus " &_
    "WHERE Address = '" & sHost & "' AND timeout = 500")
    For Each oPing In cPings
        iPingResult = oPing.StatusCode
        If (iPingResult = 0) Then
            Ping = True
            Exit Function
        Else
            Ping = False
        End If
    Next
End Function
0
 

Author Comment

by:bob_kochanski
ID: 35001560
OK, everything looks good at this point, although I should have expanded on my question originally as I would like to have a column for the type of processor as well as a column for how much memory.

Thanks again,
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35004353
Hi,

Can you be more specific as to what info you mean by processor type?

Prashanth
0
 

Author Comment

by:bob_kochanski
ID: 35007618
Sorry prashanthd, just being able to pull what type of processor resides in the servers if that is even possible.

Thanks,
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35009360
Try the following code, Included Processor Type and RAM

sOU1 = "LDAP://OU=Domain Controllers,
sOU2 = "LDAP://OU=Servers,
sOU3 = "LDAP://OU=Terminal Servers,
sOU4 = "LDAP://OU=Domain Controllers,
sAdminUserName =
sAdminPassword =

On Error Resume Next

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oOutputFile = oFS.CreateTextFile("All Servers " & Replace(Replace(Now, "/", "_"), ":", ".") & ".csv", True)

oOutputFile.WriteLine "Server Name,Operating System,Service Pack,IP Address,Manufacturer,Model,ModelNumber,Serial Number,Processor Type,Physical RAM, Disk Space"

Call EnumerateObject(GetObject(sOU1))
Call EnumerateObject(GetObject(sOU2))
Call EnumerateObject(GetObject(sOU3))
Call EnumerateObject(GetObject(sOU4))

oOutputFile.Close
Set oOutputFile = Nothing
Set oFS = Nothing
MsgBox "Complete."

Sub EnumerateObject(oContainer)
    On Error Resume Next
   
    For Each oObject In oContainer
        Select Case LCase(oObject.Class)
            Case "computer"
            sComputerName = Left(oObject.sAMAccountName, Len(oObject.sAMAccountName) - 1)
            sPCData = GetPCData(sComputerName)
            aFields = Split(sPCData, "|")
            oOutputFile.WriteLine Quote(oObject.Get("Name")) & "," & Quote(oObject.operatingSystem) & "," & Quote(oObject.operatingSystemServicePack) & "," & _
            Quote(aFields(0)) & "," & Quote(aFields(1)) & "," & Quote(aFields(2)) & "," & Quote(aFields(3)) & "," & Quote(aFields(4)& "," & Quote(aFields(5))& "," & Quote(aFields(6))& "," & Quote(aFields(7))
            sPCData = ""
            Case "organizationalunit", "container"
            Call EnumerateObject(oObject)
        End Select
    Next
End Sub

'************************************************************************************
' Put the passed text into quotes
'************************************************************************************
Function Quote(sText)
    Quote = Chr(34) & sText & Chr(34)
End Function

'************************************************************************************
' Get WMI data from the PC
'************************************************************************************
Function GetPCData(sTempName)
    On Error Resume Next
   
    If Ping(sTempName) = True Then
        Set oLocator = CreateObject("WbemScripting.SWbemLocator")
        oLocator.Security_.ImpersonationLevel = 3
        oLocator.Security_.AuthenticationLevel = 0
       
        Set oWMI = oLocator.ConnectServer(sTempName, "root\cimv2", sAdminUserName, sAdminPassword)
       
        'IPAddress
        Set cIPAddresses = oWMI.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
        For Each oIPAddress In cIPAddresses
            sIPAddress = oIPAddress.IPAddress(0)
        Next
        Set cIPAddresses = Nothing
       
        'Manufacturer and Model Number
        Set cComputerSystems = oWMI.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem")
        For Each oComputerSystem In cComputerSystems
            sManufacturer = oComputerSystem.Manufacturer
            sModelNumber = oComputerSystem.Model
        Next
        Set cComputerSystems = Nothing
       
        'Model and Serial Number
        Set cComputerSystemProducts = oWMI.ExecQuery("SELECT IdentifyingNumber,Version FROM Win32_ComputerSystemProduct")
        For Each oComputerSystemProduct In cComputerSystemProducts
            sSerialNumber = oComputerSystemProduct.IdentifyingNumber
            sModel = oComputerSystemProduct.Version
        Next
        Set cComputerSystemProducts = Nothing
       
        'Processor Type
        sProType = =""
       
        Set colProcessors = oWMI.ExecQuery("Select * From Win32_Processor")
       
        For Each objProcessor In colProcessors
            If objProcessor.Architecture = 0 Then
                sProType =  "x86"
            ElseIf objProcessor.Architecture = 1 Then
                sProType =  "MIPS"
            ElseIf objProcessor.Architecture = 2 Then
                sProType =  "Alpha"
            ElseIf objProcessor.Architecture = 3 Then
                sProType =  "PowerPC"
            ElseIf objProcessor.Architecture = 6 Then
                sProType =  "ia64"
            Else
                sProType =  "Unknown"
            End If
        Next
       
       'Physical Memory Properties
       
        sRAM=""
        Set colItems = oWMI.ExecQuery("Select * from Win32_PhysicalMemoryArray")
       
        For Each objItem In colItems
            sRAM = objItem.MaxCapacity
        Next
       
        'Disk Space Check
       
        strdisk=""
       
        Set colItems = oWMI.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
        strdisk=strdisk & vbCrLf & vbCrLf & "Drive" & vbTab & "DiskSize" & vbTab & "FreeSpace" & vbTab & "FreeSpace%" & vbCrLf
       
        For Each objItem In colItems
            pctFreeSpace = Round(((objItem.FreeSpace / objItem.Size) * 1000)/10,2)
            strDiskSize = Round((objItem.Size /1073741824),2) & "Gb"
            If Round((objItem.Size /1073741824),2) < 1 Then
                strFreeSpace=Round((objItem.FreeSpace /1048576),2) & "Mb"
            Else
                strFreeSpace = Round((objItem.FreeSpace /1073741824),2) & "Gb"
            End If                
            strUsedSpace = Round(((objItem.Size-objItem.FreeSpace)/1073741824),2) & "Gb"
            strdisk = strdisk & objItem.Name & vbTab & strDiskSize & Space(8-Len(strDiskSize)) & vbTab & strFreeSpace & Space(8-Len(strFreeSpace))& vbTab & pctFreeSpace & vbCrLf
        Next
       
        MsgBox strdisk
       
        Set oWMI = Nothing
        Set oLocator = Nothing
    End If
   
    GetPCData = sIPAddress & "|" & sManufacturer & "|" & sModel & "|" & sModelNumber & "|" & sSerialNumber & "|" & sProType & "|" & sRAM & "|" & strdisk
End Function

'************************************************************************************
' Ping the passed computer name or IP
'************************************************************************************
Function Ping(sHost)
    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")
   
    Set cPings = oWMILocal.ExecQuery("SELECT * FROM Win32_PingStatus " &_
    "WHERE Address = '" & sHost & "' AND timeout = 500")
    For Each oPing In cPings
        iPingResult = oPing.StatusCode
        If (iPingResult = 0) Then
            Ping = True
            Exit Function
        Else
            Ping = False
        End If
    Next
End Function
0
 

Author Comment

by:bob_kochanski
ID: 35010822
prashanthd,

          The script is throwing an error, I have attached the error code.

Thanks,
VBScriptError.jpg
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 250 total points
ID: 35014235
Hi,

Typo error... can you try the following

     sOU1 = "LDAP://OU=Domain Controllers,
sOU2 = "LDAP://OU=Servers,
sOU3 = "LDAP://OU=Terminal Servers,
sOU4 = "LDAP://OU=Domain Controllers,
sAdminUserName =
sAdminPassword =

On Error Resume Next

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oOutputFile = oFS.CreateTextFile("All Servers " & Replace(Replace(Now, "/", "_"), ":", ".") & ".csv", True)

oOutputFile.WriteLine "Server Name,Operating System,Service Pack,IP Address,Manufacturer,Model,ModelNumber,Serial Number,Processor Type,Physical RAM(MB),Disk Space"

Call EnumerateObject(GetObject(sOU1))
Call EnumerateObject(GetObject(sOU2))
Call EnumerateObject(GetObject(sOU3))
Call EnumerateObject(GetObject(sOU4))

oOutputFile.Close
Set oOutputFile = Nothing
Set oFS = Nothing
MsgBox "Complete."

Sub EnumerateObject(oContainer)
    On Error Resume Next
   
    For Each oObject In oContainer
        Select Case LCase(oObject.Class)
            Case "computer"
            sComputerName = Left(oObject.sAMAccountName, Len(oObject.sAMAccountName) - 1)
            sPCData = GetPCData(sComputerName)
            aFields = Split(sPCData, "|")
            oOutputFile.WriteLine Quote(oObject.Get("Name")) & "," & Quote(oObject.operatingSystem) & "," & Quote(oObject.operatingSystemServicePack) & "," & _
            Quote(aFields(0)) & "," & Quote(aFields(1)) & "," & Quote(aFields(2)) & "," & Quote(aFields(3)) & "," & Quote(aFields(4)& "," & Quote(aFields(5))& "," & Quote(aFields(6))& "," & Quote(aFields(7))
            sPCData = ""
            Case "organizationalunit", "container"
            Call EnumerateObject(oObject)
        End Select
    Next
End Sub

'************************************************************************************
' Put the passed text into quotes
'************************************************************************************
Function Quote(sText)
    Quote = Chr(34) & sText & Chr(34)
End Function

'************************************************************************************
' Get WMI data from the PC
'************************************************************************************
Function GetPCData(sTempName)
    On Error Resume Next
   
    If Ping(sTempName) = True Then
        Set oLocator = CreateObject("WbemScripting.SWbemLocator")
        oLocator.Security_.ImpersonationLevel = 3
        oLocator.Security_.AuthenticationLevel = 0
       
        Set oWMI = oLocator.ConnectServer(sTempName, "root\cimv2", sAdminUserName, sAdminPassword)
       
        'IPAddress
        Set cIPAddresses = oWMI.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
        For Each oIPAddress In cIPAddresses
            sIPAddress = oIPAddress.IPAddress(0)
        Next
        Set cIPAddresses = Nothing
       
        'Manufacturer and Model Number
        Set cComputerSystems = oWMI.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem")
        For Each oComputerSystem In cComputerSystems
            sManufacturer = oComputerSystem.Manufacturer
            sModelNumber = oComputerSystem.Model
        Next
        Set cComputerSystems = Nothing
       
        'Model and Serial Number
        Set cComputerSystemProducts = oWMI.ExecQuery("SELECT IdentifyingNumber,Version FROM Win32_ComputerSystemProduct")
        For Each oComputerSystemProduct In cComputerSystemProducts
            sSerialNumber = oComputerSystemProduct.IdentifyingNumber
            sModel = oComputerSystemProduct.Version
        Next
        Set cComputerSystemProducts = Nothing
       
        'Processor Type
        sProType = ""
       
        Set colProcessors = oWMI.ExecQuery("Select * From Win32_Processor")
       
        For Each objProcessor In colProcessors
            If objProcessor.Architecture = 0 Then
                sProType =  "x86"
            ElseIf objProcessor.Architecture = 1 Then
                sProType =  "MIPS"
            ElseIf objProcessor.Architecture = 2 Then
                sProType =  "Alpha"
            ElseIf objProcessor.Architecture = 3 Then
                sProType =  "PowerPC"
            ElseIf objProcessor.Architecture = 6 Then
                sProType =  "ia64"
            Else
                sProType =  "Unknown"
            End If
        Next
       
       'Physical Memory Properties
       
        sRAM=0
        Set colItems = oWMI.ExecQuery("Select * from Win32_PhysicalMemoryArray")
       
        For Each objItem In colItems
            sRAM = objItem.MaxCapacity/1024
        Next
       
        'Disk Space Check
       
        strdisk=""
       
        Set colItems = oWMI.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
        strdisk=strdisk & vbCrLf & vbCrLf & "Drive" & vbTab & "DiskSize" & vbTab & "FreeSpace" & vbTab & "FreeSpace%" & vbCrLf
       
        For Each objItem In colItems
            pctFreeSpace = Round(((objItem.FreeSpace / objItem.Size) * 1000)/10,2)
            strDiskSize = Round((objItem.Size /1073741824),2) & "Gb"
            If Round((objItem.Size /1073741824),2) < 1 Then
                strFreeSpace=Round((objItem.FreeSpace /1048576),2) & "Mb"
            Else
                strFreeSpace = Round((objItem.FreeSpace /1073741824),2) & "Gb"
            End If                
            strUsedSpace = Round(((objItem.Size-objItem.FreeSpace)/1073741824),2) & "Gb"
            strdisk = strdisk & objItem.Name & vbTab & strDiskSize & Space(8-Len(strDiskSize)) & vbTab & strFreeSpace & Space(8-Len(strFreeSpace))& vbTab & pctFreeSpace & vbCrLf
        Next
       
        'MsgBox strdisk
       
        Set oWMI = Nothing
        Set oLocator = Nothing
    End If
   
    GetPCData = sIPAddress & "|" & sManufacturer & "|" & sModel & "|" & sModelNumber & "|" & sSerialNumber & "|" & sProType & "|" & sRAM & "|" & strdisk
End Function

'************************************************************************************
' Ping the passed computer name or IP
'************************************************************************************
Function Ping(sHost)
    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")
   
    Set cPings = oWMILocal.ExecQuery("SELECT * FROM Win32_PingStatus " &_
    "WHERE Address = '" & sHost & "' AND timeout = 500")
    For Each oPing In cPings
        iPingResult = oPing.StatusCode
        If (iPingResult = 0) Then
            Ping = True
            Exit Function
        Else
            Ping = False
        End If
    Next
End Function
0
 

Author Closing Comment

by:bob_kochanski
ID: 35037336
prashanthd

Thanks for all your help, much appreciated.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now