?
Solved

Get Ipconfig, NetworkDrives and Printers in a text file from a single .vbs ?

Posted on 2004-11-23
8
Medium Priority
?
320 Views
Last Modified: 2010-05-02
Hello Experts,

What i would like to do, is a vbs (WinNT compatible) runned from a floppy, that create a text file and retrieve "network" infos like this (or so ;) ) :

########################################
ComputerName : <computer name>
IP : <ip adress>
Gateway : <gateway adress>
Network Drives (in alphabetical order) :
     <drive letter>: <path>
Printers :
     <printer name>: <path (if network printer)> <display "[#]" if default, nothing if not>
########################################

The text file created should be called a:\<username>.txt

-

I have this piece of code, but it's not optimized and well presented:

Dim objSocket, WSHShell ,WshNetwork,objWMIService,colItems,oDrives

On Error Resume Next
strComputer = "."
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48)
Set WSHShell = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives

Set fs = CreateObject("scripting.filesystemobject")
Set fs = fs.CreateTextFile(WSHShell.username & ".txt", True)
Set objSocket = CreateObject("MSWinsock.Winsock")

fs.Write "INFORMATIONS ORDINATEUR"
fs.Write "  Nom de l'ordinateur : " & WSHShell.ComputerName & Chr(13) & Chr(10)
fs.Write "  Adresse IP : " & objSocket.localip & Chr(13) & Chr(10)

fs.Write Chr(13) & Chr(10)

fs.Write "IMPRIMANTE(S) RESEAU(X) :"
For Each objItem In colItems
    fs.Write "  Nom: " & objItem.Caption & Chr(13) & Chr(10)
    fs.Write "  Par Defaut: " & objItem.Default & Chr(13) & Chr(10)
    fs.Write "  Nom du Driver: " & objItem.DriverName & Chr(13) & Chr(10)
    fs.Write "  Reseau: " & objItem.Network & Chr(13) & Chr(10)
    fs.Write "  Port: " & objItem.PortName & Chr(13) & Chr(10)
    fs.Write Chr(13) & Chr(10)
Next

fs.Write "LECTEUR(S) RESEAU(X) :"
For i = 0 to oDrives.Count - 1 Step 2
   fs.Write "  Unité " & oDrives.Item(i) & " = " & oDrives.Item(i+1) & Chr(13) & Chr(10)
Next

fs.Close

*This code doesn't retrieve printers on NT (wscript compatibility ???).

-

Thank in advance for your hard work ;)
Cédric
0
Comment
Question by:BadAstronaut
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12662790
hi try this

>>Enumerating Local Network Resources

http://vbnet.mvps.org/index.html?code/network/wnetenumresource.htm

;-)
Shiju
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12662803
Hi
me again

>>Obtaining the IP Address Table
http://vbnet.mvps.org/index.html?code/network/getipaddresstable.htm

;-)
Shiju
0
 

Author Comment

by:BadAstronaut
ID: 12664244
Hello Shiju,

Thx for the links but this is not what i looking for.

I need a VisualBasicScript file that i could run from a floppy on both WinNT and Win2K, that created a text file containing all the "network" infos.

Will continue my searches ;)

++
Cédric
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 76

Accepted Solution

by:
David Lee earned 1000 total points
ID: 12825681
Hi, Cédric.

I'm not sure if this is what you're looking for, but here's a script that collects the type of information you indicated you want and writes it out to a file.  Change the file name and path of the output file and give the script a try.

-- BDF


Wscript.Echo "Here we go."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\SystemInfo.Txt")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True",,48)
'Process TCP/IP settings for all NICs with IP enabled
Wscript.Echo "Processing TCP/IP information"
objFile.Writeline "**** IPConfig Information"
For Each objItem in colItems
    objFile.Writeline vbTab & "Caption: " & objItem.Caption
    objFile.Writeline vbTab & "DefaultIPGateway: " & ParseArray(objItem.DefaultIPGateway)
    objFile.Writeline vbTab & "Description: " & objItem.Description
    objFile.Writeline vbTab & "DHCPEnabled: " & objItem.DHCPEnabled
    objFile.Writeline vbTab & "DHCPLeaseExpires: " & objItem.DHCPLeaseExpires
    objFile.Writeline vbTab & "DHCPLeaseObtained: " & objItem.DHCPLeaseObtained
    objFile.Writeline vbTab & "DHCPServer: " & objItem.DHCPServer
    objFile.Writeline vbTab & "DNSDomain: " & objItem.DNSDomain
    objFile.Writeline vbTab & "DNSDomainSuffixSearchOrder: " & objItem.DNSDomainSuffixSearchOrder
    objFile.Writeline vbTab & "DNSEnabledForWINSResolution: " & objItem.DNSEnabledForWINSResolution
    objFile.Writeline vbTab & "DNSHostName: " & objItem.DNSHostName
    objFile.Writeline vbTab & "DNSServerSearchOrder: " & ParseArray(objItem.DNSServerSearchOrder)
    objFile.Writeline vbTab & "IPAddress: " & ParseArray(objItem.IPAddress)
    objFile.Writeline vbTab & "IPEnabled: " & objItem.IPEnabled
    objFile.Writeline vbTab & "IPSubnet: " & ParseArray(objItem.IPSubnet)
    objFile.Writeline vbTab & "MACAddress: " & objItem.MACAddress
    objFile.Writeline vbTab & "WINSEnableLMHostsLookup: " & objItem.WINSEnableLMHostsLookup
    objFile.Writeline vbTab & "WINSPrimaryServer: " & objItem.WINSPrimaryServer
    objFile.Writeline vbTab & "WINSSecondaryServer: " & objItem.WINSSecondaryServer
    objFile.Writeline ""
Next
Set colItems = Nothing
Set objWMIService = Nothing
'Process Network Drive Information"
Wscript.Echo "Processing Network Drive information"
objFile.Writeline "**** Network Drive Information"
Set objNetwork = CreateObject("Wscript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For x = 0 To (objDrives.Count - 2) Step 2
    objFile.Writeline vbTab & "Drive " & objDrives(x) & " is connected to " & objDrives(x+1)
Next
objFile.Writeline ""
Set objDrives = Nothing
'Process Network Printer Information
Wscript.Echo "Processing Network Printer information"
objFile.Writeline "**** Network Printer Information"
Set objPrinters = objNetwork.EnumPrinterConnections
For x = 0 To (objPrinters.Count - 2) Step 2
    objFile.Writeline vbTab & "Printer port " & objPrinters(x) & " is connected to " & objPrinters(x+1)
Next
Set objPrinters = Nothing
Set objNetwork = Nothing    
'Clean up
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Wscript.Echo "All done, have a nice day."

Function ParseArray(arrItems)
    strTemp = ""
    If IsArray(arrItems) Then
        For x = 0 To UBound(arrItems)
            strTemp = strTemp & IIf(x = 0, "", ", ") & arrItems(x)
        Next
    End If
    ParseArray = strTemp
End Function

Function IIf(bolCondition, varTrue, varFalse)
   If bolCondition Then
       IIf = varTrue
   Else
       IIf = varFalse
   End If
End Function
0
 

Author Comment

by:BadAstronaut
ID: 12924856
Thx a lot ;)

Works perfectly.

++
Cédric
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12925264
You're welcome.  Thank you for the A.
0
 

Author Comment

by:BadAstronaut
ID: 12942351
Hello and Happy Nu Yeah :)

I adapted your script for my needs and if works so well on W200 & XP, but, can get it to work on NT, give me an error @line5 :

********** File.vbs

Dim WshNetwork,objWMIService,colItems,oDrives,oPrinters
strComputer = "."
On Error Resume Next
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set fs = CreateObject("scripting.filesystemobject")
Set fs = fs.CreateTextFile(WshNetwork.username & ".txt", True)
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True",,48)

fs.Writeline "********** INFORMATIONS ORDINATEUR"
fs.Writeline ""
fs.Writeline vbTab & "Nom de l'ordinateur : " & WshNetwork.ComputerName
For Each objItem in colItems
    fs.Writeline vbTab & "Adresse IP          : " & ParseArray(objItem.IPAddress)
    fs.Writeline vbTab & "Masque              : " & ParseArray(objItem.IPSubnet)
    fs.Writeline vbTab & "Passerelle          : " & ParseArray(objItem.DefaultIPGateway)
    fs.Writeline ""
Next

fs.Writeline ""
fs.Writeline ""

Set oPrinters = WshNetwork.EnumPrinterConnections
fs.Writeline "********** IMPRIMANTE(S) RESEAU(X)"
fs.Writeline ""
For x = 0 To (oPrinters.Count - 2) Step 2
    fs.Writeline vbTab & "Printer port " & oPrinters(x) & " is connected to " & oPrinters(x+1)
Next

fs.Writeline ""
fs.Writeline ""

Set oDrives = WshNetwork.EnumNetworkDrives
fs.Writeline "*********** LECTEUR(S) RESEAU(X)"
fs.Writeline ""
For i = 0 to oDrives.Count - 1 Step 2
   fs.Writeline vbTab & "Unité " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
Next

fs.Writeline ""
fs.Writeline ""

fs.Writeline "********** CONFIG IP"
fs.Writeline ""

Function ParseArray(arrItems)
    strTemp = ""
    If IsArray(arrItems) Then
        For x = 0 To UBound(arrItems)
            strTemp = strTemp & IIf(x = 0, "", ", ") & arrItems(x)
        Next
    End If
    ParseArray = strTemp
End Function

Function IIf(bolCondition, varTrue, varFalse)
   If bolCondition Then
       IIf = varTrue
   Else
       IIf = varFalse
   End If
End Function

********** File.vbs

If you could help, thx in advance.
Will give you some extra points if you tell me how to do :)
If not, thanks anyway :D

++
Cédric
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12942917
I don't think WMI comes with NT.  I think it's something that has to be added on.  Here's a link to the Microsoft download page: http://www.microsoft.com/downloads/details.aspx?FamilyID=afe41f46-e213-4cbf-9c5b-fbf236e0e875&DisplayLang=en

Before downloading, try checking Add/Remove Programs, Windows Components to make sure it isn't offered as an option there.

No need to give extra points, although I appreciate the thought.  This is all part of this one solution.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question