Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Wednesday, May 19, 2010

Perl - Debugging Scripts

Issue: Using perl script and you need help finding a problem.

Quick: Use the built in debugger tools

Visual/Learning:
perl -w scriptName    This will give you warnings about the script if there are any.
perl -d scriptName     Will put you in debug mode
w                               Lists code.  another w lists the next lines of code and so on
.                                 Resets code listing to current execution point
s                                Steps through code
n                               Steps through code and treats subroutines as a single step
p $variableName      Prints value of the variable
x %data                   Prints array data
x \%data                  Prints better looking array contents
b 20                         Set breakpoint at 20
L                           List all break points
d or D                      Deletes breakpoints
c                              Continues to break point
c 12                        Continues to line 12

Some links to debugger tutorials
Short Tutorial
Longer Tutorial

Thursday, May 13, 2010

Regular Expressions Searching

Issue: a "Regular Expression" is a way of building complex searches but they are also confusing to use.  Below are some search expressions that I have found useful.  And links to some tutorials.

Quick/Visual/Learning:

/btarget/b  - will find the whole word target in a text string (works with start/end line, spaces, special char
 examples -- target @target -target-

(^| +)target( +$) - will find target at start/end of line or with spaces before/after. Will not match special characters.
example --    target

Here is a tutorial on regular expression searches

Wednesday, April 1, 2009

Posting Code to Blog

Issue: You want to post some code to your blog but the it gets all messed up.


Quick/Visual/Learning:
Here are some tools I found to transform your code so it displays correctly;

from Force Monkey site

---Text From Site---
Postable
Then, you simple wrap <code></code> tags around your converted "HTML friendly" text, and wala! It's viewable in a Google Blog post (and can be copied by your readers!):


Here's another site that goes one better, giving you an option to transform the text into standard HTML code with colorized tags:

---End Text From Site---

Saturday, January 31, 2009

Perl Script To Identify If Office 2003 SP3 Is Installed

Issue:
Need to identify which machines have Office 2003 SP3 installed.

Quick: Microsoft Notes indicate files that have changed. Check size of one of the updated files.

Visual/Learning:
Pick one of the updated files to use an indicator that SP3 is loaded. Search for the file to see where it is located on one of the workstations.

The following line of Perl code executes the dos dir command to list one of the files updated by SP3. If I know what the new file size should be, I can do a comparison to check for the service pack. In this case the file size should be 103,256 bytes for the SP3 version of the MCPS.DLL file.

my @results = ( `dir \\\\$workstation\\"c\$\\Program Files\\Microsoft Office\\OFFICE11\\MCPS.DLL"` );

Friday, December 12, 2008

Install Using Logon Script

Issue:
Automation of software install or upgrade using a logon batch file.

Quick:
Logon script calls batch file to remove old application and install new one.

Visual/Learning:
Looking at one of the user accounts you can see that the Logon script is pointed to a file called patches.bat

Patches.bat is located in the NETLOGON directory of the domain controller.




Patches.bat calls other modules to install and update software. In this case we will look at the module_mapcad.bat



This batch file is also in the NETLOGON directory




Below bat file listing in small text with comments in large text.
------------------

@echo off
rem remove, install or upgrade MapCAD for Land Desktop

This application depends on AutoCAD so the scripts checks to see if the acad.exe file exists. If it does the program jump to the start label otherwise it goes to the end label.

if exist "C:\Program Files\AutoCAD Civil 3D Land Desktop Companion 2008\acad.exe" goto start
goto end

Here is the start label. We next check for the existence of a text file. This file gets written to the drive if a previous install was successful. If it exists we jump to the end, otherwise we continue with the install.

:start
if exist "C:\Program Files\MapCAD\MapCAD-2008v12.05.txt" goto end

This uninstalls a previous version of the software. You can find the right key to uninstall by searching the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall for the name of the application you want to remove. Uninstall using the key name (long hexidecimal number). The -quite part of this does a silent uninstall

rem uninstall the previous version
msiexec.exe /Uninstall {B4AB209A-0493-4E11-BB16-72418BBFA9BF} -quiet



Here the new version is installed. The install files need to be place somewhere where then can be installed. In this case on the logon server.

rem install the new version
call "%logonserver%/netlogon/Mapcad/MapCAD_2008v12.05/MapCAD Suite 2008-LD v12.05 Setup.msi" /quiet /passive

After the install a text file is written as an indication to the script that it has already completed and does not need to run again on the current workstation. Also a tracking file is updated to monitor which workstations have run the script.

echo %computername% %date% %time% "2008v12.05 update" > "C:\Program Files\MapCAD\MapCAD-2008v12.05.txt"
echo 2008v12.05 update %computername% %date% %time% %username% >> \\FileSvr1\Batch-tracking\MapCAD.txt

:end

Tuesday, September 9, 2008

Surveyor PC Power Management 8 - Wake-On-Wan

Issue:
Wake computers in order to perform nightly backup of local pst mail files.

Quick:
Gather MAC address using ping and arp via day script, Use backup script and mc-wol.exe program to wake computers at night.

Visual/Learning:
Since currently Verdiem server can't take commands via script, I have to find a way to do add wake-up code to my backup script.

At the following link http://www.matcode.com/wol.htm I found a small program that works quite well (mc-wol.exe ) to wake the computers using wake-on-lan magic packets. This program requires me to pass it the Media Access Control Address (MAC Address) for the network card of the computer I want to wake.

I do not have a list of MAC addresses for all my computers and MAC information changes as computers and network cards are replaced. I can't pull the MAC from the computers at backup run-time because they will have all suspended. So I need a script to run in the day-time that can generate and maintain an updated history list ('MacHist.txt').

I created a script to do this and I have it run on a schedule during the day when machines are be used. I ping the machine to get the IP address and then "arp -a" brings up the ARP table so I can find the MAC Address associated with that IP.


The script is pretty long so I am not going to try and post it here (plus the formating gets all messed up when I do) but these are the basic steps:

-For each computer name in Active Directory
-Try to ping computer
--If it pings get the IP address from the ping return
--Check ARP table for the IP address and get MAC Address
---If the computer name is in MacHist.txt update the IP and MAC data in that record
---else add a new record to MacHist.txt
-Check all computers from AD listing
-Save the updated MacHist.txt file

The data in my MacHist.txt file looks like this:

ComputerName:IpAddress:MacAddress
ComputerName1:192.168.1.1:00-11-f3-2f-2b-a1
ComputerName2:192.168.1.2:00-15-f2-e5-18-23
ComputerName3... AND SO ON


I run this script from a computer on the same subnet as my workstations because the ARP table only shows data for machines on the local subnet.

Then my backup script reads the MacHist.txt file at night and wakes the workstations one at a time for backup (using the mc-wol.exe program). Then the script tries to ping the workstation using the IP address from the MacHist.txt file. If the script sees pings return from the computer it starts the backup process or else moves on to the next computer. When the backup is done, the computer network and disk activity stops, and the computer goes back to sleep. They go back to sleep because my night power profile is set to suspend after 15 minutes of idle time.

So all the machines sleep till it is their turn to backup, and sleep again when done.

Some questions have come up about how to allow for Windows updates which usually happen at 3 AM on Patch Tuesday (second Tuesday of the month). This can be fixed in Verdiem by scheduling an action to Wake all clients at 2:59 AM on Tuesday. If there are any updates the disk activity will keep the machines awake until the updates are done.

Surveyer PC Power Management 9

Thursday, June 26, 2008

Execute a command on a remote computer

Sysinternals Suite has a lot of great free utilities.

Find them here: http://technet.microsoft.com/en-us/sysinternals/0e18b180-9b7a-4c49-8120-c47c5a693683.aspx

I had a need to execute a command on a different networked computer with a script and I found just the tool I needed here.

PsExec file://MachineName/ -c MyFile.exe

This command will copy the MyFile.exe program to the remote computer and execute it on the remote computer.

Pretty cool!

Make for Windows Perl Scripts

Perl is a great scripting language for automating tasks. But unlike Linux, Windows lacks a make utility for compiling PERL modules.

You can download Nmake15.exe from http://support.microsoft.com/default.aspx?scid=kb;en-us;Q132084

I found help on compling perl modules using nmake at http://johnbokma.com/perl/make-for-windows.html

Copy both the NMAKE.EXE and the NMAKE.ERR file to your Perl bin directory, normally C:\Perl\bin.

The uncompiled perl module should come with a file called Makefile.pl
Just open a command prompt in the directory where you have the uncompiled module and the Makefile.pl file and run the following commands:

1. run the command: perl Makefile.pl
2. run the command: nmake
3. run the command: nmake test
4. run the command: nmake install

If you get an error like "nmake is not recognized as an internal or external command", make sure you have the C:\perl\bin directory in your path.