AE6382
Scripting in Windows
AE 6382
AE6382
Documentation
The most difficult aspect of scripting in Windows is finding the documentation.
Microsoft produces enormous quantities of documentation for its part – it is a matter of locating the correct document and interpreting it.
The documentation for non-Microsoft software can be difficult.
AE6382
MS Documentation
Local copies of some of the MS scripting documents are located at
http://www.ae.gatech.edu/classes/ae6382/documents/MS_Scripting/
The Script56.CHM file is best suited for on-line use and contains information on VBScript and Jscript languages. It also contains the documentation for Windows Scripting Host (WSH), the Script Runtime objects, and Windows Scripting Components.
AE6382
MS Documentation – script56.chm
AE6382
Windows Scripting Host
The Windows Scripting Host provides an environment for running scripts in Windows.
Provides a host for ActiveX scripting engines (VBScript, Jscript, and other 3rd party implementations)
The fundamental object is WScript and is pre-instantiated Properties
– Name, Path, StdIn, StdOut,StdErr Methods
– CreateObject, GetObject, Quit, Sleep
AE6382
Windows Scripting Host
The WshShell object makes it possible to run a program locally. Can control the programs input and output The WshScriptExec object is returned from the Exec method
and contains status information Properties
– CurrentDirectory Methods
– AppActivate, SendKeys, Exec, RegDelete, RegRead, RegWrite, PopUp
AE6382
Runtime Scripting
The Runtime Scripting component provides capabilities for VBScript and Jscript that are not otherwise available. They cannot be use when scripting a web page in IE.
The objects included in the Runtime Scripting are Dictionary (associative arrays) File System Object
– Create and delete files/folders– Read and write files
AE6382
MS Office Objects
The objects available for the Office programs (Excel in this case) are located on the Office CD in the form of .chm (MS Help files).
The file for Excel 10 is VBAXL10.CHM This file contains all the objects and their methods and
properties. The objects documented here can be used either by
VBA within Excel or accessed externally via automation of the Excel program.
For more information see http://www.ae.gatech.edu/classes/ae6382/documents/MS_scripting/Office10/
AE6382
Execl VBAXL10.CHM
AE6382
vbaxl10 – Worksheet Object
AE6382
vbaxl10 – Worksheet Properties
AE6382
vbaxl10 – Worksheet Methods
AE6382
Automation of Excel Example
An application that uses data stored in an Excel worksheet to generate a configuration file for use by a Linux dhcp server.
The Excel file contains IP address, MAC address, location, owner, and information about whether address is to be managed by dhcp.
A script file uses automation to access the fields in the Excel file then generates a Linux formatted text file.
AE6382
dhcp_sample.xls
AE6382
The script - generate_dhcp.pl
#!/usr/bin/perl## Windows version with automation access to excel#
use strict;
use Win32::OLE qw(in with);use Win32::OLE::Const 'Microsoft Excel';use Win32::OLE::Variant;use Win32::OLE::NLS qw(:LOCALE :DATE);
# Program dies on errors$Win32::OLE::Warn = 3;
# The file (spreadsheet) to accessmy $excel_file = 'e:\programming\ruby\dhcp_sample.xls';
AE6382
The script - generate_dhcp.pl
This script was written using ActiveState’s ActivePerl distribution.
It can be downloaded from http://www.activestate.com/
This script is run under windows. It is not using the Windows Scripting Host it is
standalone. The Win32::OLE modules that are loaded via the use
statements provide the automation support. See the ActivePerl documentation for more details.
AE6382
The script - generate_dhcp.pl
# Create a connection to Excelmy $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application','Quit');print "ERROR: ",$Win32::OLE::LastError,"\n" if $Win32::OLE::LastError;
# Turn off any alert boxes (such as the SaveAs Response)$Excel->{DisplayAlerts} = 0;
# Make Excel visible on the desktop$Excel->{Visible} = 1;
# Open the filemy $Book = $Excel->Workbooks->Open($excel_file);
# Create a reference to the worksheetmy $Sheet = $Book->Worksheets('ASDL');$Sheet->Activate();
AE6382
vbaxl10 – Application Object
AE6382
vbaxl10 – Application Visible
AE6382
vbaxl10 – Workbook Object
AE6382
vbaxl10 – Worksheet Object
AE6382
vbaxl10 – Worksheet Object
AE6382
The script - generate_dhcp.pl# Setupmy %mac_list = ();my %ip_list = ();
# Read the spreadsheet file contentsprint "Loading data from $excel_file\n";
my $row_count = $Sheet->Rows->{Count};print "row_count: $row_count\n";
foreach my $row (3..$row_count) { #print "ROW: $row\n";
# Look for empty name field my $name = $Sheet->Range("a${row}")->{Value}; print "name: $name\n"; last if $name eq '';
# Build the entry my @entry = (); my $i = 0; foreach my $col (qw(a b c d e f g h i j)) { #print "COL: $col\n"; $entry[$i++] = $Sheet->Range("${col}${row}")->{Value}; } #print @entry,"\n";
AE6382
vbaxl10 – Worksheet Object
AE6382
The script - generate_dhcp.pl# Save by MAC address MAC: { next MAC if $entry[1] eq '*';
# Check format of each MAC address $entry[1] =~ tr/A-F/a-f/; $entry[1] =~ tr/-/:/; my $mac = $entry[1]; my $flag = ($mac =~ m/[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}/); if ($flag != 1) { print "MAC Address format error: $entry[1]\n", "\t",@entry,"\n"; next MAC; } if (exists $mac_list{$entry[1]}) { my $entry1 = $mac_list{$entry[1]}; print "Duplicate MAC address: $entry[1]\n", "\t",@{$entry1},"\n", "\t",@entry,"\n"; next MAC; } $mac_list{$entry[1]} = \@entry; }
AE6382
The script - generate_dhcp.pl
# Save by IP address IP: { next IP if $entry[4] eq '*'; if (exists $ip_list{$entry[4]}) { my $entry1 = $ip_list{$entry[4]}; print "Duplicate IP address: $entry[4]\n", "\t",@{$entry1},"\n", "\t",@entry,"\n"; } $ip_list{$entry[4]} = \@entry; }}
AE6382
The script - generate_dhcp.pl# Generate dhcpd.conf fileprint "Generating dhcpd-hosts.conf file\n";open(OUT,">dhcpd-hosts.conf") || die "Unable to create dhcpd-hosts.conf: $!";binmode OUT;
# Insert the individual node fieldsforeach my $key (sort keys %mac_list) {
my $entry = $mac_list{$key};
# Skip non-dhcp controlled machines next if $entry->[5] ne 'Y' and $entry->[5] ne 'y';
my $host_name = $entry->[0]; $host_name =~ s/ +/_/g;
my $mac_address = $entry->[1];
my $ip_address = $entry->[4]; $ip_address =~ s/\.0{1,2}/./g;
print "SKIP: $mac_address $ip_address\n" if $ip_address eq '*'; next if $ip_address eq '*';
print OUT "host $host_name {\n"; print OUT "\thardware ethernet $mac_address;\n"; print OUT "\tfixed-address $ip_address;\n"; print OUT "}\n";}close(OUT);
__END__
AE6382
The output – dhcp-hosts.confhost df {
hardware ethernet 00:01:02:cf:78:16;fixed-address 172.16.5.3;
}host CD-COPIER {
hardware ethernet 00:01:02:cf:78:65;fixed-address 172.16.5.62;
}host scanner-pc {
hardware ethernet 00:01:02:cf:78:7d;fixed-address 172.16.5.46;
}host grc1 {
hardware ethernet 00:01:02:cf:78:81;fixed-address 172.16.96.11;
}host hippo {
hardware ethernet 00:01:e6:3e:31:15;fixed-address 172.16.2.28;
}host sonic {
hardware ethernet 00:01:e6:3f:06:a6;fixed-address 172.16.2.24;
}host falala {
hardware ethernet 00:01:e6:3f:06:ab;fixed-address 172.16.2.31;
}host python {
hardware ethernet 00:01:e6:3f:06:c5;fixed-address 172.16.2.14;
}……