I am new to vbscripting and am trying to come up with a script that uses the
net view command to determine PC names on the network and then take those
names and populate a spreadsheet. I've taken almost all of the following
code from another script i've found on the net but am unable to make it work
completely. The Net View part works but i'm unable to parse the info and
then popluate a spreadsheet. Would someone be able to help with this. My
ultimate goal is to then use those PC names and to detemine what groups\users
are part of the local admin group and write that info to the same
spreadsheet. As of right now the script errors at the Sub
AddToSpreadSheet(arrNetwork) which located toward the botom.
Const FOR_READING = 1 'Used to open and read a file
Set oShell = CreateObject("wscript.shell")
Set oFso = CreateObject("scripting.filesystemobject")
Set oXL = Wscript.CreateObject("Excel.Application")
Set oRegularExpression = New RegExp
'Start to write at row 2 of excel file
introw = 2
'Runs command prompt Net View and writes output to NetViewList.txt file
oShell.Run "cmd.exe /c net view > c:\NetViewList.txt" , 2, True
'Open NetViewList.txt file
Set oNetViewList = oFSO.OpenTextFile("C:\NetViewList.txt", FOR_READING, True)
'Loop through NetViewList.txt file
Do While oNetViewList.AtEndOfStream True
Redim arrNetwork(0)
strCurrentLine = oNetViewList.Readline
strPattern = "\\"
'Test to find strPattern in strCurrentline
bInPatternFound = GetPattern(strCurrentLine, strPattern)
'If True Proceed...
If bInPatternFound Then
strPattern = "\\\\\S*"
intFlag = 1
' find hostname in file
arrNetwork(0) = GetMatch(strCurrentLine, strPattern, intFlag)
strHostName = arrNetwork(0)
End If
Loop
Call AddToSpreadSheet(arrNetwork)
'----------------------------------------------------------------------
'----------------------------------------------------------------------
Function GetMatch(strSource, strSourcePattern, intflag)
Dim strMatch
Dim strIsMatch
'affect pattern to RegExp.Pattern
oRegularExpression.Pattern = strSourcePattern
'executes a search for a match (pattern) in the specified string
'returng a result array
Set oMatches = oRegularExpression.Execute(strSource)
For Each strMatch In oMatches
strIsMatch = strMatch.Value
'1 is matching hostname
If intFlag = 1 Then strIsMatch = Mid(strIsMatch,3)
'End If
Next
GetMatch = strIsMatch
Set oMatches = Nothing
End Function
'----------------------------------------------------------------------
'----------------------------------------------------------------------
Function GetPattern(strSource, strSourcePattern)
'Give the pattern to RegExp to test source which is strCurrentLine
oRegularExpression.Pattern = strSourcePattern
'Test for a match of a regualr expression (Pattern) in the string
'returning true is successful and false if not
If oRegularExpression.Test(strSource) Then
GetPattern = "True"
Else
GetPattern = "False"
End If
End Function
'------------------------------------------------------------------------
'------------------------------------------------------------------------
Sub BuildSpreadsheet()
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
'Create a new workbook
oExcel.Workbooks.Add
'Bind to worksheet
Set oSheet = oExcel.ActiveWorkbook.Worksheets(1)
'Populate spreadsheet with info
oSheet.Cells(1, 1).Value = "Computer Name"
oSheet.Cells(1, 2).Value = "Groups"
'Format the spreadsheet
oSheet.Range("A1:A2").Font.Bold = True
oSheet.Select
oSheet.Range("B5").Select
oExcel.Columns(1).ColumnWidth = 20
oExcel.Columns(2).ColumnWidth = 20
'-------------------------------------------------------------------------
'------------------------------------------------------------------------
Sub AddToSpreadSheet(arrNetwork)
oExcel.Cells(intRow, 1).Value = arrNetwork(0)
intRow = intRow +1
oExcel.Cells(intRow, 1).Select
End sub
Archived from group: microsoft>public>vb>syntax