Solved

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

Posted on 2011-02-28
14
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 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