Adding Or Statement to VBScript

I recently post on question on EE on "How to delete client network printer mappings LIke.."
http:Q_28498579.html

I got a great answer to my posting and have since closed it. I have one more question to add though.

How do I an OR statement to the line below to the script will delete  all printer starting with "\\serverA\ HP 43" or \\serverA\HP 123"


If left(colPrinters.Item(intPrinter + 1),len(strSrvName)) = strSrvName Then
LVL 20
compdigit44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
Please try this version of the previously accepted solution code
Set WshNetwork = WScript.CreateObject("WScript.Network")

Set oPrinters = WshNetwork.EnumPrinterConnections
dim srvName, srvNames 
srvNames = ("\\serverA\ HP 43","\\serverA\HP 123")
WScript.Echo 
WScript.Echo "Network printer mappings:"
For Each srvName In srvNames
    For i = 0 to oPrinters.Count - 1 Step 2
        if left(oPrinters.Item(i+1),len(srvName))=srvName then
            WScript.Echo "Delete#" & i & "=Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
            WshNetwork.RemovePrinterConnection oPrinters.Item(i+1)
        end if
    Next
Next

Open in new window

0
compdigit44Author Commented:
Thanks!!!!

I keep getting the error expected ")" on line five and yes I did put a parentheses at both ends.
0
aikimarkCommented:
oops.  line 5 should be this
srvNames = Array("\\serverA\ HP 43","\\serverA\HP 123")

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

compdigit44Author Commented:
Any thoughts I do not understand What I am doing wrong
0
compdigit44Author Commented:
WOrks !!!

 I did notice that if the second part of the string is missing the script will error out but work find if both string are present.. This is why I thought I needed some type of OR statement
0
aikimarkCommented:
As long as you are matching the leading characters, this code should work.  You can use the regular expression object to do wildcard matching of various patterns.
0
compdigit44Author Commented:
Thanks... I think I am making progress new error is:

Type mismatch: 'strSrvName'
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
strSrvName  is only mentioned in your original question, and has been replaced. The replacement code uses srvName inside the loop.
0
compdigit44Author Commented:
Your right I made a mistake. I also found that I had to put quotes around the strName string in the following line

if left(oPrinters.Item(i+1),len("srvName"))="srvName" then

The script is running since I added a line to export status to a log file yet it is not  deleteing any printers
0
compdigit44Author Commented:
I think it has something to do with the array possible it may be looking for this as a whole queue name...

("\\serverA\ HP 43","\\serverA\HP 123")
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Your line is wrong. It checks if the printer starts with "srvname", and not with the value of that var. The correct line is
   if left(oPrinters.Item(i+1),len(srvName))=srvName then
as shown in aikimark's code.
Also, the array contains a space before the first printer name, certainly incorrect too.
The strings in the array must match exactly, else nothing will happen.
0
compdigit44Author Commented:
Here is what i have so far...

strSrvName = Array("\\ServerA\123 ","\\Server2\HP")
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("strSrvName")) = "strSrvName" Then
            LogEntry "      Printer matches target name.  Printer will be deleted"
            On Error Resume Next
            WshNetwork.RemovePrinterConnection strPrinterName
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There is something missing at the end, and that is not even remotely what aikimark posted.
Set WshNetwork = WScript.CreateObject("WScript.Network")

Set oPrinters = WshNetwork.EnumPrinterConnections
dim srvName, srvNames 
srvNames = Array("\\ServerA\123 ","\\Server2\HP")
WScript.Echo 
WScript.Echo "Network printer mappings:"
For Each srvName In srvNames
    For i = 0 to oPrinters.Count - 1 Step 2
        strPrinterPort = colPrinters(intPrinter)
        strPrinterName = colPrinters(intPrinter + 1)
        If left(strPrinterName, len(srvName))=srvName then
            WScript.Echo "Delete#" & i & "=Port " & strPrinterPort) & " = " & strPrinterName
            WshNetwork.RemovePrinterConnection strPrinterName
        End if
    Next
Next

Open in new window

0
compdigit44Author Commented:
Ok here is the full code. One of my co workers who is now out sick  helped me to addi logging to this script
strSrvName = Array("\\ServerA\122 ","\\ServerB\HP")

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("C:\PrinterLog.txt", 8, True, 0)
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("strSrvName")) = "strSrvName" 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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You should be able to replace the necessary parts. It is in no way difficult.
If you do not understand the code, there is not much value in providing it to you.
0
aikimarkCommented:
Please try this
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

strSrvName = Array("\\ServerA\122 ","\\ServerB\HP")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("C:\PrinterLog.txt", 8, True, 0)
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
    For Each strMask In strSrvName
        If left(colPrinters.Item(intPrinter + 1),len(strMask)) = strMask 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
Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
compdigit44Author Commented:
Thanks I am trying but know I am missing something
0
aikimarkCommented:
how do you know you are missing something?
0
compdigit44Author Commented:
I gave up and decided to go back to the orginal scirpt that will delete one type of printer at a time
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.