Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a subroutine, script stops working

Status
Not open for further replies.

9991

IS-IT--Management
Aug 7, 2006
2
US
I think I got this from markdmac. Script works great, until I change it. Trying to call this script as a sub. At the top I added Sub PCLoginStats and just before the function at the bottom I added End Sub. It no longer generates the information from the sub, but it does create an empty file. Seems it skips the subroutine and runs the function only. Any sugestions?



' change this To your directory where you wants to get the files:
' \\servername\share\dir\
'
' ----------------------------------------
Sub PCLoginStats
'MsgBox ("Inside PCLoginStats.vbs")
On error resume Next
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f

set adsys = CreateObject("ADSystemInfo")
set wscr = CreateObject("WScript.Network")

Set oDrives = wscr.EnumNetworkDrives
Set oPrinters = wscr.EnumPrinterConnections

sUserDL = wscr.username
fso.DeleteFile "\\server1\reports\" & sUserDL & ".txt", TRUE

outputfile = "\\server1\reports\" & sUserDL & ".txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso_OpenTextFile(outputfile, ForWriting, True)

LogonTime = Time
line = "LogonTime: " & LogonTime
appendit(line)

Logondate = Date
line = "LogonDate: " & LogonDate & chr(13) & chr(10)
appendit(line)

'Get the downlevel logon name of the user

line = "USER DETAILS:" & chr(13) & chr(10) & "sAMAccountname: " & sUserDL
appendit(line)

'Get the Distinguished name of the user
sUserDN = adsys.username
line = "UserDN: " & sUserDN
appendit(line)

' 'Get the User's telephone extension
' Set oUser = Getobject(sPrefix & sUserDN)
' sUserExt = oUser.homePhone
' line = "Ext: " & sUserExt
' appendit(line)

' 'Get the user's group membership
' count = 1
' line = "Group Membership:"
' appendit(line)
' aMems = oUser.GetEx("memberOf")
' For each mem in aMems
' line = count & ") " & mem
' appendit(line)
' count = count + 1
' Next
'
' line = ""
' appendit (line)

'Get the short name of the machine
sMach = wscr.computername
line = "PC DETAILS:" & chr(13) & chr(10) & "Computer: " & sMach
appendit(line)

'Get the DN of the machine
sMachDN = adsys.computername
line = "PC_DN: " & sMachDN
appendit(line)

'Get the sitename
sSite = adsys.sitename
line = "Site: " & sSite
appendit(line)

'Get the IP details
set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//localhost").ExecQuery("select ServiceName, IPAddress, IPSubnet,DefaultIPGateway, DHCPServer,DHCPLeaseObtained, DNSServerSearchOrder, MACAddress, WINSPrimaryServer,WINSSecondaryServer from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

Count = 0
for each IPConfig in IPConfigSet
Count = Count + 1
next

ReDim sName(Count - 1)
ReDim sIP(Count - 1)
ReDim sDHCP(Count - 1)
ReDim sDNSSO(Count - 1)
ReDim sMAC(Count - 1)
ReDim sWINSP(Count - 1)
ReDim sWINSS(Count - 1)
ReDim sNW(Count - 1)
ReDim sSN(Count - 1)

Count = 0
j = 0

for each IPConfig in IPConfigSet
sName(Count) = IPConfig.ServiceName(0)
line = "Card Name: " & sName(Count)
appendit(line)

sIP(Count) = IPConfig.IPAddress(0)
line = "IP Address: " & sIP(Count)
appendit(line)

sSN(Count) = IPConfig.IPSubnet(0)
line = "Subnet Mask: " & sSN(Count)
appendit(line)

sNW(Count) = IPConfig.DefaultIPGateway(0)
line = "Default Gateway: " & sNW(Count)
appendit(line)

sMAC(Count) = IPConfig.MACAddress(0)
line = "MAC Address: " & sMAC(Count)
appendit(line)

sDHCP(Count) = IPConfig.DHCPServer
line = "DHCP Server: " & sDHCP(Count)
appendit(line)

sWINSP(Count) = IPConfig.WINSPrimaryServer(0)
line = "WINS 1: " & sWINSP(Count)
appendit(line)

sWINSS(Count) = IPConfig.WINSSecondaryServer(0)
line = "WINS 2: " & sWINSS(Count)
appendit(line)

x = 1
if Not IsNull(IPConfig.DNSServerSearchOrder) Then
for j=LBound(IPConfig.DNSServerSearchOrder) To UBound(IPConfig.DNSServerSearchOrder)
line = "DNS " & x & ": " & IPConfig.DNSServerSearchOrder(j)
appendit(line)
next
end if

Count = Count + 1
next

line = chr(13) & CHR(10)
appendit(line)

line = "DRIVE MAPPINGS and PRINTERS:"
appendit(line)

'Get the Network Drives mapped
For i = 0 to oDrives.Count - 1 Step 2
line = "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
appendit(line)
Next

line = chr(13) & CHR(10)
appendit(line)

'Get the Printers connected
For i = 0 to oPrinters.Count - 1 Step 2
line = "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
appendit(line)
Next

f.close
End Sub

'//////////////
Function AppendIt(input)
f.Writeline input
end Function

Call PCLoginStats
 
That is not one of mine, though I can offer you some suggested changes.

Build a report from within your sub and then write the full report all at once.

Replace everywhere you have line= with

Report = Report & vbCrLf &

Then before you close out your sub:

Code:
f.write Report

No need to call your function at all.


I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
[!]Dim fso, f[/!]
Sub PCLoginStats
...
[!]Dim fso, f[/!]
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top