VB Script Help Please!, Win32_Share, CIM_DataFile, CIM_LogicalFile

G

Guest

First off, I am not sure if this is the correct place to post this question.
I have not used the newsgroups since they went to this new format. If there
is a more appropriate newsgroup please let me know.

I have a VB script which is not accomplishing what I had hoped. We have
several hundered severs with 1000+ shares per server in multiple
domains. We are trying to determine which shares have been accessed in
the last 180 days to determine "Active Shares"

Goals:
1: Loop through a text file (servers.txt) with FQDN server names and
enumerate the server name, share name, share description, Local path to
share on the server, if last access date to share is within last 180
days, share size and populate into an excel spreadsheet. The script
should not update the last access date for the share. The script only
goes 1 level into the share. The theory behind only drilling down 1
level is if someone connects to the share and then accesses a folder or
file they will have to click on that folder or file, which will update
it's last access date. If there is a share within a share it will
enumerate that share seperately. If any file or folder has been
accessed at the first level of the share in the last 180 days it will
return a Yes/No statement including last accessed date and last
modified date.

Excel Spreadsheet columns:
Count
Server Name (from servers.txt)
Share Name (from Win32_Share Name)
Share Description (from Win32_Share Desription)
Local Path of share on server (from Win32_Share Path)
Access in last 180 days (Yes/No) (from CIM_LogicalFile LastAccessed)
Last Accessed (from CIM_LogicalFile LastAccessed)
Last Modified (from CIM_LogicalFile LastModified)
Share Size (from CIM_Datafile FileSize)?????

2: Drop default shares IPC$, C$, D$, ADMIN$, WWRoot$ out of report

3: Drop servers that are not online into a text file via a ping check
to "C:\scripts\FailedToConnect.txt"

4: Be able to pass network credentials for access.

Currently the script does almost everything we have targeted. These are
the issues.

1. Cannot get the return share size info to return correcly, removed
from script. Anyone have any ideas?
2. The script updates the last access time to when the script touched
it, it can't.
3. Script is too slow.

Does anyone out there know a better way to do this, or a program/tool
that might accomplish all of my goals? I have tried treesize pro via
it's command line interface, and robocopy (it actually works to a
degree - "for /F "tokens=1" %%i in (shares.txt) do Robocopy \\%%i c:\
/s /maxage:180 /L /LOG:c:\info.txt") for formatting the last accessed
date info the way I need it, but to no avail. Any help is greatly
appreciated. Script included below.

'##########################################################################
'##########################################################################
' Begin Script

ON ERROR RESUME NEXT

Set Shell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
const reading = 1
const append = 8
const writing = 2

intCount = 0

'############################################################################
'############################################################################
' Domain Admin Settings. For Verifying Access to remote machines when using
' WMI. Set to true and type the info below to use. Set false if you dont want
' to push the UserID and password using WMI.
DomAdminUse = True
' strAdminName should be DomainUserID.
' The script will fail without the domain.
StrAdminName = "Domain\Userid"
StrAdminPassword = "Password"
'############################################################################
'############################################################################

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "ID"
objExcel.Cells(1, 2).Value = "System"
objExcel.Cells(1, 3).Value = "Share Name"
objExcel.Cells(1, 4).Value = "Share Desc"
objExcel.Cells(1, 5).Value = "Local Path"
objExcel.Cells(1, 6).Value = "Access in last 180 days"
objExcel.Cells(1, 7).Value = "Last Accessed"
objExcel.Cells(1, 8).Value = "Last Modified"

'Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile("C:\scripts\servers.txt", reading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strText, vbCrLf)
x = 2

txtFailure = "C:\scripts\FailedToConnect.txt"
SET OutputFail = fso.opentextfile(txtFailure, Append, true)
OutputFail.Write "************************************************" & VBCRLF
OutputFail.Write "Enumerate Shares Script started at: " & Date() & " " &
time() & VBCRLF
IF DebugMode = "on" THEN
OutputFail.Write "Systems that failed are listed below:" & VBCRLF
END IF
OutputFail.Write VBCRLF

For Each strComputer in arrComputers
IF PingVerify(strComputer) = False THEN
OutputFail.Write strComputer & " - [Not Online]" & vbcrlf
ELSE
SET objwbemLocator = CreateObject("WbemScripting.SWbemLocator")
IF DomAdminUse = True THEN
IF lcase(strComputer) = "localhost" OR lcase(strComputer) = "." OR
lcase(strComputer) = "local" OR

lcase(strComputer) = "127.0.0.1" THEN
Set objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &

"\root\cimv2")
ELSE
SET objWMIService =
objwbemLocator.ConnectServer(strComputer,"root\cimv2", StrAdminName,

StrAdminPassword)
END IF
ELSE
'SET objWMIService = objwbemLocator.ConnectServer(strComputer,"root\cimv2")
Set objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &

"\root\cimv2")
END IF
'SET objWMIService =
objwbemLocator.ConnectServer(strComputer,"root\cimv2", StrAdminName,
StrAdminPassword)
'Set objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")

If Err.Number Then
OutputFail.Write strComputer & " - [WMI connection failed] - Error #: "
& Err.Number & vbcrlf
Err.Clear
ELSE
Set colShares = objWMIService.ExecQuery("Select * from Win32_Share")
For each objShare in colShares

strNewPath = REPLACE(objShare.Path, LEFT(objShare.Path, 2), "") & "\"
strNewPath = REPLACE(strNewPath, "\", "\\")

Set colFiles = objWMIService.ExecQuery("Select * from CIM_LogicalFile
where drive='" & LEFT

(objShare.Path, 2) & _
"' AND path = '" & strNewPath & "'")

IF objShare.Name = "IPC$" OR objShare.Name = "ADMIN$" OR objShare.Name =
"wwwroot$" OR

objShare.Name = "print$" OR objShare.Caption = "Default share" OR
objShare.Caption = "SharedDocs" THEN
'Wscript.echo "Found Invalid Share"
ELSE
boolAccessed = False
strLastAccessDate = ""
strLastModifiedGetDate = ""
strLATempYear = ""
strLATempMonth = ""
strLATempDay = ""
strLMTempYear = ""
strLMTempMonth = ""
strLMTempDay = ""
strShareName = ""
strShareCaption = ""
strSharePath = ""
strLADate = ""
strLMDate = ""

For Each objFile in colFiles
strLastAccessDate = ""
strLastModifiedGetDate = ""
strLATempYear = ""
strLATempMonth = ""
strLATempDay = ""
strLMTempYear = ""
strLMTempMonth = ""
strLMTempDay = ""
strShareName = ""
strShareCaption = ""
strSharePath = ""
strLADate = ""
strLMDate = ""

strLastAccessDate = objFile.LastAccessed
strLastModifiedGetDate = objFile.LastModified
strLATempYear = LEFT(strLastAccessDate, 4)
strLATempMonth = RIGHT(LEFT(strLastAccessDate, 6), 2)
strLATempDay = RIGHT(LEFT(strLastAccessDate, 8), 2)
strLMTempYear = LEFT(strLastModifiedGetDate, 4)
strLMTempMonth = RIGHT(LEFT(strLastModifiedGetDate, 6), 2)
strLMTempDay = RIGHT(LEFT(strLastModifiedGetDate, 8), 2)
strShareName = objShare.Name
strShareCaption = objShare.Caption
strSharePath = objShare.Path
'IF strLATempMonth = "" THEN
'ELSE
strLADate = strLATempMonth & "/" & strLATempDay & "/" & strLATempYear
strLMDate = strLMTempMonth & "/" & strLMTempDay & "/" & strLMTempYear
'Wscript.echo strLastAccessDate & VBCRLF & now() - 180
'wscript.echo DateDiff("d", now() - 180, strLATempMonth & "/" &

strLATempDay & "/" & strLATempYear & " 00:00:00 AM")
'wscript.echo now() - 180 & VBCRLF & strLATempMonth & "/" &

strLATempDay & "/" & strLATempYear & " 00:00:00 AM"

IF DateDiff("d", now() - 180, strLATempMonth & "/" & strLATempDay &

"/" & strLATempYear & " 00:00:00 AM") =< 180 THEN
IF strLATempMonth & "/" & strLATempDay & "/" & strLATempYear

= "//" THEN
ELSE
boolAccessed = True
END IF
END IF
'END IF
Next
IF boolAccessed = true Then
strAccessed = "Yes"
writeToExcel()
ELSE
IF strLATempMonth & "/" & strLATempDay & "/" & strLATempYear = "//" THEN
strAccessed = "N/A"
ELSE
strAccessed = "No"
END IF
writeToExcel()
END IF
END IF
Next
END IF
END IF
Next

SUB writeToExcel()
IF strLADate = "//" OR strLMDate = "//" THEN
strLADate = "N/A"
strLMDate = "N/A"
END IF

intCount = intCount + 1
objExcel.Cells(x, 1).Value = intCount
objExcel.Cells(x, 2).Value = strComputer
objExcel.Cells(x, 3).Value = strShareName
objExcel.Cells(x, 4).Value = strShareCaption
objExcel.Cells(x, 5).Value = strSharePath
objExcel.Cells(x, 6).Value = strAccessed
objExcel.Cells(x, 7).Value = strLADate
objExcel.Cells(x, 8).Value = strLMDate
x = x + 1
END SUB

FUNCTION PingVerify(strComputer)
' Verify Machine is Running. if not Send Name to Seperate file to run on
Later.
pingtemp = "c:\scripts\pingtemp.txt"
pinghost = "ping " & strComputer & " -n 1 > " & pingtemp
SHELL.RUN "COMMAND /C " & pinghost, 7, true
set pingfile = fso.opentextfile(pingtemp, reading, true)
do while pingfile.atendofstream <> true
line = pingfile.readline
CheckActive = Instr(line, "(0% loss)")
if CheckActive > 0 then
PingVerify = True
pingfile.close
'set pingtemperase = fso.opentextfile(pingtemp, writing, true)
'pingtemperase.write ""
'pingtemperase.close
Exit Function
END IF
LOOP
PingVerify = False
'set pingtemperase = fso.opentextfile(pingtemp, writing, true)
'pingtemperase.write ""
'pingtemperase.close
END FUNCTION

Set objRange = objExcel.Range("A1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()
Set objRange = objExcel.Range("B1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()
Set objRange = objExcel.Range("C1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()
Set objRange = objExcel.Range("D1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()
Set objRange = objExcel.Range("E1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()
Set objRange = objExcel.Range("F1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()
Set objRange = objExcel.Range("G1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()
Set objRange = objExcel.Range("H1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Wscript.echo "Process Complete. Found " & intCount & " shares online!!!"

'##########################################################################
'##########################################################################
' End Script
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top