Solved

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

Posted on 2011-02-28
14
231 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i reset all images size to default in a word file 6 58
Vb script to unzip a files and rename the files 12 93
Script is being strange 8 62
Problem to With line 4 36
Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

943 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

9 Experts available now in Live!

Get 1:1 Help Now