Archive

Archive for the ‘VB Scripting’ Category

Forcing a printer online using a script:

printer_iconRecently while installing Zetafax 11, we noticed that some of our maintenance scripts on our XenApp servers were causing the zetafax printer to fall offline on reboot. Below is a simple to use (built in to server 2003) script that can be used to force a printer online.

The Following will force “Printer Name” to work online:

cscript C:\WINDOWS\system32\prncnfg.vbs -t -p “Printer Name” -workoffline

As an extra function, the below will rename a local printer:

Cscript %windir%\system32\Prncnfg.vbs -x -p “old printer name” -z newprintername

Free Disk Space Report:

Whether its for daily checks or a for a once off report this handy little script allows you to connect to as many servers as you like, query all local drives, report the capacity of the drives and the free space available. It writes all this information to a snazzy HTML file that is saved on the local disk.

This is a vbs file and uses a servers.txt (keep the txt file in the same location as the script) file as a reference for each of the servers you wish to connect to. I’ve tested this on up to 30 servers at a time with flawless output. This script can be run as a schedueled task and accross psexec. Just one point to note, if it is being executed from a UNC path, map the drive first or it will fail to lookup servers.txt

I use this script as a daily check to check up on all the file servers and exchange servers in the morning.

Heres an example of the html file you are finished with.

Server Utilization Report

Server1 Drive Utilization


C:\ Size: 20,003 MB Free: 8,791 MB |||||||||||||||||||||||||||||||||||||||||||| 43.95% Free
E:\ Size: 10,000 MB Free: 3,715 MB ||||||||||||||||||||||||||||||||||||| 37.15% Free

Created 26/02/2008 12:14:12 by morgan_a

For the code, click more…

Read more…

Sending An email via a script:

January 18, 2008 Leave a comment

Recently i found myself in need of the ability to email a status script to my team. Not only this, but it would also need to attach an file. This script would run when a service stopped, start the service with a batch file doing a simple “net start ‘service'”, querying the service with psservice then call a VBS file to email the results of the net start command. This gave us visability of crappy software services stopping and the attempted automated restart.

I came across this script on our corporate network, cant give the credit as dont know where its due, but its excellent for a free script. More info after the jump, its quite a large script.

_____________________________________________________________________________________

dtmDate = Date

strMonth = Month(Date)
strDay = Day(Date)
strYear = Right(Year(Date),2)

bFlag = CDOSYS_Send_Email(“<server>”,””,””,”<Recipient1@email.com;Recipient2@email.com>”,””,””,”<From address>”,”<Subject>””<Message Body>”,”<attachment>”)

Function CDOSYS_Send_Email(sSmtpServer,sUsername,sPassword,sTo,sCC,sBCC,sFrom,sSubject,sBody,sAttach)
On Error Resume Next
‘These weird constants do NOT cause any communications with schemas.microsoft.com. They are just naming conventions.
Const cdoSendUsingMethod = “http://schemas.microsoft.com/cdo/configuration/sendusing&#8221;
Const cdoSMTPServer = “http://schemas.microsoft.com/cdo/configuration/smtpserver&#8221;
Const cdoSMTPServerPort = “http://schemas.microsoft.com/cdo/configuration/smtpserverport&#8221;
Const cdoSMTPConnectionTimeout = “http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout&#8221;
Const cdoSMTPAuthenticate = “http://schemas.microsoft.com/cdo/configuration/smtpauthenticate&#8221;
Const cdoSendUserName = “http://schemas.microsoft.com/cdo/configuration/sendusername&#8221;
Const cdoSendPassword = “http://schemas.microsoft.com/cdo/configuration/sendpassword&#8221;
Const cdoSendUsingPickup = 1 ‘Use the local IIS-SMTP service for delivery.
Const cdoSendUsingPort = 2 ‘Use a remote SMTP server for delivery.

Set oMessage = WScript.CreateObject(“CDO.Message”)
Set oConfiguration = WScript.CreateObject(“CDO.Configuration”)

Set cFields = oConfiguration.Fields
cFields.Item(cdoSendUsingMethod) = cdoSendUsingPort ‘cdoSendUsingPort = Send to remote SMTP server (2), cdoSendUsingPickup = Send using local SMTP service (1).
cFields.Item(cdoSMTPServer) = sSmtpServer ‘IP address or DNS name of the remote SMTP server.
cFields.Item(cdoSMTPServerPort) = 25
cFields.Item(cdoSMTPConnectionTimeout) = 30 ‘Timeout in seconds for connection.

If LCase(sUsername) = “ntlm” Then
cFields.Item(cdoSMTPAuthenticate) = 2 ‘2 = NTLM authentication. NTLM uses the credentials of the WSH process itself.
ElseIf sUsername <> “” Then
cFields.Item(cdoSMTPAuthenticate) = 1 ‘1 = Basic authentication.
cFields.Item(cdoSendUserName) = sUsername ‘Username for Basic authentication, ignored for anonymous or NTLM authentication.
cFields.Item(cdoSendPassword) = sPassword ‘Password for Basic authentication, ignored for anonymous or NTLM authentication.
Else
cFields.Item(cdoSMTPAuthenticate) = 0 ‘0 = Anonymous authentication.
End If

cFields.Update ‘Save data so far.
Set oMessage.Configuration = oConfiguration

oMessage.To = sTo
oMessage.CC = sCC
oMessage.BCC = sBCC
oMessage.From = sFrom
oMessage.Subject = sSubject
oMessage.TextBody = sBody

If sAttach <> “” Then
aFiles = Split(sAttach,”;”)
For Each sFile In aFiles
oMessage.AddAttachment sFile
Next
End If

oMessage.Send

If Err.Number = 0 Then
CDOSYS_Send_Email = True
Else
CDOSYS_Send_Email = False
End If

Set oMessage = Nothing
Set oFields = Nothing
Set oConfiguration = Nothing
End Function

‘END OF SCRIPT *********************************************************************

______________________________________________________________________________________

The above variables seem to translate to these:

Arguments: sSmtpServer = IP address or FQDN of out-going SMTP server.
‘ sUsername = (May be blank.) Username for Basic authentication; set blank for Anonymous; set ntlm for NTLM.
‘ sPassword = (May be blank.) Password for Basic authentication; ignored for Anonymous and NTLM.
‘ sTo = Recipient. Semicolon-delimited for multiple addresses.
‘ sCC = (May be blank.) CC recipient. Semicolon-delimited for multiple addresses.
‘ sBCC = (May be blank.) Blind CC recipient. Semicolon-delimited for multiple addresses.
‘ sFrom = Single e-mail address for sender.
‘ sSubject = (May be blank.) Subject line.
‘ sBody = (May be blank.) Body of message.
‘ sAttach = (May be blank.) Full path to local file to attach. Semicolon-delimited for multiple addresses.

if you are having difficulty with sending the email, ensure the server/desktop you run this script from can telnet to the exchange/smtp server on port 25. to test this use:

telnet <smtp address> 25

HELO

Below is a working excerpt of my script:

CDOSYS_Send_Email(“172.26.11.76″,””,””,”Andrew.Morgan@o2.com”,”SpaceChecks for DailyChecklist”,”E:\NT Infrastructure\Daily Checks\Dublin\SpaceResults\” & strMonth & “-” & strDay & “-” & strYear & “.xls”)

Happy coding =)

Categories: Scripting, VB Scripting Tags: ,
Follow

Get every new post delivered to your Inbox.

Join 2,587 other followers