Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

VB Scripting to excel multiple queries to the same cell

$
0
0
Hello, I am a help desk technical at my company and I am trying to create a vb login script that would gather various things about the user and computer and create or amend a excel spread sheet on a network drive. A little about my background with programming, I am not a programmer.  My knowledge is limited but I do understand some programming logic. So far I have been able to Frankenstein together this vbs script from various sources online with even some of my very own programming.  Here is my snag. I want to list all of the network drives of an user to a single cell within excel.  I've tried everything with no success.  
The closest I can get is it listing just one (the last) network drive.  The other issue I've been having is that it will not list any of my member of groups from AD.  I know I am a super novice but I am willing to learn and understand.  Any help would be great!
Here is my code:

   Set WshShell = WScript.CreateObject("wscript.shell")
Set objArgs = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("wscript.Shell")
Set env = oShell.environment("Process")
strComputer = env.Item("Computername")

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)
objExcel.worksheets(2).delete
objExcel.worksheets(2).delete
objExcel.ActiveWorkbook.Windows(1).Caption = OutputFile
strOut = ""
getOSInfo

Sub getOSInfo()
On Error Resume Next

objExcel.worksheets(1).Activate
objExcel.worksheets(1).Name = "Computer Info"
objExcel.Cells(1, 1).Value = "Computer Name"
objExcel.Cells(2, 1).Value = "Computer Name from system"
objExcel.Cells(3, 1).Value = "IP(s) from system"
objExcel.Cells(4, 1).Value = "Logon Name"
objExcel.Cells(5, 1).Value = "Operating System"
objExcel.Cells(6, 1).Value = "Last Bootup Time"
objExcel.Cells(7, 1).Value = "Install Date"
objExcel.Cells(8, 1).Value = "Manufacturer"
objExcel.Cells(9, 1).Value = "Serial Number"
objExcel.Cells(10, 1).Value = "Model"
objExcel.Cells(11, 1).Value = "Mapped Drives"
objExcel.Cells(12, 1).Value = "Member of Group(s)"
objExcel.Cells(13, 1).Value = "Amt. of Storage Allocated"
objExcel.Cells(14, 1).Value = "# of Processors"
objExcel.Cells(15, 1).Value = "Processor Type"
objExcel.Cells(16, 1).Value = "Memory (GB)"
colVar=2

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Err.Number <> 0 Then
objExcel.Cells(1, colVar).Value = strComputer
objExcel.Cells(2, colVar).Value = "Error # " & CStr(Err.Number) & " " & Err.Description
printout "Error # " & CStr(Err.Number) & " " & Err.Description
colVar = colVar+1
Err.Clear
Else

objExcel.ActiveWorkbook.Windows(1).Caption = "Getting Win32_OperatingSystem for " & strComputer
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

objExcel.ActiveWorkbook.Windows(1).Caption = "Getting Win32_BIOS for " & strComputer
Set colBIOS = objWMIService.ExecQuery ("Select * from Win32_BIOS")

objExcel.ActiveWorkbook.Windows(1).Caption = "Getting Win32_ComputerSystem for " & strComputer
Set colComputerSystem = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

objExcel.ActiveWorkbook.Windows(1).Caption = "Getting Win32_NetworkAdapterConfiguration for " & strComputer
Set colNetworkAdapterConfiguration = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration")

objExcel.ActiveWorkbook.Windows(1).Caption = "Getting Win32_MappedLogicalDisk for " & strComputer
Set objNetwork = WScript.CreateObject("WScript.Network")

objExcel.ActiveWorkbook.Windows(1).Caption = "Getting Win32_Processor info for " & strComputer
Set colProc = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objOS In colOperatingSystems

objExcel.ActiveWorkbook.Windows(1).Caption = "Setting Computer Name for " & strComputer
objExcel.Cells(1, colVar).Value = strComputer

objExcel.ActiveWorkbook.Windows(1).Caption = "Setting Last Boot Time for " & strComputer
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
dtmConvertedDate.Value = objOS.InstallDate
dtmInstallDate = dtmConvertedDate.GetVarDate
objExcel.Cells(7, colVar).Value = dtmInstallDate
tempArray = Split(objOS.name, "|")
objExcel.Cells(6, colVar).Value = tempArray(0)
dtmConvertedDate.Value = objOS.LastBootUpTime
dtmBootTime = dtmConvertedDate.GetVarDate
objExcel.Cells(5, colVar).Value = dtmBootTime
Next

For Each objBIOS In colBIOS
objExcel.ActiveWorkbook.Windows(1).Caption = "Setting BIOS info for " & strComputer
objExcel.Cells(9, colVar).Value = objBIOS.SerialNumber
Next

For Each objCS In colComputerSystem

objExcel.ActiveWorkbook.Windows(1).Caption = "Setting Manufacturer info for " & strComputer
objExcel.Cells(8, colVar).Value = objCS.Manufacturer

objExcel.ActiveWorkbook.Windows(1).Caption = "Setting Model info for " & strComputer
objExcel.Cells(10, colVar).Value = objCS.Model

objExcel.ActiveWorkbook.Windows(1).Caption = "Setting name from WMI for " & strComputer
objExcel.Cells(2, colVar).Value = objCS.name

objExcel.ActiveWorkbook.Windows(1).Caption = "Setting Total Physical Memory for " & strComputer
objExcel.Cells(16, colVar).Value = Round(objCS.TotalPhysicalMemory/1024/1024/1024,2)
Next

For Each objNetAdapter In colNetworkAdapterConfiguration
objExcel.ActiveWorkbook.Windows(1).Caption = "Getting IP Addresses for " & strComputer
ipAddress = objNetAdapter.ipaddress
For i = 0 To UBound(ipaddress)
If iplist = "" Then
iplist = ipaddress(i)
Else
iplist = iplist & ", " & ipaddress(i)
End If
Next
objExcel.Cells(3, colVar).Value = iplist

Next

Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
objExcel.Cells(11, colVar).Value = colDrives.Item(i) & vbTab & colDrives.Item (i + 1)
Next

Err.Clear
strUser = strComputer & "$"
objExcel.ActiveWorkbook.Windows(1).Caption = "Getting AD Group info for " & strComputer
Set objRoot = GetObject("LDAP://RootDSE")
defaultNC = objRoot.Get("defaultnamingcontext")
computerDN = FindUser(strUser, defaultNC)
ouarray = Split(computerDN,",")
For i = 1 To UBound(ouarray)
If ou = "" Then
ou = ouarray(i)
Else
ou = ou & "," & ouarray(i)
End If
Next

Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
For Each objItem In colItems
strUsers = objItem.UserName
Next 

objExcel.Cells(4, colVar).Value = strUsers 'ou

Set dicSeenGroup = CreateObject("Scripting.Dictionary")
strGroups = DisplayGroups(computerDN,"",dicSeenGroup)
aryGroups = Split(strGroups,"CN=")
strGroups = ""
For i = 2 To UBound(aryGroups)
strGroups = strGroups & ", " & aryGroups(i)
Next
objExcel.Cells(12, colVar).Value = Right(strGroups,Len(strGroups) -2)
Err.Clear

stroutput = getDriveLettersAndSize(strComputer)
objExcel.Cells(13, colVar).Value = Left(stroutput,Len(stroutput)-2)

ProcCount = 0
objExcel.ActiveWorkbook.Windows(1).Caption = "Setting number of processors for " & strComputer
For Each processor In colProc
ProcCount = ProcCount + 1
ProcName = processor.name
Next
objExcel.Cells(14, colVar).Value = ProcCount
objExcel.Cells(15, colVar).Value = Trim(ProcName)

strOut = ""
iplist = ""
ou = ""
colVar = colVar+1
End If

objExcel.Cells.Select
objExcel.Cells.EntireColumn.AutoFit
objExcel.Range("B2").Select
objExcel.ActiveWindow.FreezePanes = True
objWorksheet.Columns("B:B").HorizontalAlignment = -4131
objExcel.ActiveWorkbook.Windows(1).Caption = "Finished gathering computer info"

End Sub

Function FindUser(Byval UserName, Byval Domain)
On Error Resume Next

Set cn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")

cn.open "Provider=ADsDSOObject;"

cmd.activeconnection=cn
cmd.commandtext="SELECT ADsPath FROM 'LDAP://" & Domain & "' WHERE sAMAccountName = '" & UserName & "'"

Set rs = cmd.Execute

If Err<>0 Then
FindUser="Error connecting to Active Directory Database:" & Err.description
'wscript.quit
Else
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
FindUser = rs(0)
Else
FindUser = "Not Found"
End If
End If
cn.close
End Function

Function DisplayGroups ( strObjectADsPath, strSpaces, dicSeenGroup)
Set objObject = GetObject(strObjectADsPath)
'strOut must be global variable
strOut = strOut & strSpaces & objObject.Name
On Error Resume Next ' Doing this to avoid an error when memberOf is empty
If IsArray( objObject.Get("memberOf") ) Then
colGroups = objObject.Get("memberOf")
Else
colGroups = Array( objObject.Get("memberOf") )
End If

For Each strGroupDN In colGroups
If Not dicSeenGroup.Exists(strGroupDN) Then
dicSeenGroup.Add strGroupDN, 1
DisplayGroups "LDAP://" & strGroupDN, strSpaces & " ", dicSeenGroup
End If
Next
Err.Clear
DisplayGroups = strOut
End Function

Function getDriveLettersAndSize(strComputer)
On Error Resume Next
Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")
If Err.Number Then
getDriveLettersAndSize = "Error # " & CStr(Err.Number) & " " & Err.Description & "  "
Err.Clear
Else
On Error Goto 0
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DriveType=3", , 48)
For Each objItem In colItems
getDriveLettersAndSize = getDriveLettersAndSize & objItem.Name & " " & Round(getDriveSizeTotal(strComputer,objItem.Name)/1024/1024/1024,2) & "GB, "
Next
End If
End Function

Function getDriveSizeTotal(strComputer, drvLetter)
On Error Resume Next
Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")
strTemp = strComputer
If Err.Number Then
getDriveSizeTotal = "0"
Err.Clear
Else
On Error Goto 0
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DriveType=3", , 48)
For Each objItem In colItems
If UCase(objItem.Name) = UCase(drvLetter) Then
getDriveSizeTotal = objItem.Size
End If
Next
End If
End Function

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>