Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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
0
compdigit44
Asked:
compdigit44
  • 10
  • 5
  • 4
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now