Script to find and email files in a directory

I needed to write a batch file that would email some files, and could be run as a scheduled task.  I chose to use Blat as my email program, you can download it for free from SourceForge.

The batch file requirements were: 

1.  Had to email all the .xls files in one directory from the current date.  Luckily for me the date was in the file name, so I just had to find all files in the format *MMDDYY*.xls
 
2.  The emails had to be sent “To” some users, then “CC”d to others
 
3.  I could only use free software (no shareware) but could use our internal smtp replay server to send the mail through.
 
4. The Scheduled task that executes the script must run on a Windows 2003 server.
 
The following script is what I came up with.  I’ll go through it line by line, then post the entire thing at the end.
 
****************************************************
 
Here I’m getting the current date in the month month day day year year format, and saving it to a variable named search.  This can obviously be changed to meet your particular need
REM set search date variable in MMDDYY format for file search
REM set search to month-date-year for date formatting
for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set search=%%j%%k%%l
 
Adding a blank line to the log file ReportLog.txt since I like my logs nice and neat. Makes them easy to read.
echo. >> ReportLog.txt
 
Writing the date and time the script starts to the log file ReportLog.txt
echo %date% %time% Starting script >> ReportLog.txt
 
Using the variable search, which contains the current date, I’m getting a list of all .xls files from today and saving it in FileNames.txt
dir /b “\\server\share\%search%*.xls” > FileNames.txt
 
If there’s a problem retrieving the names of the files, write a message to the log file
IF ERRORLEVEL 1 (echo %date% %time%  problem with retrieving file names >> ReportLog.txt)
 
Using find /c to count how many files are in filenames.txt, and saving that number in the variable NUMFILES
for /f “tokens=3″ %%i in (‘find /v /c “SomeStringNotToBeFound” filenames.txt’) do set NUMFILES=%%i
 
If the variable NumFiles equals zero, there were no files found for today.  Skip to ZEROFILES to send an email alert
If %NUMFILES% EQU 0 GOTO ZEROFILES
 
If NUMFILES isn’t zero, then files exist that need to be emailed.  There may be more than one file, so we’re going to use a loop to process all the files listed in FileNames.txt
For /F “tokens=1-2* delims=” %%B IN (FileNames.txt) DO (
 
write the name of the file to the log file
echo file to email is %%B >> ReportLog.txt
 
Use blat.exe to send the email to addresses specified in tolist.txt
Use blat.exe to send emails as CC to addresses specified in cclist.txt
Attached file is listed at the end of the command
This command is all one line, it may wrap on the page
blat.exe -tf tolist.txt -cf cclist.txt -subject “Report %%B” -body “Here is the current report.” -server smtp.yourdomain.com -f sender@yourdomain.com -attach “\\server\share\%%B”
)
 
We’re done emailing the files, so skip over to WRITELOG
goto WRITELOG
 
Zero files were found, so we need to send an email alert
:ZEROFILES
 
write the name of the file to the log file, along with the date and time
echo %date% %time%  problem with retrieving number of current files >> ReportLog.txt
 
Use blat.exe to send an email alert to addresses specified in problist.txt
This command is all one line, it may wrap on the page
blat.exe -tf problist.txt -subject “Problem with Report” -body “Problem sending the current report.” -server smtp.yourdomain.com -f sender@yourdomain.com
 
Writing the date and time the script starts to the log file ReportLog.txt
:WRITELOG
echo %date% %time% Ending Script >> ReportLog.txt
 
****************************************************
 
The script in it’s entirely:
 
REM set search date variable in MMDDYY format for file search
REM set search to month-date-year for date formatting
for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set search=%%j%%k%%l
 
 
REM format ReportLog.txt
echo. >> ReportLog.txt
echo %date% %time% Starting script >> ReportLog.txt
 
 
REM Get the names of all log files for specified date, save to FileNames.txt
dir /b “\\server\share\%search%*.xls” > FileNames.txt
IF ERRORLEVEL 1 (echo %date% %time%  problem with retrieving file names >> ReportLog.txt)
 
REM Count how many files are in filenames.txt, put into %NUMFILES%
for /f “tokens=3″ %%i in (‘find /v /c “SomeStringNotToBeFound” filenames.txt’) do set NUMFILES=%%i
 
If %NUMFILES% EQU 0 GOTO ZEROFILES
 
REM loop when more than one file to be emailed
For /F “tokens=1-2* delims=” %%B IN (FileNames.txt) DO (
echo file to email is %%B >> ReportLog.txt
blat.exe -tf tolist.txt -cf cclist.txt -subject “Report %%B” -body “Here is the current report.” -server smtp.yourdomain.com -f sender@yourdomain.com -attach “\\server\share\%%B”
)
goto WRITELOG
 
:ZEROFILES
echo %date% %time%  problem with retrieving number of current files >> ReportLog.txt
blat.exe -tf problist.txt -subject “Problem with Hold Report” -body “Problem emailing the current hold report.” -server smtp.yourdomain.com -f sender@yourdomain.com
 
:WRITELOG
REM format ReportLog.txt
echo %date% %time% Ending Script >> ReportLog.txt
 
****************************************************

Howto: Disable a NIC when running Sysprep

Disabling a network card when running sysprepping a Windows machine is easy.  Two things need to happen: 

1.  Add the following command to the [GuiRunOnce] section of your sysprep.inf file
 
Command0=”C:\temp\disablenic.cmd”
 
2.  On the machine you are sysprepping, create a C:\temp\disablenic.cmd file that contains the following:
 
netsh interface set interface “Local Area Connection 2″ DISABLED
 
Change the name of the interface you want disabled as needed.  To determine the names of all network interfaces on a system, run the following command:
 
netsh interface show interface
 
Proceed with syspreping as normal. When the machine boots up, the specified network interface(s) will be disabled.

Howto automatically change the CD-ROM drive letter after running sysprep

I’m finalizing a Windows 2003 R2 build that will become our gold image, which will be the source of all new server deployments within our organization.  One challenge I had to overcome was getting the CD-ROM/DVD drive to be set to drive Z: after the syspreped image is cloned and booted.

Many people are familiar with changing drive letters within the Device Management tool aka devmgmt.msc.  I needed to automate this task so the CD-ROM drive, which shows up as drive D on my image after running sysprep, would be automatically set to drive Z.

To accomplish this, I needed three things:

  1. An entry in the [GuiRunOnce] section of my sysprep.inf file that calls a batch file after booting up the sysprep’ed image for the first time. 
  2. The batch file mentioned in step 1, changeletter.cmd runs diskpart.exe, with the parameters supplied in drives.txt
  3. The drives.txt file, which details the diskpart.exe commands that change the CD-ROM’s drive letter from drive D to drive Z.

The applicable portion on my sysprep.inf file:

[GuiRunOnce]
Command0=”C:\changeletter.cmd”

My changeletter.cmd file:

diskpart /s c:\drives.txt

My drives.txt file:

select disk 0
select volume d
assign letter z noerr

Put all these pieces together, and your CD/DVD drive should be changed to drive letter Z after booting up the sysprep’ed image.  Note that in the [GuiRunOnce] section of the sysprep.inf file, the part to the left of the equals sign is Command0, as is Command zero.  If you wanted to run additional scripts, the next would be Command1, followed by Command2, etc.

If you’re curious about diskpart.exe, check out the details on syntax in KB300415.

Find Windows system uptime from the command line

Here’s a quick and easy way of checking how long a Windows server or workstation has been up, via the command line.  It pipes the results of Net Statistics Workstation into find.  Run the following from a command prompt:

net statistics workstation | find /i “statistics since”

The results will look like

Statistics since 8/12/2009 11:08 PM

Which shows the machine has been up since 11:08pm on August 12, 2009.

Fix: Userenv event 1521 and Userenv 1511 errors in Windows Server 2003 Application log

The following events were listed in the Windows 2003 event log when one of our second level help desk staff connected to the server console via RDP:

Event 1521 Source Userenv 

Windows cannot locate the server copy of your roaming profile and is attempting to log you on with your local profile. Changes to the profile will not be copied to the server when you logoff. Possible causes of this error include network problems or insufficient security rights. If this problem persists, contact your network administrator.   

DETAIL – The network name cannot be found.
 
Event 1511 Source Userenv
 
Windows cannot find the local profile and is logging you on with a temporary profile. Changes you make to this profile will be lost when you log off.
 
The solution, as outlined in KB941339, is to delete the SID entries for the source user account from the following registry subkey:
 
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
 
To determine the user’s SID, save the script found at
as a .vbs file.  
 
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2″)
Set objAccount = objWMIService.Get _
    (“Win32_UserAccount.Name=’kenmyer’,Domain=’fabrikam’”)
Wscript.Echo objAccount.SID
 
Change the Win32_UserAccount.Name=’kenmyer’,Domain=’fabrikam
 
from kenmeyer and fabrikam to the user account in question and your domain name.

I saved the script as getsid.vbs.  Open a command prompt, and execute the script using cscript:

cscript.exe getsid.vbs

The script will display the SID associated with the user account specified.  Delete this from:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

to correct the Userenv errors.

Capturing Virtual Machine Blue Screens via Powershell

Eric Sloof at ntpro.nl has a nice post on how to capture screen shots of virtual machine blue screens using Powershell.  You can find the code here.

He then incorporated a Powershell script from Carter Shanklin that pushes a screenshot through Microsoft Office Document Imaging Library (MODI), OCR software found in Office 2003+, to extract the text from the image.

Very nice. Definitely go download his application, the Virtual Machine Blue Screen Detector.  Detailed post here.

The final product, which reads out the BSOD can be found here.

Fix for ConsoleOne.exe error – The procedure entry point WpSUDataLength could not be located in the dynamic link library gwenv1.dll

Some of our administrators received the following messages when launching ConsoleOne 1.3.6f after updating to Groupwise version 7.0.3 snapins from version 6.5.5:

The procedure entry point WpSUDataLength could not be located in the dynamic link library gwenv1.dll.   

and

An error occuring during ConsoleOne Startup.  ERROR:  java.lang.UnsatisfiedLinkError: ititIDs

 

The fix was to  copy gwenv1.dll from the Groupwise 7.0.3 client’s software distribution directory’s \win32\system32 directory to the local administrator’s c:\novell\consoleone\1.2\bin directory.
 
You must ensure gwenv1.dll file is version 7.0.3.  Also check c:\windows\system32 for existence of prior version of gwenv1.dll.

 

You can read about other gwenv1.dll issues I encountered that deal with the Groupwise client and Blackberry Enterprise Server (BES).

.cmd script that determines if a directory is empty and sends email notification of status

I have an email archiving application on a Windows 2003 server that requires a lot more manual intervention than I prefer.  As the application moves the mail messages throughout it’s various directory queues, sometimes it experiences an event that causes processing to halt.  This results in a particular directory filling up until an administrator manually clears out the queues.  The last time this happened we had 75,000 files, 10GB worth of messages we had to remove and recover by hand.

This server is only monitored by SolarWinds, which has the ability to monitor a volume, by not a particular directory.  In the past the only time we knew there was a problem was when the volume filled up with backlogged messages.
 
I decided to implement a system that would periodically poll the queue directory and would send email notifications that the queue was empty, or that files were backlogged.  Because of our strict change control procedures I am unable to load any freeware monitoring solutions, so I needed to create my own via scripts.  Luckily V-Mailer, a freeware SMTP batch mailer is already approved for usage on our internal network, so I was able to utilize this software.
 
The script shown below, chksize.cmd, utilizes the built in DIR command to determine if the server’s e:\msw\internet\pending directory is empty.  
REM delete existing mailarch1file.txt file
if exist mailarch1file.txt del mailarch1file.txt

REM determine if files are queued using dir and output results to mailarch1file.txt
e:\msw\internet\pending /A-D /B  mailarch1file.txt

REM If mailarch1file.txt is empty (EQU 0), files are not queued
For %%R in (mailarch1file.txt) do if %%~zR EQU 0 goto nofiles

REM delete existing results.txt file if exist results.txt del results.txt
REM combine message file mailarch1fail.txt with results of mailarch1file.txt into results.txt
copy /B mailarch1fail.txt + mailarch1file.txt results.txt

REM email notification that files are queued in directory e:\msw\internet\pending
REM notification to technical support using vmailer.exe that mailarch1 may be failing
vmailer.exe results.txt 172.17.61.83 mailadmin@domain.corp mailarch1@domain.corp
goto exitscript

:nofiles
REM email notification no files are queued in directory
REM notification to technical support using vmailer.exe that mailarch1 is okay
vmailer.exe mailarch1isok.txt 172.17.61.83 mailadmin@domain.corp mailarch1@domain.corp

:exitscript
REM exit script
exit

If the DIR command’s output file mailarch1file.txt is empty, a notification text file mailarchisok.txt is emailed to myself that states the server is okay.   

If the output file is not empty, the results of mailarch1file.txt is concatenated with a text file mailarchfail.txt that states the server may be failing.  Because V-Mailer does not really handle attachments, I have to combine the results text file with the notification text file into results.txt, which is an ugly but effective method.
Here is my mailarch1fail.txt file, which is pre-formatted for use with V-Mailer
To: mailadmin@domain.corp
From: mailarch1@domain.corp
Subject: mailarch1 may be failing

mailarch1 DOES have messages queued in the E:\MSW\Internet\PENDING directory
Here is my mailarch1isok.txt file, which is pre-formatted for use with V-Mailer
To: mailadmin@domain.corp
From: mailarch1@domain.corp
Subject: mailarch1 is okay

mailarch1 DOES NOT have messages queued in the E:\MSW\Internet\PENDING directory

Finally, I used the Windows schtasks command line utiliy to run c:\chksize.cmd daily at 4pm.  The task, named chksize4pm runs as as the service@domain.corp user with a password of 3rv1c3. 
This is a single command that may have wrapped in your browser.
schtasks /create /S \\mailarch1 /U service@domain.corp /P $3rv1c3 /SC Daily /TN chksize4pm /TR c:\chksize.cmd /ST 16:00

Script to backup Groupwise configuration files on Netware Part I

I performed a Groupwise 6.5 to 7.0.3 upgrade this weekend on the domain and post office servers, and wrote a quick script to backup the agent configuration files.  It’s not a pretty script, but I wrote it in about 10 minutes and it worked on all my Netware servers.  I call this script part I since it only deals with files affected by my upgrade, which are all located on the sys volume.  I upgrade the gateways in two weeks, so I’ll backup the configuration files in the domain directories then.

You need to set SERVERNAME, SERVERVOL, BKUPLOC and BKUPDIR. If I’ve missed any files, please let me know and I’ll add them to the list.

@echo off
REM script to backup Groupwise configuration files from Netware server
REM replace SERVERNAME with the name of your Netware/Groupwise server
SET SERVERNAME=\\grpwise4
SET SYSVOL=sys
REM replace SERVERVOL with the name of the volume to write the backup files to
SET SERVERVOL=vol1
REM SERVERPATH is combination of server and volume name in \\server\vol\ format
SET SERVERPATH=%SERVERNAME%\%SERVERVOL%
REM SYSVOLPATH is combination of server and volume name in \\server\sys\ format
SET SYSVOLPATH=%SERVERNAME%\%SYSVOL%
REM APACHEAPTH is sys:\apache2 Apache2 web server directory
SET APACHEPATH=%SERVERNAME%\%SYSVOL%\Apache2
REM NOVELLPATH is sys:\novell directory
SET NOVELLPATH=%SERVERNAME%\%SYSVOL%\Novell
REM TOMCATPATH is sys:\tomcat\4 directory
SET TOMCATPATH=%SERVERNAME%\%SYSVOL%\tomcat\4
REM BKUPLOC is the directory to save backup files to
REM this script has no error checking, so the directory's existance will probably matter
SET BKUPLOC=gw65bkup
REM BKUPDIR is the full path to the backup directory
SET BKUPDIR=%serverpath%\%bkuploc%
REM Create the backup directory
md %bkupdir%
REM copy sys:\system\ config files
md %bkupdir%\system
copy %sysvolpath%\system\*.mta %bkupdir%\system
copy %sysvolpath%\system\*.poa %bkupdir%\system
copy %sysvolpath%\system\*.waa %bkupdir%\system
copy %sysvolpath%\system\*.cfg %bkupdir%\system
copy %sysvolpath%\system\*.ncf %bkupdir%\system
copy %sysvolpath%\system\*.xml %bkupdir%\system
copy %sysvolpath%\system\*.bin %bkupdir%\system
copy %sysvolpath%\system\autoexec.ncf %bkupdir%\system
REM copy important Apache files
md %bkupdir%\apache2\conf
copy %apachepath%\conf\*.* %bkupdir%\apache2\conf
REM copy important Tomcat files
md %bkupdir%\tomcat\4\conf
copy %tomcatpath%\conf\*.* %bkupdir%\tomcat\4\conf
REM copy important Webaccess files
md %bkupdir%\novell\webaccess\conf
copy %novellpath%\webaccess\*.* %bkupdir%\novell\webaccess
REM copy important Groupwise NLMs from sys:\system
copy %sysvolpath%\system\dbcopy.nlm %bkupdir%\system
copy %sysvolpath%\system\ex*.nlm %bkupdir%\system
copy %sysvolpath%\system\gw*.nlm %bkupdir%\system
copy %sysvolpath%\system\ldap*.nlm %bkupdir%\system
copy %sysvolpath%\system\tsa*.nlm %bkupdir%\system
copy %sysvolpath%\system\scc*.nlm %bkupdir%\system
copy %sysvolpath%\system\vs*.nlm %bkupdir%\system
copy %sysvolpath%\system\wvc*.nlm %bkupdir%\system
copy %sysvolpath%\system\xg*.nlm %bkupdir%\system
Save the script as gwbkup1.bat and run it from your Windows workstation.

Simple script to backup Groupwise 7 configuration files on SLES Linux

I’m about to upgrade our Groupwise infrastructure, and I wanted to write a simple script that would backup and archive important Groupwise configuration files.  I’m not proficient at scripting, but here’s what I came up with.  Please recommend additional files to include if I have overlooked anything.

####################################
### Begin gwbkup.sh
### script to backup Groupwise configuration files
### /home/backups/gw7cfg is the directory to backup files to
### backup Groupwise agent configuration files
cp /opt/novell/groupwise/agents/share/* /home/backups/gw7cfg/
cp /etc/opt/novell/groupwise/gwha.conf /home/backups/gw7cfg/
### backup Apache web server configuration files
cp /etc/apache2/httpd.conf /home/backups/gw7cfg/
cp /etc/apache2/conf.d/gw* /home/backups/gw7cfg/
### Backup WebAccess configuration files
cp /opt/novell/groupwise/webaccess/webacc.cfg /home/backups/gw7cfg/
cp /opt/novell/groupwise/webaccess/commgr.cfg /home/backups/gw7cfg/commgr.cfg.webacc
cp /opt/novell/groupwise/webaccess/spellchk.cfg /home/backups/gw7cfg/
### Backup WebPublisher configuration files
cp /opt/novell/groupwise/webpublisher/webpub.cfg /home/backups/gw7cfg/
cp /opt/novell/groupwise/webpublisher/commgr.cfg /home/backups/gw7cfg/commgr.cfg.webpub
cp /opt/novell/groupwise/webaccess/ldap.cfg /home/backups/gw7cfg/
### Backup Tomcat configuration files
cp -r /etc/tomcat5/base/* /home/backups/gw7cfg/
### backup gwia files specific to each gateway
### may have more than one gwia per Groupwise system, so append gateway
### name to end of file
cp /mail/gwiado/wpgate/gwia703/exepath.cfg /home/backups/gw7cfg/exepath.cfg.gwia703
cp /mail/gwiado/wpgate/gwia703/gwac.db /home/backups/gw7cfg/gwac.db.gwia703
cp /mail/gwiado/wpgate/gwia703/gwauth.cfg /home/backups/gw7cfg/gwauth.cfg.gwia703
cp /mail/gwiado/wpgate/gwia703/mimetype.cfg /home/backups/gw7cfg/mimetype.cfg.gwia703
### backup webaccess files specific to each gateway
### may have more than one gwia per Groupwise system, so append gateway
### name to end of file
cp /mail/webdo/wpgate/webac703/comint.cfg /home/backups/gw7cfg/comint.cfg.webac703
cp /mail/webdo/wpgate/webac703/commgr.cfg /home/backups/gw7cfg/commgr.cfg.webac703
cp /mail/webdo/wpgate/webac703/gwac.db /home/backups/gw7cfg/gwac.db.webac703
cp /mail/webdo/wpgate/webac703/mimetype.cfg /home/backups/gw7cfg/mimetype.cfg.webac703

### Backup Groupwise monitor files
cp /opt/novell/groupwise/gwmonitor/gwmonitor.cfg /home/backups/gw7cfg/gwmonitor.cfg
cp /opt/novell/groupwise/gwmonitor/default/gwmonitor.cfg /home/backups/gw7cfg/gwmonitor.cfg.default
cp /opt/novell/groupwise/gwmonitor/default/gwmonitor.xml /home/backups/gw7cfg/gwmonitor.xml
### tar configuration files and label archive with today's date
tar -pcvzf /home/backups/$(date +%m-%d-%Y).tar.gz /home/backups/gw7cfg/
### Remove old configuration files rm -r /home/backups/gw7cfg/* 
### End gwbkup.sh ####################################