Stephon98caffe
Programmer
This program reads a file with out of date workstations. The workstations are in different OU and I want to list the workstations by OU's in different Excel sheets. I can not seem to figure out how to create spread sheets for each OU's list of Workstations. I have to be able to create the list on the fly(dymanically). My logic is that I go through file first time to get an array of all OU's so that there are no dublicates. Next I read the file again and want to compare each file entry with list of OU's and put the computer name on the same excel sheet of the OU it is in.
Example Here is the way the data looks. I would like to have a 2 excel sheets. 1 named "NOC" with NOC107 on the list. 2 named "ORM" with AS108DZ8D2-L
"CN=NOC107,OU=Computers,OU=NOC,OU=VACO,DC=dva,DC=va,DC=gov"
"CN=AS108DZ8D21-L,OU=Computers,OU=ORM,DC=dva,DC=va,DC=gov"
Here is my code.
' VB Script Document
Dim objFile, strGuyFile, strFilePath
strFilePath = "c:\temp\outofdate.txt"
'opens file
set objFile = CreateObject("Scripting.FileSystemObject")
set strGuyFile = objFile.OpenTextFile(strFilePath, 1,false)
'***creates excel spreadsheet
Dim intMaxWork, objXL, strMBInfo, strProcInfo, objXLWork
Dim strOU
Dim strCN, i
Dim intRow, diffOU
Dim objCon,strQuery,objRecSet
dim worksheet(2000)
dim single1 (2000)
Const conImpersonate = 3
' create an Excel object and set up the spreadsheet
Set objXL = CreateObject("Excel.Application")
'set objXLWork = objXL.ActiveWorkbook.Worksheets(2)
'objXLWork.Cells(intRow, 1).Value = "job"
'StartExcel objXL
intRow = 1
i = 0
'***Loop parses canonical name of wkst
do while not strGuyFile.AtEndOfStream
line = strGuyFile.ReadLine
arTmp = Split(line, ",")
strCN = Mid(arTmp(0), 5)
strOU = Mid(arTmp(2), 4)
worksheet(i) = strOU
if not strGuyFile.AtEndofStream then
i = i + 1
else
i = i
end if
'UpdateExcel objXL,intRow,strCN, strOU
'intRow = intRow + 1
loop
strGuyFile.Close
strGuyFile.open
'open list again
intRow =1
***Removes dublicates
Dim d, item, thekeys
Set d = CreateObject("Scripting.Dictionary")
d.removeall
d.CompareMode = 0
For Each item In worksheet
If Not d.Exists(item) Then d.Add item, item
Next
thekeys = d.keys
do while not strGuyFile.AtEndOfStream
line = strGuyFile.ReadLine
arTmp = Split(line, ",")
strCN = Mid(arTmp(0), 5)
strOU = Mid(arTmp(2), 4)
'*****LOST****
for each item in thekeys
if not Strcomp(strOU, item) then
Set item = CreateObject("Excel.Application")
StartExcel item
UpdateExcel item,intRow,strCN, strOU
else
UpdateExcel item ,intRow,strCN, strOU
end if
next
'Set d = Nothing
strGuyFile.Close
Sub StartExcel (objXL)
Dim intRow
intRow = 1
objXL.Visible = True
objXL.Workbooks.Add
objXL.Cells(intRow,1).Value = "Computer"
objXL.Cells(intRow,2).Value = "Organization"
objxl.Rows(1).font.Bold = True
End Sub
Sub UpdateExcel(objXL,intRow,strCompName, strOU)
Dim intCounter
WScript.Sleep (1000)
objXL.Cells(intRow,1).Value = strip (strCompName)
objXL.Cells(intRow,2).Value = strOU
End Sub
Function strip (strString)
Dim strOld
strOld = strString
strString = Replace(strString," "," ")
if strOld <> strString then
strString = strip(strString)
End If
strip=strString
End Function
Function StripLineFeed (strString)
If strSTring <> "" then
StripLineFeed = Left(strString,Len(strString)-1)
end if
End Function
Example Here is the way the data looks. I would like to have a 2 excel sheets. 1 named "NOC" with NOC107 on the list. 2 named "ORM" with AS108DZ8D2-L
"CN=NOC107,OU=Computers,OU=NOC,OU=VACO,DC=dva,DC=va,DC=gov"
"CN=AS108DZ8D21-L,OU=Computers,OU=ORM,DC=dva,DC=va,DC=gov"
Here is my code.
' VB Script Document
Dim objFile, strGuyFile, strFilePath
strFilePath = "c:\temp\outofdate.txt"
'opens file
set objFile = CreateObject("Scripting.FileSystemObject")
set strGuyFile = objFile.OpenTextFile(strFilePath, 1,false)
'***creates excel spreadsheet
Dim intMaxWork, objXL, strMBInfo, strProcInfo, objXLWork
Dim strOU
Dim strCN, i
Dim intRow, diffOU
Dim objCon,strQuery,objRecSet
dim worksheet(2000)
dim single1 (2000)
Const conImpersonate = 3
' create an Excel object and set up the spreadsheet
Set objXL = CreateObject("Excel.Application")
'set objXLWork = objXL.ActiveWorkbook.Worksheets(2)
'objXLWork.Cells(intRow, 1).Value = "job"
'StartExcel objXL
intRow = 1
i = 0
'***Loop parses canonical name of wkst
do while not strGuyFile.AtEndOfStream
line = strGuyFile.ReadLine
arTmp = Split(line, ",")
strCN = Mid(arTmp(0), 5)
strOU = Mid(arTmp(2), 4)
worksheet(i) = strOU
if not strGuyFile.AtEndofStream then
i = i + 1
else
i = i
end if
'UpdateExcel objXL,intRow,strCN, strOU
'intRow = intRow + 1
loop
strGuyFile.Close
strGuyFile.open
'open list again
intRow =1
***Removes dublicates
Dim d, item, thekeys
Set d = CreateObject("Scripting.Dictionary")
d.removeall
d.CompareMode = 0
For Each item In worksheet
If Not d.Exists(item) Then d.Add item, item
Next
thekeys = d.keys
do while not strGuyFile.AtEndOfStream
line = strGuyFile.ReadLine
arTmp = Split(line, ",")
strCN = Mid(arTmp(0), 5)
strOU = Mid(arTmp(2), 4)
'*****LOST****
for each item in thekeys
if not Strcomp(strOU, item) then
Set item = CreateObject("Excel.Application")
StartExcel item
UpdateExcel item,intRow,strCN, strOU
else
UpdateExcel item ,intRow,strCN, strOU
end if
next
'Set d = Nothing
strGuyFile.Close
Sub StartExcel (objXL)
Dim intRow
intRow = 1
objXL.Visible = True
objXL.Workbooks.Add
objXL.Cells(intRow,1).Value = "Computer"
objXL.Cells(intRow,2).Value = "Organization"
objxl.Rows(1).font.Bold = True
End Sub
Sub UpdateExcel(objXL,intRow,strCompName, strOU)
Dim intCounter
WScript.Sleep (1000)
objXL.Cells(intRow,1).Value = strip (strCompName)
objXL.Cells(intRow,2).Value = strOU
End Sub
Function strip (strString)
Dim strOld
strOld = strString
strString = Replace(strString," "," ")
if strOld <> strString then
strString = strip(strString)
End If
strip=strString
End Function
Function StripLineFeed (strString)
If strSTring <> "" then
StripLineFeed = Left(strString,Len(strString)-1)
end if
End Function