Solved

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

Posted on 2011-02-28
14
230 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

14 Experts available now in Live!

Get 1:1 Help Now