Solved

VB script to display mapped drives and printers (output to username.txt)

Posted on 2011-02-28
11
515 Views
Last Modified: 2012-05-11
Hi guys, I have the below code (which I stole from here) but need a little help in making a small change.

Instead of the output going to a fixed filename I need it to create it based on the user's username.

In DOS it would be equivalent to "c:\folder\%username%.txt" but have no idea how to do this in VB.

Option Explicit

Const cForWriting = 2
Const cOutFile = "C:\folder\filename.txt"

Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile(cOutFile, cForWriting, True)

Set objNetwork = CreateObject("WScript.Network")
Set colDrives = objNetwork.EnumNetworkDrives

If colDrives.Count = 0 Then
   WScript.Echo "Guy's warning: No Drives Mapped "
   Wscript.Quit
End If

objOutFile.WriteLine("Network Drives:" & vbCrLf)

For intDrive = 0 To (colDrives.Count -1) Step 2
   intNetLetter = IntNetLetter +1
   objOutFile.WriteLine("UNC Path " & intDrive & " - " & colDrives.Item(intDrive) & " Mapped drive No : " & colDrives.Item(intDrive +1))
Next

objOutFile.WriteLine("")

strComputer ="."
intPrinters = 1

' --------------------------------------------
' Pure WMI Section
Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")

Call Wait() ' Goto Sub Routine at the end

' On Error Resume Next
For Each objItem In colItems
   objOutFile.WriteLine("Printers on " _
      & objItem.name & ", Printer Number: " & intPrinters & VbCrLf & _
      "====================================" & VbCrLf & _
      "Availability: " & objItem.Availability & VbCrLf & _
      "Description: " & objItem.Description & VbCrLf & _
      "Printer: " & objItem.DeviceID & VbCrLf & _
      "Driver Name: " & objItem.DriverName & VbCrLf & _
      "Port Name: " & objItem.PortName & VbCrLf & _
      "Printer State: " & objItem.PrinterState & VbCrLf & _
      "Printer Status: " & objItem.PrinterStatus & VbCrLf & _
      "PrintJobDataType: " & objItem.PrintJobDataType & VbCrLf & _
      "Print Processor: " & objItem.PrintProcessor & VbCrLf & _
      "Spool Enabled: " & objItem.SpoolEnabled & VbCrLf & _
      "Separator File: " & objItem.SeparatorFile & VbCrLf & _
      "Queued: " & objItem.Queued & VbCrLf & _
      "Status: " & objItem.Status & VbCrLf & _
      "StatusInfo: " & objItem.StatusInfo & VbCrLf & _
      "Published: " & objItem.Published & VbCrLf & _
      "Shared: " & objItem.Shared & VbCrLf & _
      "ShareName: " & objItem.ShareName & VbCrLf & _
      "Direct: " & objItem.Direct & VbCrLf & _
      "Location: " & objItem.Location & VbCrLf & _
      "Priority: " & objItem.Priority & VbCrLf & _
      "Work Offline: " & objItem.WorkOffline & VbCrLf & _
      "Horizontal Res: " & objItem.HorizontalResolution & VbCrLf & _
      "Vertical Res: " & objItem.VerticalResolution & VbCrLf & _
      "")
   intPrinters = intPrinters + 1
Next

objOutFile.Close
WScript.Quit

sub Wait()
   If strComputer = "." then
      strComputer = "Local Host"
   else 
      strComputer = strComputer
   end if

'   WScript.Echo "Wait 2 mins for " & strComputer & " to enumerate printers"

End Sub

Open in new window

0
Comment
Question by:amaru96
  • 5
  • 5
11 Comments
 
LVL 9

Expert Comment

by:sshah254
ID: 35003839
use this to get the username into a variable

Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
wshShell = Nothing

then use that username to open the appropriate file

Const cOutFile = "C:\folder\" & str_username & ".txt"

The rest of the things should remain the same.

Sandip
0
 
LVL 1

Author Comment

by:amaru96
ID: 35003931
Hi sshah, I entered that code as below but am getting an error when I run it.

Option Explicit

Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
wshShell = Nothing

Const cForWriting = 2
Const cOutFile = "C:\temp\" & str_username & ".txt"

Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile    
vberror.jpg
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35006533
Change

Const cOutFile = "C:\temp\" & str_username & ".txt"

to

cOutFile = "C:\temp\" & str_username & ".txt"

~bp
0
 
LVL 1

Author Comment

by:amaru96
ID: 35012942
Am now getting:

Line: 3
Char: 1
Error: Variable is undefined: 'wshShell'
Code: 800A01F4
Source: Microsoft VBScript runtime error

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35013036
You will need to DIM all variables before they are used, since you have OPTION EXPLICIT. So you need at least this line, but there may be more.

Dim wshShell, str_username, cOutFile

~bp
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:amaru96
ID: 35013091
Am getting another error now.

Line: 6
Char: 1
Error: Type mismatch
Code: 800A000D

Below is the first 20 lines of my code:

Option Explicit

Dim wshShell, str_username, cOutFile

Set wshShell = CreateObject( "WScript.Shell" )
str_username wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
wshShell = Nothing

Const cForWriting = 2
cOutFile = "C:\temp\" & str_username & ".txt"

Dim objFSO, objNetwork, colDrives, intDrive, intNetLetter
Dim objWMIService, objItem, colItems, strComputer, intPrinters
dim objOutFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutFile = objFSO.OpenTextFile(cOutFile, cForWriting, True)

Set objNetwork = CreateObject("WScript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35013195
Looks like an equals sign is missing.

str_username = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

~bp
0
 
LVL 1

Author Comment

by:amaru96
ID: 35013280
Error

Line: 7
Char: 1
Error: Object variable not set

Line 7 is: wshShell = Nothing

Appreciate your help so far.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35013388
You can safely remove that line (line 7).

~bp
0
 
LVL 1

Author Comment

by:amaru96
ID: 35013396
Fantastic, works great. Thanks!!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35013406
Welcome.

~bp
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now