.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 ####################################

Powershell script to change from static to dynamic IP addressing


I’ve been working on validating an email server migration plan which has required utilizing test servers isolated from the production network.  Because they are offnet, I have to frequently switch my laptop from the production to test networks, which involves reconfiguring my network card from dynamic to static IP addresses.

I could use Windows XP’s alternate IP addressing capability to change IP addresses, but I find it takes several minutes for XP to make the automatic changeover.  I wanted to create a configuration script that would make the desired changes so that I could save shortcuts to those scripts on my desktop, which would allow for quick NIC setting modifications.

I originally tried to use the netsh command to specify NIC settings, but despite all the example syntax I found online, I was only able to get the static to dynamic changes to work, not the dynamic to static.

I finally decided to use Powershell to make these changes.  I found the following scripts at PowerShell Pro and modified them ever so slightly for my own use.

### set static IP addressing – save as setstatic.ps1
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration `
| where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
  $NIC.EnableStatic(“192.168.1.5″, “255.255.255.0″)
  $NIC.SetGateways(“192.168.1.254″)
  $DNSServers = “198.168.1.1″,”198.168.1.1″
  $NIC.SetDNSServerSearchOrder($DNSServers)
  $NIC.SetDynamicDNSRegistration(“FALSE”)
}
###

### set dynamic addressing – save as setdynamic.ps1
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration `
| where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
 $NIC.EnableDHCP()
 $NIC.SetDNSServerSearchOrder()
}
###

I then created shortcuts to the two .ps1 files using the following targets:

%windir%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\setdynamic.ps1

%windir%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\setstatic.ps1

I saved each shortcut to my desktop, and now I can switch IP addresses in about 15 seconds.

Script to securely backup and export IIS 6.0 Configuration


Last week I posted a basic script that took advantage of file encryption to securely backup the iis metabase. Today’s script builds upon it by backing up the IIS configuration and exporting it using iiscnfg.vbs.

REM delete mapping for H: if it exists
net use h: /del

REM map H: to remote server share
net use h: \\RemoteServer\backup /user:RemoteServer\user password

REM replace YouriisServerName with the name of your IIS web server
SET SERVER=YouriisServerName

REM yymmdd will be the current date.
REM For example June 18 2008 will be in the format 080618
SET yymmdd=%date:~12,2%%date:~4,2%%date:~7,2%

REM replace YouriiSserverPassword with the password of an
REM administrative user on the IIS box
SET PASSWORD=YouriiSserverPassword

REM replace User with an administrative user on IIS server
SET USER=Administrator

REM replace DECRYPTPWD with the password for decrypting IIS exported configuration
SET DECRYPTPWD=pwd

REM Saves configuration to C:\WINDOWS\system32\inetsrv\metabase.xml
iiscnfg.vbs /save /s %SERVER% /u %USER% /p %PASSWORD%

REM export IIS Configuration to date.metabase.xml.file
REM The following command should be on one line
iiscnfg.vbs /export /s %SERVER% /u %USER% /p %PASSWORD% /d %PASSWORD% /f H:\%SERVER%\%yymmdd%.metabase.xml /sp / /children

Now, for the secure part. We need to encrypt the bkupmeta.bat file so that not just anyone can read it’s contents. That would be a bad thing, since we have our password saved in the script.

To encrypt the bkupmeta.bat file:

In Windows Explorer right click on the bkupmeta.bat file
Select Properties
Select Advanced
Check the Encrypt Contents to Secure Data check box – OKApply
Select Encrypt the File Only

While you’re at it, double check the bkupmeta.bat’s ntfs permissions to make sure only the appropriate administrative user has access to the file. You should also verify the %windir%\system32\inetsrv\metaback\ directory has the appropriate permissions, even though it’s secured by default.

Finally, create a scheduled task that runs bkupmeta.bat whenever you’d like. Verify the backups are occuring by checking for the MD* and SC* files on the remote server.

This script was modified from one found in Chapter 9 of ”IIS 6.0 Administration Scripts, Tips, and Tricks”.

Script to securely backup IIS 6.0 Metabase


The following is a script to backup the IIS 6.0 Metabase.  Save it as bkupmeta.bat.

REM delete mapping for H: if it exists
net use h: /del

REM map H: to remote server share
net use h: \\RemoteServer\backup /user:RemoteServer\user password

REM replace YouriisServerName with the name of your IIS web server
SET SERVER=YouriisServerName

REM yymmdd will be the current date. 
REM For example  June 18 2008 will be in the format 080618
SET yymmdd=%date:~12,2%%date:~4,2%%date:~7,2%

REM replace YouriiSserverPassword with the password of an administrative
REM user on the IIS box
SET PASSWORD=YouriiSserverPassword

REM iisback.vbs generates the two metabase backup files and saves them
REM to the %windir%\system32\inetsrv\metaback directory
REM in the format yymmdd.MD0 and yymmdd.SC0
iisback.vbs /backup /b %yymmdd% /e %PASSWORD%

REM copy the metabase backup files to the remote server
REM /y tells xcopy to overwrite any files with the same name
REM on the remote server
Xcopy %windir%\system32\inetsrv\metaback\%yymmdd%.* h:\%SERVER% /y

Now, for the secure part.  We need to encrypt the bkupmeta.bat file so that not just anyone can read it’s contents.  That would be a bad thing, since we have our password saved in the script.

To encrypt the bkupmeta.bat file:

In Windows Explorer right click on the bkupmeta.bat file
Select Properties
Select Advanced
Check the Encrypt Contents to Secure Data check box – OKApply
Select Encrypt the File Only

While you’re at it, double check the bkupmeta.bat‘s ntfs permissions to make sure only the appropriate administrative user has access to the file.  You should also verify the %windir%\system32\inetsrv\metaback\ directory has the appropriate permissions, even though it’s secured by default.

Finally, create a scheduled task that runs bkupmeta.bat whenever you’d like.  Verify the backups are occuring by checking for the MD* and SC* files on the remote server.

This script was modified from one found in Chapter 9 of “IIS 6.0 Administration Scripts, Tips, and Tricks”.

Kiwi CatTools: Schedule automatic backups (and perform other activities) on your network devices


Kiwi CatTools is a free (up to five devices) customizable utility that help network administrators automate configuration backups of their network devices such as routers and switches. It provides email notification and compare reports, highlighting configuration changes. Some of the features of CatTools includes:

  • Instant or scheduled device configuration backups where any differences can be instantly emailed to you.
  • Send CLI commands via Telnet or SSH to many devices at once.
  • Change device configuration at scheduled times.
  • Change all of your network device passwords at once.
  • Generate various device reports such as Port, MAC, ARP and Version.
  • Compare the startup and running configuration of devices.

All versions of CatTools have the same functionality. The extent of that functionality however is limited by the license you are running.


Freeware   Edition
Engineer   Edition
Professional   Edition
Enterprise Edition
Number of Devices in database
5
20
500
Unlimited
Number of Activities in database
5
20
50
Unlimited
Simultaneous TFTP sessions
2
 10
 20
100
Simultaneous Device connections
1
 5
 10
30

Check out the following screenshots of the product. My only disappointment with the product was that there was no Sonicwall firewall preconfigured template. Luckily, there’s detailed instructions on how to define your own devices and activities. The premise is if your device supports Telnet, SSH1, SSH1.5 (Cisco), or SSH2 connectivity, you should be able to script automatic backups and perform other activities on it.

See the list of supported devices and an overview of the pre-configured activities.

Script to add an Active Directory Domain User or Group to a Windows Local Group


This Technet Hey Scripting Guy column describes how to add a Domain Group to a Local Windows Group:

strComputer = “atl-ws-001″

Set objLocalGroup = GetObject(“WinNT://” & strComputer & “/TestGroup”)
Set objADGroup = GetObject(“WinNT://Fabrikam/Finance”)

objLocalGroup.Add(objADGroup.ADsPath)

where:

  • atl-ws-001 is the name of the local computer you wish to add the domain group to
  • TestGroup is the name of the local group
  • Finance is the name of the Fabrikam domain group

Similarly, this Hey Scripting Guy column provides a script for adding a domain user to the local administrators group:

strComputer = “atl-ws-01″
Set objGroup = GetObject(“WinNT://” & strComputer & “/Administrators”)
Set objUser = GetObject(“WinNT://fabrikam/kenmyer”)
objGroup.Add(objUser.ADsPath)

where:

  • atl-ws-001 is the name of the local computer you wish to add the domain user to
  • Administrators is the name of the local Administrators group
  • KenMeyer is the name of the Fabrikam domain user

Save your script with a .vbs file extension, i.e. adduser.vbs. From a command prompt, run cscript adduser.vbs to execute the script.

Howto: automatically remove files older than ‘x’ days, Part II


Earlier I had posted my .vbs script for deleting files that were more than ‘x’ days old.

I just ran across this script that accomplishes the same thing, using Windows 2003′s native forfiles comand (scroll down to step 4).

echo on

rem First Delete old SQL Backup Files

FORFILES /p C:\filename /s /m *.* /d -3 /c “CMD /C del /Q @FILE”

rem pause

Save the script with a .bat or .cmd extension and schedule it using the Windows Scheduled Tasks applet.

Howto: automatically remove files older than ‘x’ days


Tape backups have failed me too many times, so I now do my Windows backup to an external hard drive. One of the office staff is in charge of swapping out our backup drives and taking it offsite. I needed a solution that would remove old backups without user intervention so they wouldn’t have to worry about having enough space available for the backup to complete.

My hard drives can hold four backups, so here’s a very simple .vbs script that deletes files from I:\Backup Files that are more than three days old:

Dim Fso

Dim Directory

Dim Modified

Dim Files

Set Fso = CreateObject(“Scripting.FileSystemObject”)

Set Directory = Fso.GetFolder(“I:\Backup Files”)

Set Files = Directory.Files

For Each Modified in Files

If DateDiff(“D”, Modified.DateLastModified, Now) > 3 Then Modified.Delete

Next

Save this as filename.vbs

To execute this file, run:

cscript.exe filename.vbs

Thanks to Don for the original script I modified for my particular needs.

[updated 12-07-2007]

I just added another script that automatically removes files older than ‘x’ days which uses the forfiles command that is native to Windows.

[updated 01-01-2008]

Here’s another simple script from KWSupport.

[updated 01-02-2008]

This script from the Scripting Guy deletes files ‘X’ number of hours old. Looks like it could be modified easily to meet your needs.

Follow

Get every new post delivered to your Inbox.

Join 32 other followers