Take 2 - added some Do loops and commented a bit on the code (not much)
'Create the Function To determine And store current default gateway
'
Function GetDefaultGateway
Dim Adapter, Adapters, WMI
GetDefaultGateway = ""
Set WMI = GetObject("winmgmts:\\.\root\cimv2")
Set Adapters = WMI.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each Adapter In Adapters
If Not IsNull(Adapter.DefaultIPGateway) Then
If Not Adapter.defaultIPGateway(0) = "0.0.0.0" Then
GetDefaultGateway = Adapter.DefaultIPGateway(0)
Exit For
End If
End If
Next
End Function
'start case for determining which office end user is in
'
Dim oPrinter 'to be used throughout script for printer addition
Set oPrinter = CreateObject("WScript.Network")
Select Case GetDefaultGateway
Case "111.111.111.111" 'default gateway ip of office 1 (multi floor office #1)
Dim FloorLevelA
Do 'added do loop so end user will always know whats wrong if incorrect input
FloorLevelA = InputBox("Choose your floor level" & Chr(13) & "Please enter one of the below options;" & Chr(13) & Chr(13) & "1 = Level 2" & Chr(13)& "2 = Level 3","Office 1 Printer Setup")
If VarType(FloorLevelA) = 0 Then
MsgBox "You have cancelled the printer installation utility", 64, "Install Cancelled"
Exit Do
Else
Select Case FloorLevelA
Case "1"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER1"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER2"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER3"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER4"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER5"
oPrinter.SetDefaultPrinter "\\PSERVER1\PRINTER1"
MsgBox "Your printers have been added and your default set to PRINTER1", 64, "Printer Installation Completed"
Exit Do
Case "2"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER1"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER2"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER3"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER4"
oPrinter.AddWindowsPrinterConnection "\\PSERVER1\PRINTER5"
oPrinter.SetDefaultPrinter "\\PSERVER1\PRINTER5"
MsgBox "Your printers have been added and your default set to PRINTER5", 64, "Printer Installation Completed"
Exit Do
Case Else
MsgBox "Error: Incorrect entry. Please try again", 16, "Unavailable Selection" 'typed incorrect number choice, start do loop again
End Select
End If
Loop
Case "112.112.112.112" 'default gateway ip of office 2
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER1"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER2"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER3"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER4"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER5"
oPrinter.SetDefaultPrinter "\\PSERVER2\PRINTER1"
MsgBox "Your printers have been added and your default set to PRINTER1", 64, "Printer Installation Completed"
Case "113.113.113.113" 'default gateway office 3 (multi floor office #2)
Dim FloorLevelB
Do
FloorLevelB = InputBox("Choose your floor level" & Chr(13) & "Please enter one of the below options;" & Chr(13) & Chr(13) & "1 = Ground floor" & Chr(13)& "2 = Level 1","Office 3 Printer Setup")
If VarType(FloorLevelB) = 0 Then
MsgBox "You have cancelled the printer installation utility", 64, "Install Cancelled"
Exit Do
Else
Select Case FloorLevelB
Case "1"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER1"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER2"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER3"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER4"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER5"
oPrinter.SetDefaultPrinter "\\PSERVER2\PRINTER4"
MsgBox "Your printers have been added and your default set to PRINTER4", 64, "Printer Installation Completed"
Exit Do
Case "2"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER1"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER2"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER3"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER4"
oPrinter.AddWindowsPrinterConnection "\\PSERVER2\PRINTER5"
oPrinter.SetDefaultPrinter "\\PSERVER2\PRINTER5"
MsgBox "Your printers have been added and your default set to PRINTER5", 64, "Printer Installation Completed"
Exit Do
Case Else
MsgBox "Error: Incorrect entry. Please try again", 16, "Unavailable Selection"
End Select
End If
Loop
End Select
' VPN Office
Case "192.168.0.1" 'this is the VPN only office - default gateway if not actually connected via VPN
MsgBox "You are not currently connected to the VPN client - Please connect up to the Cisco AnyConnect VPN client and try again", 16, "VPN Not Connected"
Case "222.222.222.222" 'connected via VPN. determines that either client in the VPN only office, or roaming
Dim VPNmsg
VPNmsg = MsgBox ("Are you in the VPN Office at Office 4?" & Chr(13) & Chr(13) & "Select 'No' if you are using a USB Modem.", _ 'vbYesNo for user choice - Yes, I am in VPN office, or No, I am roaming
vbYesNo + vbQuestion, _
"VPN Connection Printer Setup")
Select Case VPNmsg
Case vbYes 'if yes, add direct IP print for the local printer that's tunneled in the VPN program
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set shell = WScript.CreateObject( "WScript.Shell" )
CompName = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objWMIService = GetObject("winmgmts:\\" & CompName & "\root\cimv2")
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
Set oShell = WScript.CreateObject("WScript.shell")
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Sub createPort (name, ip)
objNewPort.Name = name
objNewPort.Protocol = 1
objNewPort.HostAddress = ip
objNewPort.SNMPEnabled = False
objNewPort.Put_
End Sub
Sub addPrinter (driver, port, name)
objPrinter.DriverName = driver
objPrinter.PortName = port
objPrinter.DeviceID = name
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.ShareName = ""
objPrinter.Put_
End Sub
createPort "PRINTER1_IP", "192.168.0.250"
oshell.run "RUNDLL32 PRINTUI.DLL,PrintUIEntry /if /b ""Office 4"" /f \\server1\software$\drivers\mpc5501a\oemsetup.inf /r ""PRINTER1_IP"" /m ""PCL6 Driver for Universal Print""" ,,True
oPrinter.SetDefaultPrinter "Office 4"
MsgBox "Your printer (Office 4) has been added and set as your default printer", 64, "Printer Installation Completed"
Case vbNo 'no is selected, client is roaming. so give client choice of main printer in each office to be installed as direct ip printing
Dim OfficeRoam
OfficeRoam = InputBox("Please select which office to print to and the click OK;" & Chr(13) & Chr(13) & "1 = Office 1 Level 2" & Chr(13)& "2 = Office 1 Level 3" & Chr(13)& "3 = Office 2 level 1" & Chr(13)& "4 = Office 3" & Chr(13) & Chr(13)& "*Note 1* Printing to Office 4 is not available whilst roaming" & Chr(13)& "*Note 2* This process will take around 10 minutes and download 15mb of data","VPN Roaming Printer Setup - All Offices")
Select Case OfficeRoam
Case "1"
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set shell = WScript.CreateObject( "WScript.Shell" )
CompName = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objWMIService = GetObject("winmgmts:\\" & CompName & "\root\cimv2")
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
Set oShell = WScript.CreateObject("WScript.shell")
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Sub createPort (name, ip)
objNewPort.Name = name
objNewPort.Protocol = 1
objNewPort.HostAddress = ip
objNewPort.SNMPEnabled = False
objNewPort.Put_
End Sub
Sub addPrinter (driver, port, name)
objPrinter.DriverName = driver
objPrinter.PortName = port
objPrinter.DeviceID = name
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.ShareName = ""
objPrinter.Put_
End Sub
createPort "PRINTER2_IP", "111.111.111.250"
oshell.run "RUNDLL32 PRINTUI.DLL,PrintUIEntry /if /b ""PRINTER2 IP Print"" /f \\server1\software$\drivers\mpc5501a\oemsetup.inf /r ""PRINTER2_IP"" /m ""PCL6 Driver for Universal Print""" ,,True
MsgBox "PRINTER2 IP Print has been added to your printer list", 64, "Printer Installation Completed"
Case "2"
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set shell = WScript.CreateObject( "WScript.Shell" )
CompName = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objWMIService = GetObject("winmgmts:\\" & CompName & "\root\cimv2")
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
Set oShell = WScript.CreateObject("WScript.shell")
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Sub createPort (name, ip)
objNewPort.Name = name
objNewPort.Protocol = 1
objNewPort.HostAddress = ip
objNewPort.SNMPEnabled = False
objNewPort.Put_
End Sub
Sub addPrinter (driver, port, name)
objPrinter.DriverName = driver
objPrinter.PortName = port
objPrinter.DeviceID = name
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.ShareName = ""
objPrinter.Put_
End Sub
createPort "PRINTER5_IP", "111.111.111.249"
oshell.run "RUNDLL32 PRINTUI.DLL,PrintUIEntry /if /b ""PRINTER5 IP Print"" /f \\server1\software$\drivers\mpc5501a\oemsetup.inf /r ""PRINTER5_IP"" /m ""PCL6 Driver for Universal Print""" ,,True
MsgBox "PRINTER5 Ip Print has been added to your printer list", 64, "Printer Installation Completed"
Case "3"
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set shell = WScript.CreateObject( "WScript.Shell" )
CompName = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objWMIService = GetObject("winmgmts:\\" & CompName & "\root\cimv2")
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
Set oShell = WScript.CreateObject("WScript.shell")
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Sub createPort (name, ip)
objNewPort.Name = name
objNewPort.Protocol = 1
objNewPort.HostAddress = ip
objNewPort.SNMPEnabled = False
objNewPort.Put_
End Sub
Sub addPrinter (driver, port, name)
objPrinter.DriverName = driver
objPrinter.PortName = port
objPrinter.DeviceID = name
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.ShareName = ""
objPrinter.Put_
End Sub
createPort "PRINTER5_IP", "112.112.112.250"
oshell.run "RUNDLL32 PRINTUI.DLL,PrintUIEntry /if /b ""PRINTER5 IP Print"" /f \\server1\software$\drivers\mpc5501a\oemsetup.inf /r ""PRINTER5_IP"" /m ""PCL6 Driver for Universal Print""" ,,True
MsgBox "PRINTER5 IP Print has been added to your printer list", 64, "Printer Installation Completed"
Case "4"
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set shell = WScript.CreateObject( "WScript.Shell" )
CompName = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objWMIService = GetObject("winmgmts:\\" & CompName & "\root\cimv2")
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
Set oShell = WScript.CreateObject("WScript.shell")
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Sub createPort (name, ip)
objNewPort.Name = name
objNewPort.Protocol = 1
objNewPort.HostAddress = ip
objNewPort.SNMPEnabled = False
objNewPort.Put_
End Sub
Sub addPrinter (driver, port, name)
objPrinter.DriverName = driver
objPrinter.PortName = port
objPrinter.DeviceID = name
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.ShareName = ""
objPrinter.Put_
End Sub
createPort "PRINTER1_IP", "113.113.113.250"
oshell.run "RUNDLL32 PRINTUI.DLL,PrintUIEntry /if /b ""PRINTER1 IP Print"" /f \\server1\software$\drivers\mpc5501a\oemsetup.inf /r ""PRINTER1_IP"" /m ""PCL6 Driver for Universal Print""" ,,True
MsgBox "PRINTER1 IP Print has been added to your printer list", 64, "Printer Installation Completed"
Case Else
MsgBox "Error: Incorrect entry. Please try again", 16, "Unavailable Selection"
End Select
End Select
Case Else
MsgBox "You are not connected to the Cisco AnyConnect VPN Client or not in an Project office." & Chr(13) & Chr(13) & "Option 1: Connect up the Cisco AnyConnect VPN Client (Perth) and run this again" & Chr(13) & Chr(13) & "Option 2: Contact your local support personal for further assistance", 48, "No MyBusiness Network Detected"
End Select