Saturday, January 19, 2008

Adding a Printer on a Remote Computer

These two scripts will add an IP port and install a printer on a remote machine. This is good to add a local IP printer on a remote workstation. For this script, you need to install prnadmin.dll. The prnadmin.dll file is available in the Windows Server 2003 Resource Kit Tools.

Add an IP port:
'Required files:prnadmin.dll
'prnadmin.dll is available
'in the Windows Resource Kit located

dim oPort
dim oMaster
set oPort = CreateObject("Port.Port.1")
set oMaster = CreateObject("PrintMaster.PrintMaster.1")
Set WshShell = WScript.CreateObject("WScript.Shell")

'Edit for comouter name
strMachineName = "\\computer"

'Edit for IP port name
strPortName = "Name of the Port"

'Edit for IP address
strIPAdress = "Insert Printer IP Address Here"

'Indicate where to add the port. Double quotes ("" ) stand for the local computer, which is the default.
oPort.ServerName = strMachineName

'The name of the port cannot be omitted.
oPort.PortName = strPortName

'The type of the port can be 1 (TCP RAW), 2 (TCP LPR), or 3 (standard local).
oPort.PortType = 1

'Mandatory for TCP ports. This is the address of the device to which the port connects.
oPort.HostAddress = strIPAdress

'For TCP RAW ports. Default is 9100.
oPort.PortNumber = 9100

'Enable or disable SNMP.
oPort.SNMP = false

'Applies to TCP LPR name, default is LPR
oPort.QueueName = "Queue"

'Byte counting or double spool applies to TCP LPR ports, is disabled by default.
oPort.DoubleSpool = false

'Try adding the port.
oMaster.PortAdd oPort
'Test for the status.
If Err <> 0 then

'An error occurred.
WshShell.Popup "Error " & Return & " returned during setup.", 10, "Printer port add.", 16
end if

WshShell.Popup "IP Port Add Successful", 10, "Install Status", 64

Add a Printer on a Remote Computer:

'Required files:prnadmin.dll
'prnadmin.dll is available
'in the Windows Resource Kit located

dim oMaster
dim oPrinter
Set WshShell = WScript.CreateObject("WScript.Shell")
set oMaster = CreateObject("PrintMaster.PrintMaster.1")
set oPrinter = CreateObject("Printer.Printer.1")

'Edit for Computer Name
'name of computer you want to add IP port to
strMachineName = "enter computer name here"

'Edit for Printer Name. This is the name users will see when selecting this printer
strPrinterName = "printer display name"

'Edit for Printer Driver Name. This specifies the printer driver to use.
'This must match the print driver name exactly.
'The string is required and cannot be empty.
'Example below is for the HP LaserJet 9040 Post Script
strDriverName = "HP LaserJet 9040 PS"

'Edit for the IP Port Name
'change aaa.bbb.ccc.ddd with the IP address of the printer
strPortName = "IP_aaa.bbb.ccc.ddd"

'Edit for the Driver Path.
'Where you unzipped the printer driver files to.
strDriverPath = "\\computer name\share name\"

'Edit for the INF path. Usually the same as the driver path plus the INF file
'point to where you unzipped the driver files
strINF = "\\computer name\share name\HP LaserJet 9040 PS.inf"

oPrinter.ServerName = strMachineName
oPrinter.PrinterName = strPrinterName
oPrinter.DriverName = strDriverName
oPrinter.PortName = strPortName
oPrinter.DriverPath = strDriverPath
oPrinter.InfFile = strINF

'The following code adds the printer.
oMaster.PrinterAdd oPrinter

'The following code uses the Err object to determine whether the printer was added successfully.
if Err <> 0 then
'An error occurred
WshShell.Popup "Error " & Return & " returned during setup.", 10, "Printer add.", 16
end if

WshShell.Popup "Printer Add Successful", 10, "Install Status", 64

Monday, January 14, 2008

Sending Email from A VBScript

You can add this code to your scripts to send an email.

Dim strComputer, sTextBody, objEmail, strSMTPServer, strFrom, strTo, strSubject, strTextbody

strSMTPServer = "enter the smtp server here"
strFrom = "enter the from email address here"
strTo = "enter the to email address here"
strSubject = "enter the email subject here"
strTextbody = "enter the body of the email here"

Set objEmail = CreateObject("CDO.Message")
objEmail.From = strFrom
objEmail.To = strTo
objEmail.Subject = strSubject
objEmail.Textbody = strTextbody
objEmail.AddAttachment "to add an attachment to the email, type in the file path"
objEmail.Configuration.Fields.Item("") = 2
objEmail.Configuration.Fields.Item("") = strSMTPServer
objEmail.Configuration.Fields.Item("") = 25

Set objEmail = Nothing

Thursday, January 10, 2008

Enable or Disable Remote Desktop on Multiple Computers

This script will enable or disable Remote Desktop on multiple computers on an IP segment. The first input box asks for the subnet (i.e. 192.168.1). The second input box asks for the lowest number on the segment that you would like to start with. The third box asks for the highest number on the segment that you want to end with. I would like to add more to this script that will allow it to ping an IP address first. If there is no response it will move on to the next address instead of trying to enable RD on a non-existent PC. I will add that when I have time.

DIM strComputer, strIPInput, strLow, strHigh
On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")

'Set to 0 to disable Remote Desktop, set to 1 to enable Remote Desktop

strIPInput = InputBox("What is the subnet? Format as XX.YY.ZZ.")
strLow = InputBox("What is the low address in the range?")
strHigh = InputBox("What is the highest address in the range?")

If Right(strIPInput,1) <> "." Then
strIPInput = strIPInput & "."
End If

If strIPInput = "" OR strLow = "" OR strHigh = "" OR strHigh > 254 Then
Wscript.Echo "Invalid input - Script Cancelled"
End If

For i = strLow to strHigh

strComputer = strIPInput & i

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery ("Select * from Win32_TerminalServiceSetting")

For Each objItem in colItems
errResult = objItem.SetAllowTSConnections(ENABLE_CONNECTIONS)

WshShell.Popup "Complete", 10, "Remote Desktop Status", 64

Sunday, January 6, 2008

Adding Network Printers Using a VBS Script

This script will install network printers for users that are a member of a specified security group. You can run this from the user's login script.

Dim shellobj, netobj, username, adsobj

On Error Resume Next

Set shellobj = WScript.CreateObject("WScript.Shell")
Set netobj = WScript.CreateObject("WScript.Network")

While username = ""
username = netobj.UserName

adsPath = "WinNT://Active Directory Domain Name/" & username
Set adsobj = GetObject(adsPath)

For Each Prop IN adsobj.Groups
Select Case Prop.Name

Case "security group"
netobj.AddWindowsPrinterConnection "\\server name\printer share name"

Saturday, January 5, 2008

Create a Desktop Shortcut on the All Users Desktop

This script will create a shortcut on the All Users Desktop.

'Define the All Users desktop file path.
allusersdesktop = "C:\Documents and Settings\All Users\Desktop"

set WshShell = WScript.CreateObject("WScript.Shell")
set oShellLink = WshShell.CreateShortcut(allusersdesktop & "\VBSBob.lnk")
'Define where you would like the shortcut to point to. This can be a program, Web site, etc.
oShellLink.TargetPath = ""
oShellLink.WindowStyle = 1
'Define what icon the shortcut should use. Here we use the explorer.exe icon.
oShellLink.IconLocation = "C:\WINDOWS\explorer.exe"
oShellLink.Description = "VBSBob"
oShellLink.WorkingDirectory = allusersdesktop

VBS Reference

VBS Site Links:

Lock Workstation


I am going to start things off easy. Here is a script I created that will automatically lock a workstation. If you do not know how to create a VBS script, stay tuned. I will post a basic tutorial on creating VBS scripts. Anyway, here is the script; start your copy at 'VBSBOB:

On Error Resume Next

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "%windir%\System32\rundll32.exe user32.dll,LockWorkStation"