[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBSCript and %userprofile% - File NOt found

Posted on 2014-08-21
64
Medium Priority
?
420 Views
Last Modified: 2014-08-26
I am try to write a script that will drop a log file at the root of the user's profile using %userprofile%

I searched online on how to do this but still no luck.

 What is the proper syntax I need to reference %userprofile% and drop a file with a predefined name in this location.
0
Comment
Question by:compdigit44
  • 36
  • 24
  • 4
64 Comments
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276276
Not sure what you mean by "drop a file". This will give you the user profile folder:
Set ws = WScript.CreateObject("WScript.Shell")
strUserProfile = ws.ExpandEnvironmentStrings("%userprofile%")

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276283
I mean the log file created has to be placed in the %userprofile% directory.

When I call the file in the script using strprofile\file.txt I get file not found
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276301
Do you want to copy the log file or move it? What is the source path?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Author Comment

by:compdigit44
ID: 40276310
I need to create a new log file in this location
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276318
Try this:
Dim oWsh, strUserProfile , oFSO, oFile

Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = ws.ExpandEnvironmentStrings("%userprofile%")
Set oFSO= CreateObject("Scripting.FileSystemObject")
Set oFile= oFSO.CreateTextFile("strUserProfile\file.txt")

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276328
I get the error in line one:  "object required: 'ws'
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276339
Use the new code instead of the old one.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276380
I am it errors on line 4

strUserProfile = ws.ExpandEnvironmentStrings("%userprofile%")

Object required 'WS'
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276388
I'm so sorry, should be:
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276404
Thanks!!!

I we are makin progess.. I keep getting path not found though.

Here is my line to insert the log file...

Set objLogFile = objFSO.OpenTextFile("oFile", 8, True, 0)
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276419
Now I'm confused, the code I posted creates a text file (file.txt) in the user profile. Isn't that what you want?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276452
No I think  I am confused since  I not not a scripting person. Below is sample from my scirpt so far...
Dim oWsh, strUserProfile , oFSO, oFile

Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
Set oFSO= CreateObject("Scripting.FileSystemObject")
Set oFile= oFSO.CreateTextFile("strUserProfile\file.txt")



Function LogEntry (LogEntryText)
      objLogFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date(), "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile(oFile, 8, True, 0)

Why is my Set objLogFile = objFSO.OpenTextFile(oFile, 8, True, 0) line not working
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276544
What is going wrong ??????
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276566
Try this:
Dim oWsh, strUserProfile, oFSO, oFile

Call LogEntry("TEST")

Function LogEntry(LogEntryText)
    Set oWsh = WScript.CreateObject("WScript.Shell")
    strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
    oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276598
Still having problems ...

Below is a sample of my script.

Dim oWsh, strUserProfile, oFSO, oFile

Call LogEntry("LogEntryText")

Function LogEntry(LogEntryText)
    Set oWsh = WScript.CreateObject("WScript.Shell")
    strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
    Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, 0)
    oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file="""    '& WScript.Application & " " & WScript.Version & """>"
End Function



Set WshNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = WshNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
      strPrinterPort = colPrinters(intPrinter)
      strPrinterName = colPrinters(intPrinter + 1)
      LogEntry strPrinterName & " - " & strPrinterPort

      If left(colPrinters.Item(intPrinter + 1),len(strSrvName1)) = strSrvName1 Then
            LogEntry "      Printer matches target name.  Printer will be deleted"
            On Error Resume Next
            WshNetwork.RemovePrinterConnection strPrinterName
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40276625
What is the problem and where does it occur?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276652
line 10 char 5 file not found...
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276697
I am sorry one of my co-workers was helping me write then left for vacation
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40276851
Disregard my post about my error it was a typo. New error is permision denied for %userprofile%
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40277396
Try this:
Dim oWsh, strUserProfile , oFSO, oFile

Call LogEntry("TEST")

Function LogEntry (LogEntryText)
     Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
     oFile.Close
     Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
     oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date(), "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40278879
Thanks this seems to be working, although I do not understand something.

In lines 9 & 11 if I rename the file.txt I get a Permission Denied message. Yet it works fine with file.txt

From what I am finding online it seems to be caused by the fact the file could already be open or present. Yet, I have set the file write to append and we are already closing the connection before reading it.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40279320
I have confirm the script is creating the file in the correct location but nothing is getting written to it. The script is close the file after creating it so I am at a loss as to the problem
0
 
LVL 5

Expert Comment

by:ploftin
ID: 40280048
The top portion of your script should look as follows:

Dim oWsh, strUserProfile, oFSO, oFile, strFile

 Call LogEntry("LogEntryText")

 Function LogEntry(LogEntryText)
     Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     strFile = strUserProfile & "\file.txt"
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     If Not oFSO.FileExists(strFile) Then
         oFSO.CreateTextFile strFile
     End If
     Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
     oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file="""    '& WScript.Application & " " & WScript.Version & """>"
 End Function

Open in new window


The problem is you're assigning oFile when you're creating the file and then you're trying to assign it again when you're opening it for appending.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280636
I got the same error again using the code you suggested... :o(
0
 
LVL 5

Expert Comment

by:ploftin
ID: 40280695
How are you executing the script?  Are you double-clicking it, running it from a command prompt using 'cscript scriptname.vbs' or are you assigning this as a startup script that runs when the computer boots?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280713
For testing purposes I am double clicking the script from my desktop but plan on deploying it via a group policy login script
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280717
I just tried to run it using cscript from the command line and got the same permission denined error
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280747
the script is creating the file but not able to write to it after it is created
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280764
I have read online this has nothing to do with a permission problem persay but have to do with the fact the CreateTextFile method is beging used and is failing on the opentextfile method becusae the file is already open.

The suggestion has been to write to the reference instead.. Does anyone know what they are referring to?
http://stackoverflow.com/questions/369242/vbscript-fso-opentextfile-permission-denied
0
 
LVL 5

Expert Comment

by:ploftin
ID: 40280814
The reference is correct.  If you create the text file and leave it open, such as:

Set objFile = objFSO.CreateTextFile(strFile)

Open in new window


Then objFile has the file locked.  However, in the code I'm proposing, we're creating the text file with:

oFSO.CreateTextFile strFile

Open in new window


And this does not leave the file open.  If you're using the block of text below, exactly as I have it, it should run with no issues.  I've written to the log file several times with no issues.  I can run it over and over and it appends properly.

Dim oWsh, strUserProfile, oFSO, oFile, strFile

 Call LogEntry("LogEntryText")

 Function LogEntry(LogEntryText)
     Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     strFile = strUserProfile & "\file.txt"
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     If Not oFSO.FileExists(strFile) Then
         oFSO.CreateTextFile strFile
     End If
     Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
     oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file="""    '& WScript.Application & " " & WScript.Version & """>"
 End Function

Open in new window


Try just that code snippet in a vbs file all by itself and see if it works - it should.  Perhaps you're experiencing another issue that we're not seeing the code for.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280836
Thanks for the reply I am using the exact code of posted earlier and getting the same permission denied at line 13 which is:    

Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280838
Here is my code....
Dim oWsh, strUserProfile, oFSO, oFile, strFile

 Call LogEntry("LogEntryText")

 Function LogEntry(LogEntryText)
     Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     strFile = strUserProfile & "\file.txt"
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     If Not oFSO.FileExists(strFile) Then
         oFSO.CreateTextFile strFile
     End If
     Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
     oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file="""    '& WScript.Application & " " & WScript.Version & """>"
 End Function

strSrvName1 = "\\server1\HP"
strSrvName2 = "\\server2\Dell"

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = WshNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
	strPrinterPort = colPrinters(intPrinter)
	strPrinterName = colPrinters(intPrinter + 1)
	LogEntry strPrinterName & " - " & strPrinterPort

	If left(colPrinters.Item(intPrinter + 1),len(strSrvName1)) = strSrvName1 Then
		LogEntry "	Printer matches target name.  Printer will be deleted"
		On Error Resume Next
		WshNetwork.RemovePrinterConnection strPrinterName
		If Err.Number = 0 Then
			LogEntry "	Printer was succesfully deleted"
		Else
			LogEntry "	An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
		End If
	Else
		LogEntry "	Printer does not match target name.  No action required"
	End If

If left(colPrinters.Item(intPrinter + 1),len(strSrvName2)) = strSrvName2 Then
		LogEntry "	Printer matches target name.  Printer will be deleted"
		On Error Resume Next
		WshNetwork.RemovePrinterConnection strPrinterName
		If Err.Number = 0 Then
			LogEntry "	Printer was succesfully deleted"
		Else
			LogEntry "	An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
		End If
	Else
		LogEntry "	Printer does not match target name.  No action required"
	End If
Next

Open in new window

0
 
LVL 5

Expert Comment

by:ploftin
ID: 40280901
I'm wondering if perhaps another script or program is still running and has the file locked.

Can you please open Task Manager (CTRL + SHIFT + ESCAPE), change to the processes view (or Details view on Windows 8), and look for any processes called 'wscript' or 'wscript.exe'.  If you find any, end task on them.  Then try the script again.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40280950
Nothing is using the script an I only see the one instance of wscript and it goes away  after I close the error dialog box

I have tried three different computers all with the same result

 I am not a scripting person but trying hard to research this error and do not get what I am doing wrong..
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40281645
Any ideas!!!!

I have even tried to change the file location to C:\ on my workstation which I have local admin rights on and still get permission denied.. I am at a total lose an need to find a solution for this is week .. :o(
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40281686
I missed most of the discussion, did the code I wrote work? It worked fine for me.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40281894
I got the same error using your code.

Tried three different workstations 2 Windows 7 and 1 Windows 8 all gave permission denied error.

Why is this failing I am trying so hard to research this and stumped
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40281944
This is the file my code created, is this what it should look like?
file.txt
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40281954
Yes it does contain that text but a dialg box comes up with permission denied...
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40281962
when do you get the permission denied?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40282967
As soon as I double click the script to run an it is always on the OpenTextFile line.


 I did find something interesting. When I copy the script to my %username%b directory and run it from there I get Path Not Found.... This make me would if for some reason the script is not finding the %username% location...
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283068
That's easy enough to find out. The following will display a message box with the user profile path:
Dim oWsh, strUserProfile , oFSO, oFile

Call LogEntry("TEST")

Function LogEntry (LogEntryText)
     Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     MsgBox "The current users profile is located at: " & strUserProfile
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
     oFile.Close
     Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
     oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date(), "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function

Open in new window

0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283071
I just noticed, your not using %username% right? you need %userprofile%.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283088
Sorry ... my script is using %userprofile% I was testing different combinations when I was trying to figure this out.

 I ran you test script from my desktop an it ran perfectly and showed by path as C:\users\MyName
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283096
Does the script write the text to the text file?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283109
Yes the script did write to the file.txt
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283123
In which case I fail to see what the problem is.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283128
This is why I am confused when I run the whole script just like I ran your test script I get permisson denied. I am using the same script I posted earlier.
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283165
Does this script work:
strSrvName1 = "\\server1\HP"
strSrvName2 = "\\server2\Dell"

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = WshNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
    strPrinterPort = colPrinters(intPrinter)
    strPrinterName = colPrinters(intPrinter + 1)
    LogEntry strPrinterName & " - " & strPrinterPort

    If Left(colPrinters.Item(intPrinter + 1), Len(strSrvName1)) = strSrvName1 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If

    If Left(colPrinters.Item(intPrinter + 1), Len(strSrvName2)) = strSrvName2 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If
Next

Function LogEntry(LogEntryText)
     Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     MsgBox "The current users profile is located at: " & strUserProfile
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
     oFile.Close
     Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
     oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
     oFile.Close
End Function

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283196
Well I do not get a permissoin denied error but I get a dialog box that pops up showing by location as C:\users\MyUser name but will not go away when we click on it.

I think we " You" are getting close .. You are scripting master!!!!
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283328
Delete line 41.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283499
It working with no errors.. The only thing I noticed in the log file it that it states the printer was deleted but does listed which on..

Thank you so much for your help
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283603
I'm sorry I don't understand. What information is missing?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283647
Sorry for the poor explanation. There are no errors and the script runs perfectly..  I noticed in the file.txt file it states a printer was deleted or not deleted but doesn't the the printer's name
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283780
Try this script, does it give you the names?
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set oDrives = WshNetwork.EnumNetworkDrives
    Set oPrinters = WshNetwork.EnumPrinterConnections
    For i = 0 to oDrives.Count - 1 Step 2
        WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
    Next

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283833
Should I run this script as it or add it to the order script you wrote earlier today?
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283842
Run it separately.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283858
I ran the script but it brought up a dialog box with my home folder path and nothing about printers.
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40283887
Sorry my bad, try this script:
         Set WshNetwork = WScript.CreateObject("WScript.Network")
         Set oDrives = WshNetwork.EnumNetworkDrives
         Set oPrinters = WshNetwork.EnumPrinterConnections
         WScript.Echo "Network drive mappings:"
         For i = 0 to oDrives.Count - 1 Step 2
            WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
         Next
         WScript.Echo 
         WScript.Echo "Network printer mappings:"
         For i = 0 to oPrinters.Count - 1 Step 2
            WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
         Next

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283897
After multiple dialog boxes if does end up listing my printers..
0
 
LVL 28

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 40283929
Is this OK:
strSrvName1 = "\\server1\HP"
strSrvName2 = "\\server2\Dell"

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = WshNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
    strPrinterPort = colPrinters.Item(intPrinter)
    strPrinterName = colPrinters.Item(intPrinter + 1)
    LogEntry strPrinterName & " - " & strPrinterPort

    If Left(colPrinters.Item(intPrinter + 1), Len(strSrvName1)) = strSrvName1 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If

    If Left(colPrinters.Item(intPrinter + 1), Len(strSrvName2)) = strSrvName2 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If
Next

Function LogEntry(LogEntryText)
     Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     MsgBox "The current users profile is located at: " & strUserProfile
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
     oFile.Close
     Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
     oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
     oFile.Close
End Function

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40283946
It think it is working!!!

 I have to test it some more but so far it is looking good..........

You are a master... If I could award 50,000 points for this would ..

 I will post back shortly.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40285452
Everything worked perfectly. Thank you so much for your help. Script is one of those things I have wanted to learn and even though this was stressfull I did learn something as well!!!!!
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40286147
Glad it worked. Thanks for the points.
0

Featured Post

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.

Question has a verified solution.

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

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…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 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